標籤:sqlite
1.SQLite資料庫的特點
安卓手機內建, 小巧, 適合在手機中使用
不區分資料類型(主鍵除外)
SQL語句和MySQL幾乎相同
SQLite不使用JDBC串連, 使用的是Android自有的API
每個資料庫對應一個檔案
* 2.建立資料庫
定義類繼承SQLiteOpenHelper, 實現onCreate(), onUpgrade()
建立該類對象, 調用getWritableDatabse()或者getReadableDatabse()
情況1: 資料庫檔案不存在, 建立檔案, 開啟資料庫連接(得到SQLiteDatabase對象), 執行onCreate()方法
情況2: 資料庫檔案存在, 版本號碼沒變, 開啟資料庫連接
情況3: 資料庫檔案存在, 版本號碼提升, 升級資料庫, 開啟資料庫連接,執行onUpgrade()方法
情況4: 資料庫檔案存在, 版本號碼降低, 執行onDowngrade()方法, 方法中預設會拋出一個異常
代碼:MySQLiteOpenHelper.java
package com.oterman.mysqlite;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class MySQLiteOpenHelper extends SQLiteOpenHelper {/** * 由於父類沒有預設的無參數的建構函式,故需要顯示的寫出建構函式,然後去調用父類有參數的建構函式; * 參數1:context表示應用程式的環境,用來確定資料庫檔案的位置; * 參數2:資料庫檔案的名字; * 參數3:用來建立結果集Cursor的工廠,預設傳入null; * 參數4:資料的版本號碼,從1開始; * @param context * @param version */public MySQLiteOpenHelper(Context context,int version) {super(context,"myfirstdb.db",null,version);}public MySQLiteOpenHelper(Context context) {super(context,"myfirstdb.db",null,1);}/** * 如果資料庫檔案不存在,調用該方法; */@Overridepublic void onCreate(SQLiteDatabase db) {System.out.println("資料庫建立啦");db.execSQL("create table account(_id Integer primary key autoincrement,name varchar(40))");}/** * 資料庫檔案存在,版本號碼發生變化,會調用該方法; */@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {System.out.println("資料庫升級啦");}}
* 3.建立表或修改表
SQLiteDatabase類的execSQL()方法可以執行一條SQL語句
如果希望建立資料庫的時候就建立一些表, 那麼這個操作就可以在onCreate()方法中執行
如果希望在資料庫升級的時候做類似修改表添加表的操作, 可以在onUpgrade()方法中執行
*** 4.增刪改查
execSQL()方法可以進行增刪改操作
rawQuery()執行查詢操作, 得到Cursor, 調用moveToNext()判斷是否包含資料, 調用getString(), getInt()等方法擷取資料
insert(), delete(), update(), query() 四個方法內部也是調用execSQL()和rawQuery()的, 它們在ContentProvider中使用更方便(明天講)
* 5.交易管理
beginTransaction() 開啟事務
setTransactionSuccessful() 設定事務成功標記
endTransaction() 結束事務.
事務結束的時候, 會把最後一個成功標記之前的操作提交, 成功標記之後的操作復原
代碼:Accout.java
package domain;public class Account {private Integer id ;private String name;private Integer balance ;public Account(Integer id, String name, Integer balance) {super();this.id = id;this.name = name;this.balance = balance;}public Account() {super();}public Integer getId() {return id;}public Account(String name, Integer balance) {super();this.name = name;this.balance = balance;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getBalance() {return balance;}public void setBalance(Integer balance) {this.balance = balance;}@Overridepublic String toString() {return "Account [id=" + id + ", name=" + name + ", balance=" + balance + "]";}}
AccountDao.java
package com.oterman.dao;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import com.oterman.mysqlite.MySQLiteOpenHelper;import domain.Account;public class AccountDao {private Context context;public AccountDao(Context context) {this.context = context;}public void insert(Account a){//擷取資料庫;MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context);SQLiteDatabase db=helper.getWritableDatabase();//操作資料庫;db.execSQL("insert into account values(null,?,?)",new Object[]{a.getName(),a.getBalance()});//關閉資料庫;db.close();}//刪除記錄;public void delete(int i) {MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context);SQLiteDatabase db=helper.getWritableDatabase();//擷取資料庫;db.execSQL("delete from account where _id=?",new Object[]{i});db.close();}//修改資料庫;public void update(){MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context);SQLiteDatabase db=helper.getWritableDatabase();db.execSQL("update account set balance=? where _id<?",new Object[]{1000,9});db.close();}//查詢資料庫;public Account query(int i) {//擷取資料庫;MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context);SQLiteDatabase db=helper.getWritableDatabase();//執行查詢語句;擷取結果集;Cursor c=db.rawQuery("select name,balance from account where _id=?", new String[]{i+""});Account a=null;while(c.moveToNext()){String name=c.getString(0);int balance=c.getInt(1);a=new Account(i,name,balance);}return a;}//查詢所有;public List<Account> queryAll(){MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context);SQLiteDatabase db=helper.getWritableDatabase();List<Account> list=new ArrayList<Account>();String sql="select * from account";Cursor c=db.rawQuery(sql, null);while(c.moveToNext()){int id=c.getInt(0);String name=c.getString(c.getColumnIndex("name"));int balance=c.getInt(c.getColumnIndex("balance"));list.add(new Account(id,name,balance));}c.close();db.close();return list;}//示範事務public void trans(int fromId,int toId, int amount) {MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context);SQLiteDatabase db=helper.getWritableDatabase();String sql1="update account set balance=balance-? where _id=?";String sql2="update account set balance=balance+? where _id=?";try{db.beginTransaction();//開啟事務;db.execSQL(sql1, new Object[]{amount,fromId});db.execSQL(sql2, new Object[]{amount,toId});db.setTransactionSuccessful();//可以設定多個標記點;分組提交;如果在標記點之前未出現異常,則之前的所有的sql操作提交;db.execSQL(sql1, new Object[]{amount,fromId});db.execSQL(sql2, new Object[]{amount,toId});db.setTransactionSuccessful();db.execSQL(sql1, new Object[]{amount,fromId});int i=1/0;db.execSQL(sql2, new Object[]{amount,toId});db.setTransactionSuccessful();//標記點;出現異常時,該標記點至上一個標記點的所有內容被復原;}finally{db.endTransaction();db.close();}}}
Android下使用SQLite資料庫