Android數(shù)據(jù)持久化之SQLite數(shù)據(jù)庫(kù)用法分析
本文實(shí)例講述了Android數(shù)據(jù)持久化之SQLite數(shù)據(jù)庫(kù)用法。分享給大家供大家參考,具體如下:
這一節(jié)我將總結(jié)一下android中的另一種數(shù)據(jù)存儲(chǔ)——SQLite 的相關(guān)知識(shí)點(diǎn)
SQLite數(shù)據(jù)庫(kù)是android系統(tǒng)自帶的,主要用到的類(lèi)包括SQLiteOpenHelper和SQLiteDatabase。
1、SQLiteOpenHelper:創(chuàng)建數(shù)據(jù)庫(kù)和數(shù)據(jù)庫(kù)版本管理的輔助類(lèi),該類(lèi)是一個(gè)抽象類(lèi),所以我們一般都有一個(gè)子類(lèi)SQLiteOpenHelper,需要繼承實(shí)現(xiàn)的方法主要有onCreate()、onUpgrade()、getWritableDatabase()等。getWritableDatabase()方法返回的是SQLiteDatabase對(duì)象實(shí)例,如果數(shù)據(jù)庫(kù)尚未創(chuàng)建,則會(huì)自動(dòng)調(diào)用onCreate()方法來(lái)創(chuàng)建數(shù)據(jù)庫(kù),所以一些建表和數(shù)據(jù)初始化操作,應(yīng)該放在onCreate()方法里 。
2、SQLiteDatabase:操作SQLite數(shù)據(jù)庫(kù)的類(lèi),可以進(jìn)行SQL語(yǔ)句,對(duì)數(shù)據(jù)庫(kù)進(jìn)行增、刪、改、查的操作,該對(duì)象已經(jīng)對(duì)基本的數(shù)據(jù)庫(kù)操作進(jìn)行了封裝??梢哉{(diào)用insert()、delete()、executeSQL()等方法,進(jìn)行實(shí)際的數(shù)據(jù)庫(kù)操作 ,這個(gè)類(lèi)相當(dāng)于JDBC中的Connection,也類(lèi)似Hibernate中的Session,或者Spring中的HibernateTemplate;也可以進(jìn)行transaction的控制。很多對(duì)數(shù)據(jù)庫(kù)的操作最終都是通過(guò)SQLiteDatabase實(shí)例來(lái)調(diào)用執(zhí)行的。
注意:數(shù)據(jù)庫(kù)對(duì)于一個(gè)應(yīng)用時(shí)私有的,并且在一個(gè)應(yīng)用當(dāng)中,數(shù)據(jù)庫(kù)的名字也是唯一的。
3、Corsor:游標(biāo)。通過(guò)Cursor可以對(duì)于從數(shù)據(jù)庫(kù)中查詢(xún)出來(lái)的結(jié)果集進(jìn)行隨機(jī)的讀寫(xiě)訪問(wèn)。對(duì)于數(shù)據(jù)庫(kù)的查詢(xún)結(jié)果,一般是由子類(lèi)SQLiteCursor返回的。
特別注意:開(kāi)發(fā)的時(shí)候一般會(huì)對(duì)前面兩個(gè)類(lèi)做一下包裝,比如進(jìn)行簡(jiǎn)單的封裝,使得SQLiteDatabase的查詢(xún)方法不是返回原始的Cursor類(lèi)(Cursor相當(dāng)于JDBC中的ResultSet),而是返回業(yè)務(wù)對(duì)象等等
實(shí)現(xiàn)的代碼如下:
SQLiteOpenHelper類(lèi)的實(shí)現(xiàn):
package com.sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelpler extends SQLiteOpenHelper{
private static final int VERSION = 1;
public DataBaseHelpler(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public DataBaseHelpler(Context context,String name){
this(context,name,VERSION);
}
public DataBaseHelpler(Context context, String name,int version){
this(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
System.out.println("creat database");
db.execSQL("create table student(no int,name verchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
System.out.println("upgrade database");
}
}
SQLite類(lèi)的實(shí)現(xiàn):
package com.sql;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SQLiteActivity extends Activity {
/** Called when the activity is first created. */
private Button button_create,
button_upgreate,
button_insert,
button_up,
button_query,
button_delete;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button_create = (Button) findViewById(R.id.button1);
button_upgreate = (Button) findViewById(R.id.button2);
button_insert = (Button) findViewById(R.id.button3);
button_up = (Button) findViewById(R.id.button4);
button_query = (Button) findViewById(R.id.button5);
button_delete = (Button) findViewById(R.id.button6);
//創(chuàng)建數(shù)據(jù)庫(kù)
button_create.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
DataBaseHelpler dbh = new DataBaseHelpler(SQLiteActivity.this,"tabel_one");
SQLiteDatabase sql = dbh.getReadableDatabase();
}
});
//更新數(shù)據(jù)庫(kù)
button_upgreate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
DataBaseHelpler dbh = new DataBaseHelpler(SQLiteActivity.this,"tabel_one",2);
SQLiteDatabase sql = dbh.getReadableDatabase();
}
});
//向數(shù)據(jù)庫(kù)中的表中插入內(nèi)容
button_insert.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
values.put("no", 123);
values.put("name", "zhangsan");
DataBaseHelpler dbh = new DataBaseHelpler(SQLiteActivity.this,"tabel_one",2);
SQLiteDatabase sql = dbh.getReadableDatabase();
sql.insert("tabel_one", null, values);
}
});
//更新表的內(nèi)容
button_up.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
DataBaseHelpler dbh = new DataBaseHelpler(SQLiteActivity.this,"tabel_one");
SQLiteDatabase sql = dbh.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("name", "wangwu");
sql.update("tabel_one", values, "id=?", new String[]{"1"});
}
});
//查找表的內(nèi)容
button_query.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
//刪除
button_delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
以上就是SQLite基本的應(yīng)用。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android開(kāi)發(fā)之獲取LayoutInflater對(duì)象的方法總結(jié)
這篇文章主要介紹了Android開(kāi)發(fā)之獲取LayoutInflater對(duì)象的方法,結(jié)合實(shí)例形式總結(jié)分析了Android獲取LayoutInflater對(duì)象的常用技巧,需要的朋友可以參考下2016-02-02
Android OpenGL ES 實(shí)現(xiàn)抖音傳送帶特效(原理解析)
這篇文章主要介紹了Android OpenGL ES 實(shí)現(xiàn)抖音傳送帶特效,抖音傳送帶特效推出已經(jīng)很長(zhǎng)一段時(shí)間了,前面也實(shí)現(xiàn)了下,最近把它整理出來(lái)了,如果你有仔細(xì)觀測(cè)傳送帶特效,就會(huì)發(fā)現(xiàn)它的實(shí)現(xiàn)原理其實(shí)很簡(jiǎn)單,需要的朋友可以參考下2022-07-07
Android仿UC底部菜單欄實(shí)現(xiàn)原理與代碼
最近剛看完ViewPager,開(kāi)始我打算用自定義的imgBtn,但是發(fā)現(xiàn)放在pager選項(xiàng)卡中不好排版,所以最好選了GridView,接下來(lái)介紹底部菜單欄實(shí)現(xiàn)2013-01-01
Android實(shí)現(xiàn)完整游戲循環(huán)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)完整游戲循環(huán)的方法,以實(shí)例代碼形式較為詳細(xì)的分析了Android游戲循環(huán)的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android開(kāi)發(fā)之?dāng)?shù)據(jù)的存儲(chǔ)方式詳解
本篇文章主要介紹了Android開(kāi)發(fā)之?dāng)?shù)據(jù)的存儲(chǔ)方式,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。2016-11-11

