Android 数据存储与读取:SQLite
|
admin
2013年2月25日 14:33
本文热度 4127
|
在Android平台上,集成了一个嵌入式关系型数据库—SQLite。如果你想要开发 Android 应用程序,一定需要在 Android 上存储数据,使用SQLite 数据库是一种非常好的选择。
下面介绍的基本使用:
是一个抽象类不能直接实例化,所以新建一个类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);
}
//数据库第一次创建时候调用,
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) {
}
如果执行上边的代码,则会创建一个数据库文件xx.db
数据库操作类:
- 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);
}
/**
* 添加一条数据
* @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();
}
/**
* 删除一条数据
* @param uid
*/
public void delete(Integer uid){
SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
db.execSQL("delete from user where uid=?", new Object[]{uid});
db.close();
}
/**
* 更新一条数据
* @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();
}
/**
* 查找一条数据
* @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;
}
/**
* 分页查找数据
* @param offset 跳过多少条数据
* @param maxResult 每页多少条数据
* @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;
}
/**
* 获取数据总数
* @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;
}
}
示例:向数据库中添加一条数据
- 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数据库的方法:
上边添加数据后可以看到数据添加成功:
该文章在 2013/2/25 14:33:24 编辑过