Android使用SQLite數(shù)據(jù)庫的簡單實(shí)例
先畫個(gè)圖,了解下Android下數(shù)據(jù)庫操作的簡單流程:
1.首先,寫一個(gè)自己的數(shù)據(jù)庫操作幫助類,這個(gè)類繼承自Android自帶的SQLiteOpenHelper.
2.在自己的DAO層借助自己的Helper寫數(shù)據(jù)庫操作的一些方法
3.Activity調(diào)用DAO層的數(shù)據(jù)庫操作方法進(jìn)行操作
下面例子是:
1.Helper
package cn.learn.db.util;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBHelper extends SQLiteOpenHelper {
private final static String DB_NAME ="test.db";//數(shù)據(jù)庫名
private final static int VERSION = 1;//版本號
//自帶的構(gòu)造方法
public DBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
//為了每次構(gòu)造時(shí)不用傳入dbName和版本號,自己得新定義一個(gè)構(gòu)造方法
public DBHelper(Context cxt){
this(cxt, DB_NAME, null, VERSION);//調(diào)用上面的構(gòu)造方法
}
//版本變更時(shí)
public DBHelper(Context cxt,int version) {
this(cxt,DB_NAME,null,version);
}
//當(dāng)數(shù)據(jù)庫創(chuàng)建的時(shí)候調(diào)用
public void onCreate(SQLiteDatabase db) {
String sql = "create table student(" +
"id integer primary key autoincrement," +
"name varchar(20)," +
"age int)";
db.execSQL(sql);
}
//版本更新時(shí)調(diào)用
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "update student ....";//自己的Update操作
db.execSQL(sql);
}
}
2.寫DAO層
package cn.learn.db.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import cn.learn.db.dao.domain.Student;
import cn.learn.db.util.DBHelper;
public class StudentDao {
DBHelper helper = null;
public StudentDao(Context cxt) {
helper = new DBHelper(cxt);
}
/**
* 當(dāng)Activity中調(diào)用此構(gòu)造方法,傳入一個(gè)版本號時(shí),系統(tǒng)會在下一次調(diào)用數(shù)據(jù)庫時(shí)調(diào)用Helper中的onUpgrade()方法進(jìn)行更新
* @param cxt
* @param version
*/
public StudentDao(Context cxt, int version) {
helper = new DBHelper(cxt, version);
}
// 插入操作
public void insertData(Student stu) {
String sql = "insert into student (name,age)values(?,?)";
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL(sql, new Object[] { stu.name, stu.age });
}
// 其它操作
}
完成這些,其它操作就簡單了....
另外,數(shù)據(jù)庫文件放在這個(gè)目錄
- android studio使用SQLiteOpenHelper()建立數(shù)據(jù)庫的方法
- Android Studio 通過登錄功能介紹SQLite數(shù)據(jù)庫的使用流程
- Android使用SQLite數(shù)據(jù)庫的示例
- Android創(chuàng)建和使用數(shù)據(jù)庫SQLIte
- 實(shí)例講解Android App使用自帶的SQLite數(shù)據(jù)庫的基本方法
- Android App使用SQLite數(shù)據(jù)庫的一些要點(diǎn)總結(jié)
- Android中使用SQLite3 命令行查看內(nèi)嵌數(shù)據(jù)庫的方法
- Android SQLite數(shù)據(jù)庫增刪改查操作的使用詳解
- SQLite數(shù)據(jù)庫在Android中的使用小結(jié)
相關(guān)文章
Android實(shí)現(xiàn)圓形純數(shù)字按鈕
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓形純數(shù)字按鈕,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02使用Eclipse配置android開發(fā)環(huán)境教程
這篇文章主要介紹了使用Eclipse配置android開發(fā)環(huán)境教程,本文講解了下載需要用到的工具、下載完需要的工具之后開始安裝、讓Ecplise自動安裝Android開發(fā)插件(ADT- plugin)、配置Andiord SDK路徑、測試開發(fā)一個(gè)Android項(xiàng)目等內(nèi)容,需要的朋友可以參考下2015-04-04Android實(shí)現(xiàn)系統(tǒng)打印功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)系統(tǒng)打印功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android動態(tài)更換應(yīng)用圖標(biāo)詳情
這篇文章主要介紹了Android動態(tài)更換應(yīng)用圖標(biāo)詳情,文章圍繞主題展開詳細(xì)的介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07Android編程實(shí)現(xiàn)的EditText彈出打開和關(guān)閉工具類
這篇文章主要介紹了Android編程實(shí)現(xiàn)的EditText彈出打開和關(guān)閉工具類,涉及Android輸入框EditText彈出打開和關(guān)閉功能簡單實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02Android中Glide加載圖片并實(shí)現(xiàn)圖片緩存
本篇文章主要介紹了Android中Glide加載圖片并實(shí)現(xiàn)圖片緩存,這里和大家簡單的分享一下Glide的使用方法以及緩存 ,有興趣的可以了解一下。2017-03-03Android開發(fā)筆記 TableLayout常用的屬性介紹
今天看了安卓簡單控件的布局方式,大概有絕對、相對、表格、線性、幀式布局五種方式,表格布局里面的一些屬性相對來說比較復(fù)雜,下面主要談?wù)劚砀穹绞讲季值囊恍傩?/div> 2012-11-11Android編程之控件可拖動的實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程之控件可拖動的實(shí)現(xiàn)方法,實(shí)例分析了Android響應(yīng)點(diǎn)擊及觸摸事件的相關(guān)技巧,需要的朋友可以參考下2016-02-02Android實(shí)現(xiàn)圖片在屏幕內(nèi)縮放和移動效果
這篇文章主要為大家詳細(xì)介紹了Android控制圖片在屏幕內(nèi)縮放和移動效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02房卡麻將分析系列 "牌局回放" 之 數(shù)據(jù)設(shè)計(jì)詳解及實(shí)例
這篇文章主要介紹了房卡麻將分析系列 "牌局回放" 之 數(shù)據(jù)設(shè)計(jì)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03最新評論