欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android App使用SQLite數(shù)據(jù)庫的一些要點(diǎn)總結(jié)

 更新時(shí)間:2016年03月22日 16:05:43   作者:chenlijian  
這篇文章主要介紹了Android App使用SQLite數(shù)據(jù)庫的一些要點(diǎn)總結(jié),使用Sqlite作為應(yīng)用的嵌入式數(shù)據(jù)庫非常輕便,需要的朋友可以參考下

/DATA/data/包名/databases是該程序存放數(shù)據(jù)的目錄,DATA是Environment.getDataDirectory() 方法返回的路徑。找到數(shù)據(jù)庫之后可以選中user.db 執(zhí)行導(dǎo)出。
用真機(jī)調(diào)試,data目錄如果打不開,說明你的手機(jī)沒有root,改用模擬器就OK了。

1.獲取SQLiteDatabase對象:

SQLiteDatabase db = openOrCreateDatabase(File file, SQLiteDatabase.Cursor, Factory factor);

2.SQLiteDatabase提供了如下方法:

db.execSQL(sql)     //執(zhí)行任何SQL語句
db.insert(table, nullColumnHack, value)   //(增)
db.delete(table, whereClause, whereArgs)  //(刪)
db.updata(table, values, whereClause, whereArgs) //(改)
db.query(table,columns,whereClause,whereArgs,groupBy,having,orderBy) //(查)
db.rawQuery(sql, selectionArgs)  //可以使用SQL語句直接查詢

3.執(zhí)行query和rawQuery操作,返回一個(gè)Cursor游標(biāo)對象,它可以遍歷整個(gè)查詢處的內(nèi)容,Cursor提供了如下方法來移動(dòng)游標(biāo):

c.move( int offset)  //游標(biāo)向上或向下移動(dòng)指定行數(shù),正數(shù)向下,負(fù)數(shù)向上
c.moveToFirst()    //移動(dòng)到第一行,返回布爾值
c.moveToLast()
c.moveToNext()
c.moveToPostion(int postion)  //移動(dòng)到指定行,返回布爾值
c.moveToPrevious()  //移動(dòng)到上一行
c.isFirst();       //是否指向第一條 
c.isLast();       //是否指向最后一條 
c.isBeforeFirst();  //是否指向第一條之前 
c.isAfterLast();   //是否指向最后一條之后 
c.isNull(int columnIndex); //指定列是否為空(列基數(shù)為0) 
c.isClosed();     //游標(biāo)是否已關(guān)閉 
c.getCount();    //總數(shù)據(jù)項(xiàng)數(shù) 
c.getPosition();  //返回當(dāng)前游標(biāo)所指向的行數(shù) 
c.getColumnIndex(String columnName); //返回某列名對應(yīng)的列索引值 
c.getString(int columnIndex);         //返回當(dāng)前行指定列的值

下面是一個(gè)創(chuàng)建一個(gè)SQLiteDatabase對象,只用SQL語句進(jìn)行查詢的實(shí)例

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //每個(gè)程序都有自己的數(shù)據(jù)庫,而且互不干擾
  //創(chuàng)建一個(gè)數(shù)據(jù)庫,并且打開,這個(gè)方法返回的是一個(gè)SQLiteDadabase對象(如果沒有就創(chuàng)建,有就直接打開)
  //這個(gè)數(shù)據(jù)庫的名字叫user.db,這樣取名是為了以后導(dǎo)出到電腦上后方便第三方軟件打開,第二個(gè)參數(shù)是一個(gè)常量,此例表示私有別的數(shù)據(jù)庫無法訪問
  SQLiteDatabase db = openOrCreateDatabase("user.db", MODE_PRIVATE, null);
  //創(chuàng)建一張表 usertb ,主鍵名字建議寫成_id, 1個(gè)主鍵,3列, 加上_id總共4列
   db.execSQL("create table if not exists usertb(_id integer primary key autoincrement, name text not null, age integer not null, sex text not null)");
  //往這張表usertb中加3條數(shù)據(jù),分別3列,3個(gè)對應(yīng)的值
   db.execSQL("insert into usertb(name, age, sex) values('Jack','18','男')");
   db.execSQL("insert into usertb(name, age, sex) values('Hellen','19','女')");
   db.execSQL("insert into usertb(name, age, sex) values('Mike','20','男')");

  //查詢數(shù)據(jù),第一個(gè)參數(shù)還是一條語句,查詢方法,指定如何查找查詢條件, 第二個(gè)參數(shù)是查詢條件,默認(rèn)把數(shù)據(jù)全部查詢出來
  //這里的返回值是Cursor,是查詢數(shù)據(jù)后得到的管理集合的一個(gè)類,可以理解為list(游標(biāo)接口)
  Cursor c = db.rawQuery("select * from usertb", null);
   if (c!= null){                    //如果能查詢到數(shù)據(jù)
     c.moveToFirst();                //如果沒有處理過數(shù)據(jù),這條可以省略,默認(rèn)光標(biāo)第一行
     while(c.moveToNext()){          //每次只能查詢到一條數(shù)據(jù),判斷是否能查詢到下一行(重點(diǎn):每次光標(biāo)到達(dá)一行后,下面的語句依次打印那一行中的數(shù)據(jù),再循環(huán),打印下面一行數(shù)據(jù))
       Log.i ("info", " "+ c.getInt(c.getColumnIndex("_id")));       //第一個(gè)字段int型, 需要轉(zhuǎn)成String型才能用Log打?。ㄕ业竭@一條數(shù)據(jù)中字段角標(biāo)為0的integer型數(shù)據(jù))
       Log.i("info", c.getString(c.getColumnIndex("name")));      //第二個(gè)字段是text型
       Log.i("info", " "+c.getInt(c.getColumnIndex("age")));
       Log.i("info", c.getString(c.getColumnIndex("sex")));
       Log.i("info", "~~~~~~~~");                          //測試一次循環(huán)有多少數(shù)據(jù)被打印
     }
     c.close();                                  //查詢完,游標(biāo)一定要釋放
   }
   db.close();    
}

