Android 操作SQLite基本用法
生活随笔
收集整理的這篇文章主要介紹了
Android 操作SQLite基本用法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在Android
開發(fā)中SQLite起著很重要的作用,網(wǎng)上SQLite的教程有很多很多,不過那些教程大多數(shù)都講得不是很全面。本人總結(jié)了一些SQLite的常用的方法,借著論壇的大賽,跟大家分享分享的。
一.SQLite的介紹
1.SQLite簡(jiǎn)介
SQLite是一款輕型的數(shù)據(jù)庫(kù),是遵守ACID的關(guān)聯(lián)式數(shù)據(jù)庫(kù)管理系統(tǒng),它的設(shè)計(jì)目標(biāo)是嵌入??式的,而且目前已經(jīng)在很多嵌入式產(chǎn)品中使用了它,它占用資源非常的低,在嵌入式設(shè)備中,可能只需要幾百K的內(nèi)存就夠了。它能夠支持 Windows/Linux/Unix等等主流的操作系統(tǒng),同時(shí)能夠跟很多程序語(yǔ)言相結(jié)合,比如Tcl、PHP、Java、C++、.Net等,還有ODBC接口,同樣比起 Mysql、PostgreSQL這兩款開源世界著名的數(shù)據(jù)庫(kù)管理系統(tǒng)來(lái)講,它的處理速度比他們都快。
2.SQLite的特點(diǎn):
SQLite和C/S模式的數(shù)據(jù)庫(kù)軟件不同,它是進(jìn)程內(nèi)的數(shù)據(jù)庫(kù)引擎,因此不存在數(shù)據(jù)庫(kù)的客戶端和服務(wù)器。使用SQLite一般只需要帶上它的一個(gè)動(dòng)態(tài)??庫(kù),就可以享受它的全部功能。而且那個(gè)動(dòng)態(tài)庫(kù)的尺寸也挺小,以版本3.6.11為例,Windows下487KB、Linux下347KB。
SQLite的核心引擎本身不依賴第三方的軟件,使用它也不需要"安裝"。有點(diǎn)類似那種綠色軟件。
數(shù)據(jù)庫(kù)中所有的信息(比如表、視圖等)都包含在一個(gè)文件內(nèi)。這個(gè)文件可以自由復(fù)制到其它目錄或其它機(jī)器上。
除了主流操作系統(tǒng) windows,linux之后,SQLite還支持其它一些不常用的操作系統(tǒng)。
同一列中的數(shù)據(jù)可以是不同類型
這個(gè)相信大家都懂的!!!!!!!!!!!!
3.SQLite數(shù)據(jù)類型
一般數(shù)據(jù)采用的固定的靜態(tài)數(shù)據(jù)類型,而SQLite采用的是動(dòng)態(tài)數(shù)據(jù)類型,會(huì)根據(jù)存入值自動(dòng)判斷。SQLite具有以下五種常用的數(shù)據(jù)類型:
NULL: 這個(gè)值為空值
VARCHAR(n):長(zhǎng)度不固定且其最大長(zhǎng)度為 n 的字串,n不能超過 4000。
CHAR(n):長(zhǎng)度固定為n的字串,n不能超過 254。
INTEGER: 值被標(biāo)識(shí)為整數(shù),依據(jù)值的大小可以依次被存儲(chǔ)為1,2,3,4,5,6,7,8.
REAL: 所有值都是浮動(dòng)的數(shù)值,被存儲(chǔ)為8字節(jié)的IEEE浮動(dòng)標(biāo)記序號(hào).
TEXT: 值為文本字符串,使用數(shù)據(jù)庫(kù)編碼存儲(chǔ)(TUTF-8, UTF-16BE or UTF-16-LE).
BLOB: 值是BLOB數(shù)據(jù)塊,以輸入的數(shù)據(jù)格式進(jìn)行存儲(chǔ)。如何輸入就如何存儲(chǔ),不改??變格式。
DATA :包含了 年份、月份、日期。
TIME: 包含了 小時(shí)、分鐘、秒。
相信學(xué)過數(shù)據(jù)庫(kù)的童鞋對(duì)這些數(shù)據(jù)類型都不陌生的!!!!!!!!!!
二.SQLiteDatabase的介紹
Android提供了創(chuàng)建和是用SQLite數(shù)據(jù)庫(kù)的API。SQLiteDatabase代表一個(gè)數(shù)據(jù)庫(kù)對(duì)象,提供了操作數(shù)據(jù)庫(kù)的一些方法。在Android的SDK目錄下有sqlite3工具,我們可以利用它創(chuàng)建數(shù)據(jù)庫(kù)、創(chuàng)建表和執(zhí)行一些SQL語(yǔ)句。下面是SQLiteDatabase的常用方法。?
SQLiteDatabase的常用方法?
Google公司命名這些方法的名稱都是非常形象的。例如openOrCreateDatabase,我們從字面英文含義就能看出這是個(gè)打開或創(chuàng)建數(shù)據(jù)庫(kù)的方法。
1、打開或者創(chuàng)建數(shù)據(jù)庫(kù)
在Android 中使用SQLiteDatabase的靜態(tài)方法openOrCreateDatabase(String??path,SQLiteDatabae.CursorFactory??factory)打開或者創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)。它會(huì)自動(dòng)去檢測(cè)是否存在這個(gè)數(shù)據(jù)庫(kù),如果存在則打開,不存在則創(chuàng)建一個(gè)數(shù)據(jù)庫(kù);創(chuàng)建成功則返回一個(gè)SQLiteDatabase對(duì)象,否則拋出異常FileNotFoundException。
下面是創(chuàng)建名為“stu.db”數(shù)據(jù)庫(kù)的代碼:
openOrCreateDatabase(String??path,SQLiteDatabae.CursorFactory??factory)
參數(shù)1??數(shù)據(jù)庫(kù)創(chuàng)建的路徑
db=SQLiteDatabase.openOrCreateDatabase("/data/data/com.lingdududu.db/databases/stu.db",null);??
2、創(chuàng)建表
創(chuàng)建一張表的步驟很簡(jiǎn)單:
下面的代碼創(chuàng)建了一張用戶表,屬性列為:id(主鍵并且自動(dòng)增加)、sname(學(xué)生姓名)、snumber(學(xué)號(hào))
private?void?createTable(SQLiteDatabase?db){??? //創(chuàng)建表SQL語(yǔ)句??? String?stu_table="create?table?usertable(_id?integer?primary?key?autoincrement,sname?text,snumber?text)";??? //執(zhí)行SQL語(yǔ)句??? db.execSQL(stu_table);??? }??
3、插入數(shù)據(jù)
插入數(shù)據(jù)有兩種方法:
①SQLiteDatabase的insert(String table,String nullColumnHack,ContentValues??values)方法,
??參數(shù)1??表名稱,
??參數(shù)2??空列的默認(rèn)值
??參數(shù)3??ContentValues類型的一個(gè)封裝了列名稱和列值的Map;
②編寫插入數(shù)據(jù)的SQL語(yǔ)句,直接調(diào)用SQLiteDatabase的execSQL()方法來(lái)執(zhí)行
第一種方法的代碼:
private?void?insert(SQLiteDatabase?db){??? //實(shí)例化常量值??? ContentValues?cValue?=?new?ContentValues();??? //添加用戶名??? cValue.put("sname","xiaoming");??? //添加密碼??? cValue.put("snumber","01005");??? //調(diào)用insert()方法插入數(shù)據(jù)??? db.insert("stu_table",null,cValue);??? }???
第二種方法的代碼:
private?void?insert(SQLiteDatabase?db){??? //插入數(shù)據(jù)SQL語(yǔ)句??? String?stu_sql="insert?into?stu_table(sname,snumber)?values('xiaoming','01005')";??? //執(zhí)行SQL語(yǔ)句??? db.execSQL(sql);??? }???
4、刪除數(shù)據(jù)
刪除數(shù)據(jù)也有兩種方法:
①調(diào)用SQLiteDatabase的delete(String table,String whereClause,String[]??whereArgs)方法
參數(shù)1??表名稱?
參數(shù)2??刪除條件
參數(shù)3??刪除條件值數(shù)組
②編寫刪除SQL語(yǔ)句,調(diào)用SQLiteDatabase的execSQL()方法來(lái)執(zhí)行刪除。
第一種方法的代碼:
private?void?delete(SQLiteDatabase?db)?{??? //刪除條件??? String?whereClause?=?"id=?";??? //刪除條件參數(shù)??? String[]?whereArgs?=?{String.valueOf(2)};??? //執(zhí)行刪除??? db.delete("stu_table",whereClause,whereArgs);??? }???
第二種方法的代碼:
private?void?delete(SQLiteDatabase?db)?{??? //刪除SQL語(yǔ)句??? String?sql?=?"delete?from?stu_table?where?_id?=?6";??? //執(zhí)行SQL語(yǔ)句??? db.execSQL(sql);??? }???
5、修改數(shù)據(jù)
修改數(shù)據(jù)有兩種方法:
①調(diào)用SQLiteDatabase的update(String table,ContentValues values,String??whereClause, String[]??whereArgs)方法
參數(shù)1??表名稱
參數(shù)2??跟行列ContentValues類型的鍵值對(duì)Key-Value
參數(shù)3??更新條件(where字句)
參數(shù)4??更新條件數(shù)組
②編寫更新的SQL語(yǔ)句,調(diào)用SQLiteDatabase的execSQL執(zhí)行更新。
第一種方法的代碼:
private?void?update(SQLiteDatabase?db)?{??? //實(shí)例化內(nèi)容值?ContentValues?values?=?new?ContentValues();??? //在values中添加內(nèi)容??? values.put("snumber","101003");??? //修改條件??? String?whereClause?=?"id=?";??? //修改添加參數(shù)??? String[]?whereArgs={String.valuesOf(1)};??? //修改??? db.update("usertable",values,whereClause,whereArgs);??? }???
第二種方法的代碼:
private?void?update(SQLiteDatabase?db){??? //修改SQL語(yǔ)句??? String?sql?=?"update?stu_table?set?snumber?=?654321?where?id?=?1";??? //執(zhí)行SQL??? db.execSQL(sql);??? }???
6、查詢數(shù)據(jù)
在Android中查詢數(shù)據(jù)是通過Cursor類來(lái)實(shí)現(xiàn)的,當(dāng)我們使用SQLiteDatabase.query()方法時(shí),會(huì)得到一個(gè)Cursor對(duì)象,Cursor指向的就是每一條數(shù)據(jù)。它提供了很多有關(guān)查詢的方法,具體方法如下:
public??Cursor query(String table,String[] columns,String selection,String[]??selectionArgs,String groupBy,String having,String orderBy,String limit);
各個(gè)參數(shù)的意義說(shuō)明:
參數(shù)table:表名稱
參數(shù)columns:列名稱數(shù)組
參數(shù)selection:條件字句,相當(dāng)于where
參數(shù)selectionArgs:條件字句,參數(shù)數(shù)組
參數(shù)groupBy:分組列
參數(shù)having:分組條件
參數(shù)orderBy:排序列
參數(shù)limit:分頁(yè)查詢限制
參數(shù)Cursor:返回值,相當(dāng)于結(jié)果集ResultSet
Cursor是一個(gè)游標(biāo)接口,提供了遍歷查詢結(jié)果的方法,如移動(dòng)指針方法move(),獲得列值方法getString()等.
Cursor游標(biāo)常用方法
下面就是用Cursor來(lái)查詢數(shù)據(jù)庫(kù)中的數(shù)據(jù),具體代碼如下:
private?void?query(SQLiteDatabase?db)?{??? //查詢獲得游標(biāo)??? Cursor?cursor?=?db.query?("usertable",null,null,null,null,null,null);??? ?? //判斷游標(biāo)是否為空??? if(cursor.moveToFirst()?{??? //遍歷游標(biāo)??? for(int?i=0;i<cursor.getCount();i++){??? cursor.move(i);??? //獲得ID??? int?id?=?cursor.getInt(0);??? //獲得用戶名??? String?username=cursor.getString(1);??? //獲得密碼??? String?password=cursor.getString(2);??? //輸出用戶信息?System.out.println(id+":"+sname+":"+snumber);??? }??? }??? }??
7、刪除指定表
編寫插入數(shù)據(jù)的SQL語(yǔ)句,直接調(diào)用SQLiteDatabase的execSQL()方法來(lái)執(zhí)行
private?void?drop(SQLiteDatabase?db){??? //刪除表的SQL語(yǔ)句??? String?sql?="DROP?TABLE?stu_table";??? //執(zhí)行SQL??? db.execSQL(sql);??? }???
三.? SQLiteOpenHelper
該類是SQLiteDatabase一個(gè)輔助類。這個(gè)類主要生成一??個(gè)數(shù)據(jù)庫(kù),并對(duì)數(shù)據(jù)庫(kù)的版本進(jìn)行管理。當(dāng)在程序當(dāng)中調(diào)用這個(gè)類的方法getWritableDatabase()或者 getReadableDatabase()方法的時(shí)候,如果當(dāng)時(shí)沒有數(shù)據(jù),那么Android系統(tǒng)就會(huì)自動(dòng)生成一個(gè)數(shù)據(jù)庫(kù)。 SQLiteOpenHelper 是一個(gè)抽象類,我們通常需要繼承它,并且實(shí)現(xiàn)里面的3個(gè)函數(shù):
1.onCreate(SQLiteDatabase)
在數(shù)據(jù)庫(kù)第一次生成的時(shí)候會(huì)調(diào)用這個(gè)方法,也就是說(shuō),只有在創(chuàng)建數(shù)據(jù)庫(kù)的時(shí)候才會(huì)調(diào)用,當(dāng)然也有一些其它的情況,一般我們?cè)谶@個(gè)方法里邊生成數(shù)據(jù)庫(kù)表。
2.??onUpgrade(SQLiteDatabase,int,int)?
當(dāng)數(shù)據(jù)庫(kù)需要升級(jí)的時(shí)候,Android系統(tǒng)會(huì)主動(dòng)的調(diào)用這個(gè)方法。一般我們?cè)谶@個(gè)方法里邊刪除數(shù)據(jù)表,并建立新的數(shù)據(jù)表,當(dāng)然是否還需要做其他的操作,完全取決于應(yīng)用的需求。
3.??onOpen(SQLiteDatabase):
這是當(dāng)打開數(shù)據(jù)庫(kù)時(shí)的回調(diào)函數(shù),一般在程序中不是很常使用。
寫了這么多,改用用實(shí)際例子來(lái)說(shuō)明上面的內(nèi)容了。下面這個(gè)操作數(shù)據(jù)庫(kù)的實(shí)例實(shí)現(xiàn)了創(chuàng)建數(shù)據(jù)庫(kù),創(chuàng)建表以及數(shù)據(jù)庫(kù)的增刪改查的操作。
該實(shí)例有兩個(gè)類:
com.lingdududu.testSQLite 調(diào)試類
com.lingdududu.testSQLiteDb??數(shù)據(jù)庫(kù)輔助類
SQLiteActivity.java
package?com.lingdududu.testSQLite;?? ?? import?com.lingdududu.testSQLiteDb.StuDBHelper;?? ?? import?android.app.Activity;?? import?android.content.ContentValues;?? import?android.database.Cursor;?? import?android.database.sqlite.SQLiteDatabase;?? import?android.os.Bundle;?? import?android.view.View;?? import?android.view.View.OnClickListener;?? import?android.widget.Button;?? /*? *?@author?lingdududu? */?? public?class?SQLiteActivity?extends?Activity?{?? /**?Called?when?the?activity?is?first?created.?*/?? //聲明各個(gè)按鈕?? private?Button?createBtn;?? private?Button?insertBtn;?? private?Button?updateBtn;?? private?Button?queryBtn;?? private?Button?deleteBtn;?? private?Button?ModifyBtn;?? @Override?? public?void?onCreate(Bundle?savedInstanceState)?{?? super.onCreate(savedInstanceState);?? setContentView(R.layout.main);?? ?? //調(diào)用creatView方法?? creatView();?? //setListener方法?? setListener();??? }?? ?? //通過findViewById獲得Button對(duì)象的方法?? private?void?creatView(){?? createBtn?=?(Button)findViewById(R.id.createDatabase);?? updateBtn?=?(Button)findViewById(R.id.updateDatabase);?? insertBtn?=?(Button)findViewById(R.id.insert);?? ModifyBtn?=?(Button)findViewById(R.id.update);?? queryBtn?=?(Button)findViewById(R.id.query);?? deleteBtn?=?(Button)findViewById(R.id.delete);?? }?? ?? //為按鈕注冊(cè)監(jiān)聽的方法?? private?void?setListener(){?? createBtn.setOnClickListener(new?CreateListener());?? updateBtn.setOnClickListener(new?UpdateListener());?? insertBtn.setOnClickListener(new?InsertListener());?? ModifyBtn.setOnClickListener(new?ModifyListener());?? queryBtn.setOnClickListener(new?QueryListener());?? deleteBtn.setOnClickListener(new?DeleteListener());?? }?? ?? //創(chuàng)建數(shù)據(jù)庫(kù)的方法?? class?CreateListener?implements?OnClickListener{?? ?? @Override?? public?void?onClick(View?v)?{?? //創(chuàng)建StuDBHelper對(duì)象?? StuDBHelper?dbHelper?=?new?StuDBHelper(SQLiteActivity.this,"stu_db",null,1);?? //得到一個(gè)可讀的SQLiteDatabase對(duì)象?? SQLiteDatabase?db?=dbHelper.getReadableDatabase();?? }??? }?? ?? //更新數(shù)據(jù)庫(kù)的方法?? class?UpdateListener?implements?OnClickListener{?? ?? @Override?? public?void?onClick(View?v)?{?? //?數(shù)據(jù)庫(kù)版本的更新,由原來(lái)的1變?yōu)??? StuDBHelper?dbHelper?=?new?StuDBHelper(SQLiteActivity.this,"stu_db",null,2);?? SQLiteDatabase?db?=dbHelper.getReadableDatabase();?? }??? }?? ?? //插入數(shù)據(jù)的方法?? class?InsertListener?implements?OnClickListener{?? ?? @Override?? public?void?onClick(View?v)?{?? ?? StuDBHelper?dbHelper?=?new?StuDBHelper(SQLiteActivity.this,"stu_db",null,1);?? //得到一個(gè)可寫的數(shù)據(jù)庫(kù)?? SQLiteDatabase?db?=dbHelper.getWritableDatabase();?? ?? //生成ContentValues對(duì)象?//key:列名,value:想插入的值??? ContentValues?cv?=?new?ContentValues();?? //往ContentValues對(duì)象存放數(shù)據(jù),鍵-值對(duì)模式?? cv.put("id",?1);?? cv.put("sname",?"xiaoming");?? cv.put("sage",?21);?? cv.put("ssex",?"male");?? //調(diào)用insert方法,將數(shù)據(jù)插入數(shù)據(jù)庫(kù)?? db.insert("stu_table",?null,?cv);?? //關(guān)閉數(shù)據(jù)庫(kù)?? db.close();?? }??? }?? ?? //查詢數(shù)據(jù)的方法?? class?QueryListener?implements?OnClickListener{?? ?? @Override?? public?void?onClick(View?v)?{?? ?? StuDBHelper?dbHelper?=?new?StuDBHelper(SQLiteActivity.this,"stu_db",null,1);?? //得到一個(gè)可寫的數(shù)據(jù)庫(kù)?? SQLiteDatabase?db?=dbHelper.getReadableDatabase();?? //參數(shù)1:表名??? //參數(shù)2:要想顯示的列??? //參數(shù)3:where子句??? //參數(shù)4:where子句對(duì)應(yīng)的條件值??? //參數(shù)5:分組方式??? //參數(shù)6:having條件??? //參數(shù)7:排序方式??? Cursor?cursor?=?db.query("stu_table",?new?String[]{"id","sname","sage","ssex"},?"id=?",?new?String[]{"1"},?null,?null,?null);?? while(cursor.moveToNext()){?? String?name?=?cursor.getString(cursor.getColumnIndex("sname"));?? String?age?=?cursor.getString(cursor.getColumnIndex("sage"));?? String?sex?=?cursor.getString(cursor.getColumnIndex("ssex"));?? System.out.println("query------->"?+?"姓名:"+name+"?"+"年齡:"+age+"?"+"性別:"+sex);?? }?? //關(guān)閉數(shù)據(jù)庫(kù)?? db.close();?? }??? }?? ?? //修改數(shù)據(jù)的方法?? class?ModifyListener?implements?OnClickListener{?? ?? @Override?? public?void?onClick(View?v)?{?? ?? StuDBHelper?dbHelper?=?new?StuDBHelper(SQLiteActivity.this,"stu_db",null,1);?? //得到一個(gè)可寫的數(shù)據(jù)庫(kù)?? SQLiteDatabase?db?=dbHelper.getWritableDatabase();?? ContentValues?cv?=?new?ContentValues();?? cv.put("sage",?"23");?? //where?子句?"?"是占位符號(hào),對(duì)應(yīng)后面的"1",?? String?whereClause="id=?";?? String?[]?whereArgs?=?{String.valueOf(1)};?? //參數(shù)1?是要更新的表名?? //參數(shù)2?是一個(gè)ContentValeus對(duì)象?? //參數(shù)3?是where子句?? db.update("stu_table",?cv,?whereClause,?whereArgs);?? }?? }?? ?? //刪除數(shù)據(jù)的方法?? class?DeleteListener?implements?OnClickListener{?? ?? @Override?? public?void?onClick(View?v)?{?? ?? StuDBHelper?dbHelper?=?new?StuDBHelper(SQLiteActivity.this,"stu_db",null,1);?? //得到一個(gè)可寫的數(shù)據(jù)庫(kù)?? SQLiteDatabase?db?=dbHelper.getReadableDatabase();?? String?whereClauses?=?"id=?";?? String?[]?whereArgs?=?{String.valueOf(2)};?? //調(diào)用delete方法,刪除數(shù)據(jù)??? db.delete("stu_table",?whereClauses,?whereArgs);?? }??? }?? }??
StuDBHelper.java [java] view plain copy print?package?com.lingdududu.testSQLiteDb;?? ?? import?android.content.Context;?? import?android.database.sqlite.SQLiteDatabase;?? import?android.database.sqlite.SQLiteDatabase.CursorFactory;?? import?android.database.sqlite.SQLiteOpenHelper;?? import?android.util.Log;?? ?? public?class?StuDBHelper?extends?SQLiteOpenHelper?{?? ?? private?static?final?String?TAG?=?"TestSQLite";?? public?static?final?int?VERSION?=?1;?? ?? //必須要有構(gòu)造函數(shù)?? public?StuDBHelper(Context?context,?String?name,?CursorFactory?factory,?? int?version)?{?? super(context,?name,?factory,?version);?? }?? ?? //?當(dāng)?shù)谝淮蝿?chuàng)建數(shù)據(jù)庫(kù)的時(shí)候,調(diào)用該方法??? public?void?onCreate(SQLiteDatabase?db)?{?? String?sql?=?"create?table?stu_table(id?int,sname?varchar(20),sage?int,ssex?varchar(10))";?? //輸出創(chuàng)建數(shù)據(jù)庫(kù)的日志信息?? Log.i(TAG,?"create?Database------------->");?? //execSQL函數(shù)用于執(zhí)行SQL語(yǔ)句?? db.execSQL(sql);?? }?? ?? //當(dāng)更新數(shù)據(jù)庫(kù)的時(shí)候執(zhí)行該方法?? public?void?onUpgrade(SQLiteDatabase?db,?int?oldVersion,?int?newVersion)?{?? //輸出更新數(shù)據(jù)庫(kù)的日志信息?? Log.i(TAG,?"update?Database------------->");?? }?? }??
main.xml
<?xml?version="1.0"?encoding="utf-8"?>?? <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?? android:orientation="vertical"?? android:layout_width="fill_parent"?? android:layout_height="fill_parent"?? >?? <TextView??? android:layout_width="fill_parent"??? android:layout_height="wrap_content"??? android:text="@string/hello"?? />?? <Button?? android:id="@+id/createDatabase"?? android:layout_width="fill_parent"??? android:layout_height="wrap_content"??? android:text="創(chuàng)建數(shù)據(jù)庫(kù)"?? />??? <Button?? android:id="@+id/updateDatabase"?? android:layout_width="fill_parent"??? android:layout_height="wrap_content"??? android:text="更新數(shù)據(jù)庫(kù)"?? />??? <Button?? android:id="@+id/insert"?? android:layout_width="fill_parent"??? android:layout_height="wrap_content"??? android:text="插入數(shù)據(jù)"?? />?? <Button?? android:id="@+id/update"?? android:layout_width="fill_parent"??? android:layout_height="wrap_content"??? android:text="更新數(shù)據(jù)"?? />?? <Button?? android:id="@+id/query"?? android:layout_width="fill_parent"??? android:layout_height="wrap_content"??? android:text="查詢數(shù)據(jù)"?? />?? <Button?? android:id="@+id/delete"?? android:layout_width="fill_parent"??? android:layout_height="wrap_content"??? android:text="刪除數(shù)據(jù)"?? />?? </LinearLayout>??
程序運(yùn)行的效果圖:
1.在命令行窗口輸入adb??shell回車,就進(jìn)入了Linux命令行,現(xiàn)在就可以使用Linux的命令了。
2.ls回車,顯示所有的東西,其中有個(gè)data。
3.cd data回車,再ls回車,cd??data回車,ls回車后就會(huì)看到很多的com................,那就是系統(tǒng)上的應(yīng)用程序包名,找到你數(shù)據(jù)庫(kù)程序的包名,然后進(jìn)入。
4.進(jìn)去后在查看所有,會(huì)看到有databases,進(jìn)入databases,顯示所有就會(huì)發(fā)現(xiàn)你的數(shù)據(jù)庫(kù)名字,這里使用的是"stu_db"。
5.sqlite3 stu_db回車就進(jìn)入了你的數(shù)據(jù)庫(kù)了,然后“.schema”就會(huì)看到該應(yīng)用程序的所有表及建表語(yǔ)句。
6.之后就可以使用標(biāo)準(zhǔn)的SQL語(yǔ)句查看剛才生成的數(shù)據(jù)庫(kù)及對(duì)數(shù)據(jù)執(zhí)行增刪改查了。
注:ls,cd等命令都是linux的基本命令,不了解的同學(xué)可以看看有關(guān)這方面的資料。
下面介紹幾個(gè)在SQLite中常用到的adb命令:
查看
.database 顯示數(shù)據(jù)庫(kù)信息;
.tables 顯示表名稱;
.schema 命令可以查看創(chuàng)建數(shù)據(jù)表時(shí)的SQL命令;
.schema table_name 查看創(chuàng)建表table_name時(shí)的SQL的命令;
插入記錄
insert into table_name values (field1, field2, field3...);
查詢
select * from table_name;查看table_name表中所有記錄;
select * from table_name where field1='xxxxx'; 查詢符合指定條件的記錄;
刪除
drop table_name;? ???刪除表;
drop index_name;? ???刪除索引;
-------------------------------------------查詢,插入,刪除等操作數(shù)據(jù)庫(kù)的語(yǔ)句記得不要漏了;----------------------------------------
#?sqlite3 stu_db
sqlite3 stu_db
SQLite version 3.6.22
Enter??".help" for instructions
Enter SQL statements terminated with a??";"
sqlite>?.schema
.schema
CREATE TABLE??android_metadata (locale TEXT);
CREATE TABLE stu_table(id int,sname varchar(20),sage int,ssex??varchar(10));??--->創(chuàng)建的表
sqlite> select * from stu_table;
select * from??stu_table;
1|xiaoming|21|male
sqlite>
插入數(shù)據(jù)
sqlite> insert into stu_table??values(2,'xiaohong',20,'female');
插入的數(shù)據(jù)記得要和表中的屬性一一對(duì)應(yīng)
insert into stu_table??values(2,'xiaohong',20,'female');
sqlite> select * from??stu_table;
select * from??stu_table;
1|xiaoming|21|male
2|xiaohong|20|female? ?--------------> 插入的數(shù)據(jù)
sqlite>
當(dāng)點(diǎn)擊修改數(shù)據(jù)的按鈕時(shí)候
sqlite> select * from stu_table;
select * from??stu_table;
1|xiaoming|23|male??-------------->年齡被修改為23
2|xiaohong|20|female
sqlite>
當(dāng)點(diǎn)擊刪除數(shù)據(jù)的按鈕
sqlite> select * from stu_table;
select * from??stu_table;
1|xiaoming|23|male? ?? ???id=2的數(shù)據(jù)已經(jīng)被刪除
總之,我們可以在代碼中執(zhí)行數(shù)據(jù)庫(kù)的增刪改查,也可以在adb命令行下實(shí)現(xiàn)。不過因?yàn)镾QLite沒有客戶端,不能直接的查看數(shù)據(jù)庫(kù)變化后的信息,所以常用adb命令行查看數(shù)據(jù)庫(kù)改變后的信息。
一.SQLite的介紹
1.SQLite簡(jiǎn)介
SQLite是一款輕型的數(shù)據(jù)庫(kù),是遵守ACID的關(guān)聯(lián)式數(shù)據(jù)庫(kù)管理系統(tǒng),它的設(shè)計(jì)目標(biāo)是嵌入??式的,而且目前已經(jīng)在很多嵌入式產(chǎn)品中使用了它,它占用資源非常的低,在嵌入式設(shè)備中,可能只需要幾百K的內(nèi)存就夠了。它能夠支持 Windows/Linux/Unix等等主流的操作系統(tǒng),同時(shí)能夠跟很多程序語(yǔ)言相結(jié)合,比如Tcl、PHP、Java、C++、.Net等,還有ODBC接口,同樣比起 Mysql、PostgreSQL這兩款開源世界著名的數(shù)據(jù)庫(kù)管理系統(tǒng)來(lái)講,它的處理速度比他們都快。
2.SQLite的特點(diǎn):
- 輕量級(jí)
SQLite和C/S模式的數(shù)據(jù)庫(kù)軟件不同,它是進(jìn)程內(nèi)的數(shù)據(jù)庫(kù)引擎,因此不存在數(shù)據(jù)庫(kù)的客戶端和服務(wù)器。使用SQLite一般只需要帶上它的一個(gè)動(dòng)態(tài)??庫(kù),就可以享受它的全部功能。而且那個(gè)動(dòng)態(tài)庫(kù)的尺寸也挺小,以版本3.6.11為例,Windows下487KB、Linux下347KB。
- 不需要"安裝"
SQLite的核心引擎本身不依賴第三方的軟件,使用它也不需要"安裝"。有點(diǎn)類似那種綠色軟件。
- 單一文件??
數(shù)據(jù)庫(kù)中所有的信息(比如表、視圖等)都包含在一個(gè)文件內(nèi)。這個(gè)文件可以自由復(fù)制到其它目錄或其它機(jī)器上。
- 跨平臺(tái)/可移植性
除了主流操作系統(tǒng) windows,linux之后,SQLite還支持其它一些不常用的操作系統(tǒng)。
- 弱類型的字段
同一列中的數(shù)據(jù)可以是不同類型
- 開源
這個(gè)相信大家都懂的!!!!!!!!!!!!
3.SQLite數(shù)據(jù)類型
一般數(shù)據(jù)采用的固定的靜態(tài)數(shù)據(jù)類型,而SQLite采用的是動(dòng)態(tài)數(shù)據(jù)類型,會(huì)根據(jù)存入值自動(dòng)判斷。SQLite具有以下五種常用的數(shù)據(jù)類型:
NULL: 這個(gè)值為空值
VARCHAR(n):長(zhǎng)度不固定且其最大長(zhǎng)度為 n 的字串,n不能超過 4000。
CHAR(n):長(zhǎng)度固定為n的字串,n不能超過 254。
INTEGER: 值被標(biāo)識(shí)為整數(shù),依據(jù)值的大小可以依次被存儲(chǔ)為1,2,3,4,5,6,7,8.
REAL: 所有值都是浮動(dòng)的數(shù)值,被存儲(chǔ)為8字節(jié)的IEEE浮動(dòng)標(biāo)記序號(hào).
TEXT: 值為文本字符串,使用數(shù)據(jù)庫(kù)編碼存儲(chǔ)(TUTF-8, UTF-16BE or UTF-16-LE).
BLOB: 值是BLOB數(shù)據(jù)塊,以輸入的數(shù)據(jù)格式進(jìn)行存儲(chǔ)。如何輸入就如何存儲(chǔ),不改??變格式。
DATA :包含了 年份、月份、日期。
TIME: 包含了 小時(shí)、分鐘、秒。
相信學(xué)過數(shù)據(jù)庫(kù)的童鞋對(duì)這些數(shù)據(jù)類型都不陌生的!!!!!!!!!!
二.SQLiteDatabase的介紹
Android提供了創(chuàng)建和是用SQLite數(shù)據(jù)庫(kù)的API。SQLiteDatabase代表一個(gè)數(shù)據(jù)庫(kù)對(duì)象,提供了操作數(shù)據(jù)庫(kù)的一些方法。在Android的SDK目錄下有sqlite3工具,我們可以利用它創(chuàng)建數(shù)據(jù)庫(kù)、創(chuàng)建表和執(zhí)行一些SQL語(yǔ)句。下面是SQLiteDatabase的常用方法。?
SQLiteDatabase的常用方法?
Google公司命名這些方法的名稱都是非常形象的。例如openOrCreateDatabase,我們從字面英文含義就能看出這是個(gè)打開或創(chuàng)建數(shù)據(jù)庫(kù)的方法。
1、打開或者創(chuàng)建數(shù)據(jù)庫(kù)
在Android 中使用SQLiteDatabase的靜態(tài)方法openOrCreateDatabase(String??path,SQLiteDatabae.CursorFactory??factory)打開或者創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)。它會(huì)自動(dòng)去檢測(cè)是否存在這個(gè)數(shù)據(jù)庫(kù),如果存在則打開,不存在則創(chuàng)建一個(gè)數(shù)據(jù)庫(kù);創(chuàng)建成功則返回一個(gè)SQLiteDatabase對(duì)象,否則拋出異常FileNotFoundException。
下面是創(chuàng)建名為“stu.db”數(shù)據(jù)庫(kù)的代碼:
openOrCreateDatabase(String??path,SQLiteDatabae.CursorFactory??factory)
參數(shù)1??數(shù)據(jù)庫(kù)創(chuàng)建的路徑
參數(shù)2??一般設(shè)置為null就可以了
[sql] view plain copy print?
2、創(chuàng)建表
創(chuàng)建一張表的步驟很簡(jiǎn)單:
- 編寫創(chuàng)建表的SQL語(yǔ)句
- 調(diào)用SQLiteDatabase的execSQL()方法來(lái)執(zhí)行SQL語(yǔ)句
下面的代碼創(chuàng)建了一張用戶表,屬性列為:id(主鍵并且自動(dòng)增加)、sname(學(xué)生姓名)、snumber(學(xué)號(hào))
[sql] view plain copy print?
3、插入數(shù)據(jù)
插入數(shù)據(jù)有兩種方法:
①SQLiteDatabase的insert(String table,String nullColumnHack,ContentValues??values)方法,
??參數(shù)1??表名稱,
??參數(shù)2??空列的默認(rèn)值
??參數(shù)3??ContentValues類型的一個(gè)封裝了列名稱和列值的Map;
②編寫插入數(shù)據(jù)的SQL語(yǔ)句,直接調(diào)用SQLiteDatabase的execSQL()方法來(lái)執(zhí)行
第一種方法的代碼:
[sql] view plain copy print?
第二種方法的代碼:
[sql] view plain copy print?
4、刪除數(shù)據(jù)
刪除數(shù)據(jù)也有兩種方法:
①調(diào)用SQLiteDatabase的delete(String table,String whereClause,String[]??whereArgs)方法
參數(shù)1??表名稱?
參數(shù)2??刪除條件
參數(shù)3??刪除條件值數(shù)組
②編寫刪除SQL語(yǔ)句,調(diào)用SQLiteDatabase的execSQL()方法來(lái)執(zhí)行刪除。
第一種方法的代碼:
[sql] view plain copy print?
第二種方法的代碼:
[sql] view plain copy print?
5、修改數(shù)據(jù)
修改數(shù)據(jù)有兩種方法:
①調(diào)用SQLiteDatabase的update(String table,ContentValues values,String??whereClause, String[]??whereArgs)方法
參數(shù)1??表名稱
參數(shù)2??跟行列ContentValues類型的鍵值對(duì)Key-Value
參數(shù)3??更新條件(where字句)
參數(shù)4??更新條件數(shù)組
②編寫更新的SQL語(yǔ)句,調(diào)用SQLiteDatabase的execSQL執(zhí)行更新。
第一種方法的代碼:
[sql] view plain copy print?
第二種方法的代碼:
[sql] view plain copy print?
6、查詢數(shù)據(jù)
在Android中查詢數(shù)據(jù)是通過Cursor類來(lái)實(shí)現(xiàn)的,當(dāng)我們使用SQLiteDatabase.query()方法時(shí),會(huì)得到一個(gè)Cursor對(duì)象,Cursor指向的就是每一條數(shù)據(jù)。它提供了很多有關(guān)查詢的方法,具體方法如下:
public??Cursor query(String table,String[] columns,String selection,String[]??selectionArgs,String groupBy,String having,String orderBy,String limit);
各個(gè)參數(shù)的意義說(shuō)明:
參數(shù)table:表名稱
參數(shù)columns:列名稱數(shù)組
參數(shù)selection:條件字句,相當(dāng)于where
參數(shù)selectionArgs:條件字句,參數(shù)數(shù)組
參數(shù)groupBy:分組列
參數(shù)having:分組條件
參數(shù)orderBy:排序列
參數(shù)limit:分頁(yè)查詢限制
參數(shù)Cursor:返回值,相當(dāng)于結(jié)果集ResultSet
Cursor是一個(gè)游標(biāo)接口,提供了遍歷查詢結(jié)果的方法,如移動(dòng)指針方法move(),獲得列值方法getString()等.
Cursor游標(biāo)常用方法
下面就是用Cursor來(lái)查詢數(shù)據(jù)庫(kù)中的數(shù)據(jù),具體代碼如下:
[sql] view plain copy print?
7、刪除指定表
編寫插入數(shù)據(jù)的SQL語(yǔ)句,直接調(diào)用SQLiteDatabase的execSQL()方法來(lái)執(zhí)行
[java] view plain copy print?
三.? SQLiteOpenHelper
該類是SQLiteDatabase一個(gè)輔助類。這個(gè)類主要生成一??個(gè)數(shù)據(jù)庫(kù),并對(duì)數(shù)據(jù)庫(kù)的版本進(jìn)行管理。當(dāng)在程序當(dāng)中調(diào)用這個(gè)類的方法getWritableDatabase()或者 getReadableDatabase()方法的時(shí)候,如果當(dāng)時(shí)沒有數(shù)據(jù),那么Android系統(tǒng)就會(huì)自動(dòng)生成一個(gè)數(shù)據(jù)庫(kù)。 SQLiteOpenHelper 是一個(gè)抽象類,我們通常需要繼承它,并且實(shí)現(xiàn)里面的3個(gè)函數(shù):
1.onCreate(SQLiteDatabase)
在數(shù)據(jù)庫(kù)第一次生成的時(shí)候會(huì)調(diào)用這個(gè)方法,也就是說(shuō),只有在創(chuàng)建數(shù)據(jù)庫(kù)的時(shí)候才會(huì)調(diào)用,當(dāng)然也有一些其它的情況,一般我們?cè)谶@個(gè)方法里邊生成數(shù)據(jù)庫(kù)表。
2.??onUpgrade(SQLiteDatabase,int,int)?
當(dāng)數(shù)據(jù)庫(kù)需要升級(jí)的時(shí)候,Android系統(tǒng)會(huì)主動(dòng)的調(diào)用這個(gè)方法。一般我們?cè)谶@個(gè)方法里邊刪除數(shù)據(jù)表,并建立新的數(shù)據(jù)表,當(dāng)然是否還需要做其他的操作,完全取決于應(yīng)用的需求。
3.??onOpen(SQLiteDatabase):
這是當(dāng)打開數(shù)據(jù)庫(kù)時(shí)的回調(diào)函數(shù),一般在程序中不是很常使用。
寫了這么多,改用用實(shí)際例子來(lái)說(shuō)明上面的內(nèi)容了。下面這個(gè)操作數(shù)據(jù)庫(kù)的實(shí)例實(shí)現(xiàn)了創(chuàng)建數(shù)據(jù)庫(kù),創(chuàng)建表以及數(shù)據(jù)庫(kù)的增刪改查的操作。
該實(shí)例有兩個(gè)類:
com.lingdududu.testSQLite 調(diào)試類
com.lingdududu.testSQLiteDb??數(shù)據(jù)庫(kù)輔助類
SQLiteActivity.java
[java] view plain copy print?
StuDBHelper.java [java] view plain copy print?
main.xml
[java] view plain copy print?
程序運(yùn)行的效果圖:
1.在命令行窗口輸入adb??shell回車,就進(jìn)入了Linux命令行,現(xiàn)在就可以使用Linux的命令了。
2.ls回車,顯示所有的東西,其中有個(gè)data。
3.cd data回車,再ls回車,cd??data回車,ls回車后就會(huì)看到很多的com................,那就是系統(tǒng)上的應(yīng)用程序包名,找到你數(shù)據(jù)庫(kù)程序的包名,然后進(jìn)入。
4.進(jìn)去后在查看所有,會(huì)看到有databases,進(jìn)入databases,顯示所有就會(huì)發(fā)現(xiàn)你的數(shù)據(jù)庫(kù)名字,這里使用的是"stu_db"。
5.sqlite3 stu_db回車就進(jìn)入了你的數(shù)據(jù)庫(kù)了,然后“.schema”就會(huì)看到該應(yīng)用程序的所有表及建表語(yǔ)句。
6.之后就可以使用標(biāo)準(zhǔn)的SQL語(yǔ)句查看剛才生成的數(shù)據(jù)庫(kù)及對(duì)數(shù)據(jù)執(zhí)行增刪改查了。
注:ls,cd等命令都是linux的基本命令,不了解的同學(xué)可以看看有關(guān)這方面的資料。
下面介紹幾個(gè)在SQLite中常用到的adb命令:
查看
.database 顯示數(shù)據(jù)庫(kù)信息;
.tables 顯示表名稱;
.schema 命令可以查看創(chuàng)建數(shù)據(jù)表時(shí)的SQL命令;
.schema table_name 查看創(chuàng)建表table_name時(shí)的SQL的命令;
插入記錄
insert into table_name values (field1, field2, field3...);
查詢
select * from table_name;查看table_name表中所有記錄;
select * from table_name where field1='xxxxx'; 查詢符合指定條件的記錄;
刪除
drop table_name;? ???刪除表;
drop index_name;? ???刪除索引;
-------------------------------------------查詢,插入,刪除等操作數(shù)據(jù)庫(kù)的語(yǔ)句記得不要漏了;----------------------------------------
#?sqlite3 stu_db
sqlite3 stu_db
SQLite version 3.6.22
Enter??".help" for instructions
Enter SQL statements terminated with a??";"
sqlite>?.schema
.schema
CREATE TABLE??android_metadata (locale TEXT);
CREATE TABLE stu_table(id int,sname varchar(20),sage int,ssex??varchar(10));??--->創(chuàng)建的表
sqlite> select * from stu_table;
select * from??stu_table;
1|xiaoming|21|male
sqlite>
插入數(shù)據(jù)
sqlite> insert into stu_table??values(2,'xiaohong',20,'female');
插入的數(shù)據(jù)記得要和表中的屬性一一對(duì)應(yīng)
insert into stu_table??values(2,'xiaohong',20,'female');
sqlite> select * from??stu_table;
select * from??stu_table;
1|xiaoming|21|male
2|xiaohong|20|female? ?--------------> 插入的數(shù)據(jù)
sqlite>
當(dāng)點(diǎn)擊修改數(shù)據(jù)的按鈕時(shí)候
sqlite> select * from stu_table;
select * from??stu_table;
1|xiaoming|23|male??-------------->年齡被修改為23
2|xiaohong|20|female
sqlite>
當(dāng)點(diǎn)擊刪除數(shù)據(jù)的按鈕
sqlite> select * from stu_table;
select * from??stu_table;
1|xiaoming|23|male? ?? ???id=2的數(shù)據(jù)已經(jīng)被刪除
總之,我們可以在代碼中執(zhí)行數(shù)據(jù)庫(kù)的增刪改查,也可以在adb命令行下實(shí)現(xiàn)。不過因?yàn)镾QLite沒有客戶端,不能直接的查看數(shù)據(jù)庫(kù)變化后的信息,所以常用adb命令行查看數(shù)據(jù)庫(kù)改變后的信息。
總結(jié)
以上是生活随笔為你收集整理的Android 操作SQLite基本用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android listview显示数据
- 下一篇: ANDROID开发之SQLite详解