Androide SQLiteDatabase資料庫操作(轉)

來源:互聯網
上載者:User

標籤:des   android   style   c   code   java   

SQLite可以解析大部分的標準SQL語句:
建表語句:create table 表名(主鍵名 integer primary key autoincrement(設定為自增列),其他列名及屬性)或(主鍵名 integer primary key )SQLite資料庫中將所有聲明為“integer primary key”的列自動識別為自增列。 

查詢語句:select * from 表名 where 條件子句 group by 分組子句 having…order by 排序子句 DESC(降序)(ASC 升序)。

分頁語句:select * from 表名 limit 記錄數 offset 開始位置

或者 select * from 表名 limit 開始位置,記錄數

插入語句:insert into 表名(欄位列表) values (值列表)

更新語句:update 表名 set 欄位名=值 where 條件子句

刪除語句:delete from 表名 where 條件子句

刪表語句:drop table if exists 表名

增加欄位語句:

alter table 表名 add column 列名 欄位類型

修改資料庫名稱

alter table 表名 rename to 新表名

 

而為了方便對資料庫進行版本管理,建議在開發項目的時候使用SQLiteOpenHelper類,它提供了兩個重要的方法,分別是

onCreate(SQLiteDatabase db)——用於初次使用軟體時產生資料庫,一旦資料庫存在則不會調用此方法。函數是在第一次建立資料庫的時候執行的,僅僅產生DataBaseHelper對象的時候是不會調用該函數的,而只有當實現DataBaseHelper對象的getReadableDataBase時或者是調用了getWritableDataBase時,如果是第一次建立資料庫,那麼就一定會調用onCreate()函數 。

onUpgrade(SQLiteDatabase db,int oldVersion,int vewVersion)——用於升級軟體時更新資料庫表結構,如增加表、欄欄位等操作。

提示一下,在軟體升級前,最好對原有資料進行備份,在新表建好後把資料匯入新表中。

實現了這兩個方法,就可以用他的getWritableDatabase()和getReadableDatabase()來獲得資料庫。

 

強調下:當第一次建立資料庫時,當實現DataBaseHelper對象的getReadableDataBase時或者是調用了getWritableDataBase時,系統自動調用oncreater()方法(當然程式中也可以調用);

如果使用者需要升級資料庫表結構,需要主動調用onUpgrade(SQLiteDatabase db,int oldVersion,int vewVersion),傳入一個新的版本的號。

 

查詢操作:在使用SQLite的進行查詢時最好用預留位置“?”來代替各值,注意直接用?而不是‘?‘。

例如:
SQLiteDatabase db=databaseHelper.getWritableDatabase();
db.execSQL(“update person set name=?,age=? where personid=?”,new Obect{person.getName(),person.getAge(),person.getId()});
execSQL()方法是用來執行除尋找語句外的sql語句,尋找語句用rawQuery()來執行它會放回一個Cursor。當然,他還提供了封裝好的Java類方法供我們操作。具體方法不介紹了,可以直接查看文檔中SQLiteDatabase類的用法。

插入操作:

插入一行記錄,可以通過三種方式實現:

第一種,封裝列-值對,調用 db.insert(table, nullColumnHack, values)或者db.insertOrThrow(table, nullColumnHack, values)方法插入,插入成功返回行號,失敗時返回-1.

ContentValues values=new ContentValues();
        values.put(MyHelper.COUNTRY, "中國");
        values.put(MyHelper.CODE,86);  
        //插入一行資料
        db.insert(MyHelper.TD_NAME, MyHelper.ID, values);

如果傳入的values為空白的話,則將插入一行各列指為NULL或0的資料,(如果列是Integer類型,則0,其它類型為NULL)。

第二種,組織SQL語句,使用 db.execSQL(String sql)或 db.execSQL(String sql,Object[] data)執行,無傳回值。

        String sql1="insert into "
          +MyHelper.TD_NAME+"("
          +MyHelper.COUNTRY+","
          +MyHelper.CODE+")values"
          +"(?,789)";
        String[] data={"Japan"};    
        db.execSQL(sql1, data); 

第三種,使用update(String table, ContentValues values, String whereClause, String[] whereArgs)

更新行資料,注意WhereClause為update的where條件,不帶關鍵字where:如:

SQLiteDatabase db = this.getWritableDatabase();
  String where = BOOK_ID + " = ?";
  String[] whereValue = { Integer.toString(id) };

  ContentValues cv = new ContentValues();
  cv.put(BOOK_NAME, bookname);
  cv.put(BOOK_AUTHOR, author);
  db.update(TABLE_NAME, cv, where, whereValue);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.