4.增刪查改的相關(guān)參數(shù):

table:查詢數(shù)據(jù)的表名
columns: 要查詢出來的列名
whereClause: 查詢條件子句,允許使用占位符"?"
whereArgs: 用于為占位符傳入?yún)?shù)值
groupBy:用于控制分組
having:用于對分組進(jìn)行過濾
orderBy:用于對記錄進(jìn)行排序

ContentValues是對key/value的一個(gè)包裝,使用它可以將要插入或者要修改的數(shù)據(jù)以key/value的形式進(jìn)行封裝,在使用相應(yīng)增改方法的時(shí)候直接使用。
它有兩個(gè)存入和取出兩個(gè)方法:

put(String key,Xxx);
getAsXxx(String Key);

下面一個(gè)實(shí)例,使用內(nèi)置函數(shù)操作數(shù)據(jù)庫增刪改查:

  

 SQLiteDatabase db = openOrCreateDatabase("user.db", MODE_PRIVATE, null);
  db.execSQL("create table if not exists usertb(_id integer primary key autoincrement, name text not null, age integer not null, sex integer not null) ");

  //在執(zhí)行增、改方法之前,先創(chuàng)建insert方法中的一個(gè)ContentValues對象,再對這個(gè)對象存入數(shù)據(jù),存完后把values插入
  ContentValues values = new ContentValues(); 
  //增
  values.put("name", "張三");
  values.put("age",18);
  values.put("sex","男");
  db.insert("usertb", null, values);  //插入方法的返回值是一個(gè)long,表示新添記錄的行號(hào)
  values.clear();    //在插入下一條數(shù)據(jù)前需要把values清空,再對values存入新數(shù)據(jù)
  values.put("name", "李四");
  values.put("age",19);
  values.put("sex","男");
  db.insert("usertb", null, values);  
  values.clear();
  values.put("name", "王五");
  values.put("age",20);
  values.put("sex","男");
  db.insert("usertb", null, values);  
  values.clear();
  //改 (將id大于的性別改成女
  values.put("sex", "女");
  db.update("usertb", values, "_id >?", new String[]{"2"});
  //刪 (將名字里帶三的人刪除)
  db.delete("uesrtb", "name like ?", new String [] {"%三%"});
  //查 (查詢usertb這張表,所有行都差,_id >0的數(shù)據(jù)都查,查詢出的數(shù)據(jù)按照name排序)
  Cursor c = db.query("usertb", null, "_id > ?", new String[]{"0"}, null, null, "name");

  c.close();

  //關(guān)閉當(dāng)前數(shù)據(jù)庫
  db.close();
  //刪除user.db數(shù)據(jù)庫(注意不是表名table)
  deleteDatabase("user.db");

   
5.SQLiteOpenHelper : 
SQLiteOpenHelper是一個(gè)幫助類,通過繼承它并實(shí)現(xiàn)onCreate方法和Upgrade方法,來管理我們的數(shù)據(jù)庫。

SQLiteDatabase db = helper.getWritableDatabase();
SQLiteDatabase db = helper.getReadableDatabase();

下面一個(gè)實(shí)例,新建一個(gè)類繼承SQLiteOpenHelper

public class DBOpenHelper extends SQLiteOpenHelper{    
  public DBOpenHelper(Context context, String name) {
    super(context, name, null, 1);
  }
  public DBOpenHelper(Context context, String name, CursorFactory factory,int version) {
    super(context, name, factory, version);
  }

  @Override//首次創(chuàng)建數(shù)據(jù)庫的時(shí)候調(diào)用 一般可以把建庫 建表的操作
  public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table if not exists stutb(_id integer primary key autoincrement,name text not null,sex text not null,age integer not null)");
    db.execSQL("insert into stutb(name,sex,age)values('張三','女',18)");
  }

  @Override//當(dāng)數(shù)據(jù)庫的版本發(fā)生變化的時(shí)候 會(huì)自動(dòng)執(zhí)行
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

  }
}

然后在主activity中就可以創(chuàng)建這個(gè)子類的對象,通過這個(gè)類的get方法得到SQLiteDatabase對象

DBOpenHelper helper =new DBOpenHelper(MainActivity.this, "stu.db");
SQLiteDatabase db = helper.getWritableDatabase();

相關(guān)文章

最新評論