Android開發(fā)之SQLite的使用方法
SQLite是一種輕量級的小型數(shù)據(jù)庫,雖然比較小,但是功能相對比較完善,一些常見的數(shù)據(jù)庫基本功能也具有,在現(xiàn)在的嵌入式系統(tǒng)中使用該數(shù)據(jù)庫的比較多,因為它占用系統(tǒng)資源很少。Android系統(tǒng)中也不例外,也是采用SQLite,本節(jié)中就學(xué)習(xí)下在andorid中怎樣使用該數(shù)據(jù)庫來存放數(shù)據(jù),并且對SQLite完成簡單的新建,更新,查詢,刪除等操作。
實驗說明:
Android中使用SQLite數(shù)據(jù)庫時,需要用adb來輔助調(diào)試,如果想在windows下的cmd命令行中使用adb,必須先配置環(huán)境變量,我這里是配的用戶環(huán)境變量path: C:\Program Files\android-sdk\platform-tools;
配置好環(huán)境變量后,在cmd中輸入adb shell進入linux shell環(huán)境前,需要把android模擬器打開(本文都是針對模擬器而言,并非真機)。如果啟動好了模擬器,且輸入adb shell命令后出現(xiàn)error: device not found錯誤提示,則可以殺掉adb進程然后重新啟動該進程,在cmd中輸入如下命令:
adb kill-server
adb start-server
如果要在android中使用SQLite,一般需要重新寫一個類,而該類繼承一個android提供訪問該數(shù)據(jù)庫的助手類SQLiteOpenHelper。 本次實驗中我們在src文件夾下新建一個類,類名為DatabaseHelper,該類繼承SQLiteOpenHelper類,而繼承SQLiteOpenHelper類的類必須有自己的構(gòu)造函數(shù),因此按照mars老師的代碼,在程序中寫了3個構(gòu)造函數(shù),這3個函數(shù)的區(qū)別是參數(shù)個數(shù)的不同,參數(shù)個數(shù)少的函數(shù)是因為另外一些參數(shù)已經(jīng)被固定了,且它們都是顯示或隱式調(diào)用了父類的構(gòu)造函數(shù),下面是SQLiteOpenHelper的構(gòu)造函數(shù)。
public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
第一個參數(shù)為該類本身;第二個參數(shù)為數(shù)據(jù)庫的名字;第3個參數(shù)是用來設(shè)置游標對象的,這里一般設(shè)置為null;參數(shù)四是數(shù)據(jù)庫的版本號。
public void execSQL (String sql)
該函數(shù)是類SQLiteDatabase中的一個函數(shù),其功能是執(zhí)行一條SQL語句命令,這條語句的內(nèi)容就是該函數(shù)的參數(shù)。因此參數(shù)sql需要符合SQL語法規(guī)則。
public ContentValues ()
ContentValues是用于數(shù)據(jù)庫中存放數(shù)據(jù)的類,也是采用的鍵值對來存放數(shù)據(jù)的,有點類似content和bundle等。該構(gòu)造函數(shù)是建立一個默認大小的空的數(shù)據(jù)集。
實驗步驟和結(jié)果:
界面設(shè)計:
該程序的界面設(shè)計比較簡單,因為需要完成6中功能,所以在界面中只有6個按鈕,每個按鈕對應(yīng)一種功能,這些按鈕的功能依次為新建數(shù)據(jù)庫,更新數(shù)據(jù)庫的版本,向數(shù)據(jù)庫中插入記錄,更新數(shù)據(jù)庫中的記錄,查詢數(shù)據(jù)庫中的記錄,刪除數(shù)據(jù)庫中的記錄。其界面效果如下:

