Android-Sqlite使用手冊

來源:互聯網
上載者:User

標籤:

sqlite3使用手冊 查看資料庫版本sqlite3 -version
開啟或建立資料庫          sqlite3 DatabaseName.db          
查看資料庫檔案資訊          .database   
查看資料庫表          .table          
退出sqlite          .quit 或 .exit          
列出當前顯示格式的配置          .show          
顯示資料庫結構/ 顯示表的結構          .schema          .schema  表名          
設定分隔字元          .separator    分隔字元            
顯示標題列          .headers   on/off          
設定顯示模式          .mode    模式                    
設定NULL值顯示樣式          .nullvalue               
sqlite簡單文法 建立資料表          create table table_name(field type1,fieldtype1,….);          table_name是要建立資料表的名稱,field x上是資料庫表內欄位名字,typex則是欄位類型。          
添加資料記錄                   insert into table_name(列 field1,field2,….)values(值val1,val2,….);                        例如,往老師資訊表添加資料:          
修改資料         update 表 set 列 = ‘新值‘ [where 條件陳述式]                    
刪除資料          delete from 表 [where 條件陳述式]          如果設定where條件子句,則刪除合格資料記錄;如果沒有設定條件陳述式,則刪除所有記錄。          
修改資料          update 表 set 列 = ‘新值’ [where 條件陳述式]               update語句用來更新表中的某個列,如果不設定條件,則所有記錄的這一列都被更新;如果設定了條件,則合格這一列被更新,where子句被用來設定條件,如下例:                                               
刪除資料          delete from 表 [where 條件陳述式]          如果設定where條件子句,則刪除合格資料記錄;如果沒有設定條件陳述式,則刪除所有記錄。          
查詢資料記錄          查詢輸出資料行出資料記錄(select * from table_name;)          限制輸出資料記錄數量(select * from table_name limit val;)          升序輸出資料記錄(select * from table_name order by field asc;)             降序輸出資料記錄(select * from table order by field desc;)          條件查詢 (select * from table_name where expression;)                         (select * from table_name where field in(‘val1’,’val2’,’val3’);)                         (select * from table_name where fieldbetween val1 and val2;)          查詢記錄數目(select count(*) from table_name;)
刪除資料表          drop table table_name;
Android編碼手冊 建立輔助類public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String  name = "count"; //資料庫名稱
    private static final int  version = 1; //資料庫版本
    public DatabaseHelper(Context context) {
        super(context,  name, null,  version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS person (personid integer primary key autoincrement, name varchar(20), age INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("ALTER TABLE person ADD phone VARCHAR(12)"); //往表中增加一列
    }
}

插入資料     SQLiteDatabase db = DatabaseHelper.(mContext,1).getWritableDatabase();
db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)”, new String[] { "The Da Vinci Code", "Dan Brown", "454", "16.96" });
db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)”, new String[] { "The Lost Symbol", "Dan Brown", "510", "19.95" });
db.close();

刪除資料SQLiteDatabase db = DatabaseHelper.(mContext, 1).getWritableDatabase();
db.execSQL("delete from Book where pages > ?", new String[] { "500" });
db.close();

更新資料SQLiteDatabase db = DatabaseHelper.(mContext, 1).getWritableDatabase();
db.execSQL("update Book set price = ? where name = ?", new String[] { "10.99",
"The Da Vinci Code" });
db.close();

查詢資料SQLiteDatabase db=DatabaseHelper.(mContext,1).getReadableDatabase();
Cursor cursor=db.rawQuery("select * from Book",null);
//跳轉到指標最開始
if(cursor.moveToFirst()){
    do{
    //擷取資料
    /* cursor.getString(cursor.getColumnIndex("isRemind”)) */
    }while(cursor.moveToNext());
}
db.close();
 
參考使用介紹:《第一行代碼》-- 6.4節 -- SQLite資料庫儲存SQLite 教程:http://www.runoob.com/sqlite/sqlite-tutorial.html

Android-Sqlite使用手冊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.