在Android平臺(tái)上,集成了一個(gè)嵌入式關(guān)系型數(shù)據(jù)庫(kù)—SQLite。如果你想要開發(fā) Android 應(yīng)用程序,一定需要在 Android 上存儲(chǔ)數(shù)據(jù),使用SQLite 數(shù)據(jù)庫(kù)是一種非常好的選擇。
下面介紹的基本使用:
是一個(gè)抽象類不能直接實(shí)例化,所以新建一個(gè)類DBOpenHelper繼承自SQLiteOpenHelper :
- public class DBOpenHelper extends SQLiteOpenHelper {
- public DBOpenHelper(Context context) {
- super(context, "yhn.db", null, 1);
- }
-
-
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("create table user(uid integer primary key autoincrement, uname varchar(20), uaddress varchar(20))");
- }
-
-
- public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
-
- }
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context) {
super(context, "yhn.db", null, 1);
}
//數(shù)據(jù)庫(kù)第一次創(chuàng)建時(shí)候調(diào)用,
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(uid integer primary key autoincrement, uname varchar(20), uaddress varchar(20))");
}
//數(shù)據(jù)庫(kù)文件版本號(hào)發(fā)生變化時(shí)調(diào)用
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
如果執(zhí)行上邊的代碼,則會(huì)創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)文件xx.db
數(shù)據(jù)庫(kù)操作類:
- public class DBDao {
- DBOpenHelper dbOpenHelper;
-
- public DBDao(Context context){
- this.dbOpenHelper=new DBOpenHelper(context);
- }
-
-
-
-
- public void save(User user){
- SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
- db.execSQL("insert into user(uname,uaddress) values(?,?)", new Object[]{user.getUname(),user.getUaddress()});
- db.close();
- }
-
-
-
-
- public void delete(Integer uid){
- SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
- db.execSQL("delete from user where uid=?", new Object[]{uid});
- db.close();
- }
-
-
-
-
- public void update(User user){
- SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
- db.execSQL("update user set uname=?,uaddress=? where uid=?", new Object[]{user.getUname(),user.getUaddress(),user.getUid()});
- db.close();
- }
-
-
-
-
- public User find(Integer uid){
- SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
- Cursor cursor =db.rawQuery("select * from user where uid=?", new String[]{uid.toString()});
- if(cursor.moveToFirst()){
- int uid2=cursor.getInt(cursor.getColumnIndex("uid"));
- String uname=cursor.getString(cursor.getColumnIndex("uname"));
- String uaddress=cursor.getString(cursor.getColumnIndex("uaddress"));
- User user=new User();
- user.setUid(uid2);
- user.setUname(uname);
- user.setUaddress(uaddress);
- return user;
- }
- cursor.close();
- return null;
- }
-
-
-
-
-
-
- public List getScrollData(int offset, int maxResult){
- Listusers=new ArrayList();
- SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
- Cursor cursor =db.rawQuery("select * from user order by uid asc limit ?,?", new String[]{String.valueOf(offset), String.valueOf(maxResult)});
- while(cursor.moveToNext()){
- int uid2=cursor.getInt(cursor.getColumnIndex("uid"));
- String uname=cursor.getString(cursor.getColumnIndex("uname"));
- String uaddress=cursor.getString(cursor.getColumnIndex("uaddress"));
- User user=new User();
- user.setUid(uid2);
- user.setUname(uname);
- user.setUaddress(uaddress);
- users.add(user);
- }
- return users;
- }
-
-
-
-
- public long getCount(){
- SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
- Cursor cursor =db.rawQuery("select count(*) from user", null);
- cursor.moveToFirst();
- long reslut=cursor.getLong(0);
- return reslut;
- }
- }
public class DBDao {
DBOpenHelper dbOpenHelper;
public DBDao(Context context){
this.dbOpenHelper=new DBOpenHelper(context);
}
/**
* 添加一條數(shù)據(jù)
* @param user
*/
public void save(User user){
SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
db.execSQL("insert into user(uname,uaddress) values(?,?)", new Object[]{user.getUname(),user.getUaddress()});
db.close();
}
/**
* 刪除一條數(shù)據(jù)
* @param uid
*/
public void delete(Integer uid){
SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
db.execSQL("delete from user where uid=?", new Object[]{uid});
db.close();
}
/**
* 更新一條數(shù)據(jù)
* @param user
*/
public void update(User user){
SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
db.execSQL("update user set uname=?,uaddress=? where uid=?", new Object[]{user.getUname(),user.getUaddress(),user.getUid()});
db.close();
}
/**
* 查找一條數(shù)據(jù)
* @param uid
*/
public User find(Integer uid){
SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
Cursor cursor =db.rawQuery("select * from user where uid=?", new String[]{uid.toString()});
if(cursor.moveToFirst()){
int uid2=cursor.getInt(cursor.getColumnIndex("uid"));
String uname=cursor.getString(cursor.getColumnIndex("uname"));
String uaddress=cursor.getString(cursor.getColumnIndex("uaddress"));
User user=new User();
user.setUid(uid2);
user.setUname(uname);
user.setUaddress(uaddress);
return user;
}
cursor.close();
return null;
}
/**
* 分頁(yè)查找數(shù)據(jù)
* @param offset 跳過(guò)多少條數(shù)據(jù)
* @param maxResult 每頁(yè)多少條數(shù)據(jù)
* @return
*/
public List getScrollData(int offset, int maxResult){
Listusers=new ArrayList();
SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
Cursor cursor =db.rawQuery("select * from user order by uid asc limit ?,?", new String[]{String.valueOf(offset), String.valueOf(maxResult)});
while(cursor.moveToNext()){
int uid2=cursor.getInt(cursor.getColumnIndex("uid"));
String uname=cursor.getString(cursor.getColumnIndex("uname"));
String uaddress=cursor.getString(cursor.getColumnIndex("uaddress"));
User user=new User();
user.setUid(uid2);
user.setUname(uname);
user.setUaddress(uaddress);
users.add(user);
}
return users;
}
/**
* 獲取數(shù)據(jù)總數(shù)
* @return
*/
public long getCount(){
SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
Cursor cursor =db.rawQuery("select count(*) from user", null);
cursor.moveToFirst();
long reslut=cursor.getLong(0);
return reslut;
}
}
示例:向數(shù)據(jù)庫(kù)中添加一條數(shù)據(jù)
- DBDao dbDao=new DBDao(this);
- User user=new User();
- user.setUname("qixiaohu");
- user.setUaddress("chengdu");
- dbDao.save(user);
DBDao dbDao=new DBDao(this);
User user=new User();
user.setUname("qixiaohu");
user.setUaddress("chengdu");
dbDao.save(user);
PC上管理SQLite數(shù)據(jù)庫(kù)的方法:
上邊添加數(shù)據(jù)后可以看到數(shù)據(jù)添加成功:
該文章在 2013/2/25 14:33:24 編輯過(guò)