建立數(shù)據(jù)庫:
運行模擬器,單擊create sqlite database按鈕,創(chuàng)建一個名為”tornadomeet”的數(shù)據(jù)庫,版本號為1,這時候可以看到程序終端顯示”create a sqlite database”字樣。在cmd中輸入如下命令:adb shell;cd data; cd data; ls; cd com.example.sqlite_test; ls;命令,其中com.example.splite是本程序的包名,輸入最后ls命令后可以看到有2個文件夾cache, lib。然后在單擊后繼續(xù)ls命令查看,多了一個database文件夾。使用命令cd database進入該文件夾后ls發(fā)現(xiàn)里面有數(shù)據(jù)庫,截圖如下:
繼續(xù)在cmd命令行輸入sqlite3 tornadomeet.db;(其中tornadomeet為單擊創(chuàng)建數(shù)據(jù)庫后新建立的數(shù)據(jù)庫名稱。)輸入 .schema;(注意前面有個點)顯示如下:
可以看到這個數(shù)據(jù)庫有2個表,且可以看到這2個表建立的sqlite語句,其中第一個表示android自帶的,第二個表示我們新建的。
繼續(xù)輸入命令select * from user1;(注意此時因為是真正的sqlite語句,所以在命令行中要以分號結(jié)束)查詢自己建立的數(shù)據(jù)庫中的表user1,發(fā)現(xiàn)里面什么內(nèi)容都沒有。
更新數(shù)據(jù)庫版本:
在該步驟中,當(dāng)按下update sqlite database按鈕后,我們在監(jiān)聽器函數(shù)中新建了一個數(shù)據(jù)庫,數(shù)據(jù)庫名為”tornadomeet”不變,只是將其版本號設(shè)置為了2。因為版本號變了,所以程序會自動去調(diào)用SQLiteOpenHelper的子類的onUpgrade方法,該程序中只是在該方法中輸出一條語句而已。
數(shù)據(jù)庫插入:
單擊模擬器的insert按鈕,進入sqlite對應(yīng)的數(shù)據(jù)庫后,輸入select * from user1;命令后(帶分號)可以看到有如下顯示:1 | tornado,說明我們的數(shù)據(jù)庫成功插入了一條記錄,該記錄的內(nèi)容是在程序中靜態(tài)給的。
更新數(shù)據(jù)庫內(nèi)容:
更新操作相當(dāng)于執(zhí)行SQL的UPDATE語句,語法為:UPDATE table_name SET ###col =###1 WHERE ###col = ###2
本程序?qū)⑸厦娌迦氲囊粭l記錄的name名有tornado改為了tornadomeet,所以當(dāng)按下update按鈕后,重新在cmd使用select * from user1可以看出那條記錄變成了1 | tornadomeet了。
查詢操作:
查詢操作使用SQLiteOpenHelper中的query方法,這里的查詢是指按照列名name去查詢的,實際查詢過程中是有一個游標,開始時指向表頭,后面一次一次往下移,直到滿足查詢的條件時就輸出,然后終止查詢。
刪除操作:
刪除操作與前面的類似,使用SQLiteOpenHelper中的delete方法,按照指定列名為某一值,然后刪除那條記錄即可。
下面是在依次單擊新建一個SQLite數(shù)據(jù)庫按鈕,插入記錄按鈕,更新記錄按鈕,再次插入記錄按鈕,刪除按鈕后adb后臺調(diào)試的結(jié)果:

由此可以看出,刪除操作也是成功的。
實驗主要部分代碼及注釋(附錄有實驗工程code下載鏈接): import android.app.Activity; public class MainActivity extends Activity { private Button create_database = null; public class CreateDatabaseOnClickListener implements OnClickListener{ public void onClick(View v) { public void onClick(View v) { public void onClick(View v) { public void onClick(View v) { public void onClick(View v) { public void onClick(View v) { import android.content.Context; public class DatabaseHelper extends SQLiteOpenHelper { private static final int VERSON = 1;//默認的數(shù)據(jù)庫版本 @Override } <Button <Button <Button <Button
package com.example.sqlite_test;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
private Button update_database = null;
private Button insert = null;
private Button update = null;
private Button query = null;
private Button delete = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
create_database = (Button)findViewById(R.id.create_database);
create_database.setOnClickListener(new CreateDatabaseOnClickListener());
update_database = (Button)findViewById(R.id.update_database);
update_database.setOnClickListener(new UpdateDatabaseOnClickListener());
insert = (Button)findViewById(R.id.insert);
insert.setOnClickListener(new InsertOnClickListener());
update = (Button)findViewById(R.id.update);
update.setOnClickListener(new UpdateOnClickListener());
query = (Button)findViewById(R.id.query);
query.setOnClickListener(new QueryOnClickListener());
delete = (Button)findViewById(R.id.delete);
delete.setOnClickListener(new DeleteOnClickListener());
}
// TODO Auto-generated method stub
//創(chuàng)建一個DatabaseHelper類的對象,該類是單獨一個java文件,這里采用2個參數(shù)的構(gòu)造函數(shù),建立的數(shù)據(jù)
//庫的名字為tornadomeet.db
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
//只有調(diào)用getReadableDatabase()或者getWriteableDatabase()函數(shù)后才能返回一個SQLiteDatabase對象
SQLiteDatabase db = database_helper.getReadableDatabase();
}
}
public class UpdateDatabaseOnClickListener implements OnClickListener{
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db", 2);
SQLiteDatabase db = database_helper.getReadableDatabase();
}
}
public class InsertOnClickListener implements OnClickListener{
// 生成contentvallues對象,該對象用來存數(shù)據(jù)的
ContentValues values = new ContentValues();
values.put("id", 1);//注意值的類型要匹配
values.put("name", "tornado");
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();//這里是獲得可寫的數(shù)據(jù)庫
db.insert("user1", null, values);
}
}
public class UpdateOnClickListener implements OnClickListener{
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "tornadomeet");
//參數(shù)1為表名,參數(shù)2為更新后的值,參數(shù)3表示滿足條件的列名稱,參數(shù)4為該列名下的值
db.update("user1", values, "id=?", new String[]{"1"});
}
}
public class QueryOnClickListener implements OnClickListener{
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();
//查詢的語法,參數(shù)1為表名;參數(shù)2為表中的列名;參數(shù)3為要查詢的列名;參數(shù)時為對應(yīng)列的值;該函數(shù)返回的是一個游標
Cursor cursor = db.query("user1", new String[]{"id", "name"}, "id=?", new String[]{"1"}, null, null, null);
//遍歷每一個記錄
while(cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));//返回列名為name的值
System.out.println("query---->" + name);
}
}
}
public class DeleteOnClickListener implements OnClickListener{
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();
//直接刪除名為tornadomeet對應(yīng)的那條記錄
db.delete("user1", "name=?" ,new String[]{"tornadomeet"});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
DatabaseHelper.java:
package com.example.sqlite_test;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
//繼承SQLiteOpenHelper類的類必須有自己的構(gòu)造函數(shù)
//該構(gòu)造函數(shù)4個參數(shù),直接調(diào)用父類的構(gòu)造函數(shù)。其中第一個參數(shù)為該類本身;第二個參數(shù)為數(shù)據(jù)庫的名字;
//第3個參數(shù)是用來設(shè)置游標對象的,這里一般設(shè)置為null;參數(shù)四是數(shù)據(jù)庫的版本號。
public DatabaseHelper(Context context, String name, CursorFactory factory, int verson){
super(context, name, factory, verson);
}
//該構(gòu)造函數(shù)有3個參數(shù),因為它把上面函數(shù)的第3個參數(shù)固定為null了
public DatabaseHelper(Context context, String name, int verson){
this(context, name, null, verson);
}
//該構(gòu)造函數(shù)只有2個參數(shù),在上面函數(shù) 的基礎(chǔ)山將版本號固定了
public DatabaseHelper(Context context, String name){
this(context, name, VERSON);
}
//該函數(shù)在數(shù)據(jù)庫第一次被建立時調(diào)用
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
System.out.println("create a sqlite database");
//execSQL()為執(zhí)行參數(shù)里面的SQL語句,因此參數(shù)中的語句需要符合SQL語法,這里是創(chuàng)建一個表
arg0.execSQL("create table user1(id int, name varchar(20))");
}
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
System.out.println("update a sqlite database");
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/create_database"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/create_database"
/>
android:id="@+id/update_database"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/create_database"
android:layout_alignParentLeft="true"
android:text="@string/update_database" />
android:id="@+id/insert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/update_database"
android:layout_alignParentLeft="true"
android:text="@string/insert" />
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/insert"
android:layout_alignParentLeft="true"
android:text="@string/update" />
<Button
android:id="@+id/query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@id/update"
android:text="@string/query"
/>
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@id/query"
android:text="@string/delete"
/>
</RelativeLayout>
總結(jié): 通過本次實驗,對SQLite在andorid中的使用流程有了初步的了解。
附錄:
- Android SQLite數(shù)據(jù)庫增刪改查操作的使用詳解
- Android使用SQLite數(shù)據(jù)庫的簡單實例
- android創(chuàng)建數(shù)據(jù)庫(SQLite)保存圖片示例
- Android SQLite數(shù)據(jù)庫基本操作方法
- Android登錄注冊功能 數(shù)據(jù)庫SQLite驗證
- android中sqlite的按條件查找的小例子
- Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實現(xiàn)
- 深入Android SQLite 事務(wù)處理詳解
- Android--SQLite(增,刪,改,查)操作實例代碼
- Android?Studio中使用SQLite的操作方法
相關(guān)文章
Android開發(fā)獲取系統(tǒng)中已安裝程序信息的方法
這篇文章主要介紹了Android開發(fā)獲取系統(tǒng)中已安裝程序信息的方法,可實現(xiàn)Android針對系統(tǒng)中已安裝程序名稱、路徑、大小、圖標、是否為系統(tǒng)app等信息的獲取功能,需要的朋友可以參考下2017-12-12
Android提高之SQLite分頁表格實現(xiàn)方法
這篇文章主要介紹了Android提高之SQLite分頁表格實現(xiàn)方法,在項目開發(fā)中有很高的實用價值,需要的朋友可以參考下2014-08-08
Android實例代碼理解設(shè)計模式SOLID六大原則
程序設(shè)計領(lǐng)域, SOLID (單一功能、開閉原則、里氏替換、接口隔離以及依賴反轉(zhuǎn))是由羅伯特·C·馬丁在21世紀早期 引入的記憶術(shù)首字母縮略字,指代了面向?qū)ο缶幊毯兔嫦驅(qū)ο笤O(shè)計的基本原則2021-10-10
android使用viewpager計算偏移量實現(xiàn)選項卡功能
這篇文章主要為大家詳細介紹了android使用viewpager計算偏移量實現(xiàn)選項卡功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
Android實現(xiàn)旋轉(zhuǎn)動畫的兩種方式案例詳解
這篇文章主要介紹了Android實現(xiàn)旋轉(zhuǎn)動畫的兩種方式,需要的朋友可以參考下2021-08-08
淺析KJFrameForAndroid框架如何高效加載Bitmap
Bitmap是Android系統(tǒng)中的圖像處理的最重要類之一。用它可以獲取圖像文件信息,進行圖像剪切、旋轉(zhuǎn)、縮放等操作,并可以指定格式保存圖像文件。本文主要是從KJFrameForAndroid框架中分析高效加載Bitmap的方法2014-07-07
Android中利用ViewHolder優(yōu)化自定義Adapter的寫法(必看)
下面小編就為大家?guī)硪黄狝ndroid中利用ViewHolder優(yōu)化自定義Adapter的寫法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04

