Android的数据存储(二)——SQLite数据库

    Android系统集成了一个轻量级的数据库,SQLite。它是一个嵌入式的数据引擎,专门适用于资源有限的设备上(如手机、PDA等)适量数据存储。SQLite数据库只是一个文件,不需要像Oracle、MySQL等需要安装启动。

1,SQLiteDatabase

     Android提供一个SQLiteDatabase代表一个数据库(底层就是一个数据库文件),一旦获取了SQLiteDatabase对象,就能通过SQLiteDatabase对象来管理操作数据库。

1)获取SQLieteDatabase对象

//		打开path文件对应的SQLite数据库
//		db = SQLiteDatabase.openDatabase(path, null, null);
//		打开或创建mydb.dat数据库,该文件在/data/data/app.packagename/files目录中
		db = SQLiteDatabase.openOrCreateDatabase(getFilesDir().getPath()
				+ "/mydb.dat", null);

2)使用execSQL来执行SQL语句。

//创建
db.execSQL("create table if not exists tb_user(_id integer primary key autoincrement,username varchar(20),password varchar(20) )");
//插入
db.execSQL("insert into tb_user values(null,?,?)", new String[] {
					"张三", "1234" });
//查询
// rawQuery(String sql, String[] selectionArgs)
Cursor cursor = db.rawQuery("select * from tb_user", null);
//删除
db.execSQL("delete from tb_user where _id=49");
db.execSQL("delete from tb_user where _id = ?",new String[] { "50" });
//修改
db.execSQL("update tb_user set username='1234' where _id = ?", new String[]{"12"});

 

3)使用SQLiteDatabase 提供的方法

    

//插入	
	ContentValues values = new ContentValues();
	values.put("username", "张无忌");
	values.put("password", "1234");
	db.insert("tb_user", null, values);
//删除
	db.delete("tb_user", "username like ?", new String[] { "张_" });
//修改
	db.update("tb_user", values, "_id>?", new String[]{"10"});
//查询
// query(String table, String[] columns, String selection,
// String[] selectionArgs, String groupBy, String having, String
// orderBy, String limit)
	Cursor cursor = db.query("tb_user", new String[] { "_id",
						"username", "password" }, "username like ?",
						new String[] { "张%" }, null, null, null, "5,10");

   4)遍历Cursor结果集

       boolean  cursor.move(int offset);  offset为正,向下移动,为负,向上移动

       booealn  cursor.moveToFirst()

       booealn  cursor.moveToLast()

       booealn  cursor.moveToNext() 

        booealn  cursor.moveToPosition(int position)  移动到指定行,成功返回true

        booealn  cursor.moveToPrevious()

	Cursor cursor = db.rawQuery("select * from tb_user", null);
	while (cursor.moveToNext()) {
	int id = cursor.getInt(cursor.getColumnIndex("_id"));
	String username = cursor.getString(cursor.getColumnIndex("username"));
	String password = cursor.getString(cursor.getColumnIndex("password"));
        }

 5)事务处理

    SQLiteDatabase.beginTransaction();

    SQLiteDatabase.endTransation();

    SQLiteDatabase.inTransation();  //

    SQLiteDatabase.setTransationSuccessful();//判断事务是否成功,否则在endTransation()时回滚事务

   

	db.beginTransaction();
		try {
			db.execSQL("insert into tb_user values(null,?,?", new String[] {
					"张三", "1234" });//语句有错,异常
			db.setTransactionSuccessful();//此处觉得是提交事务还是回滚事务
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();
		}

    2,使用命令去操作SQLite数据库

    cmd-->

    >adb shell   进入android系统

    >ls data/data/com.package/files  进入对应数据库文件的存放目录

    >sqlite3 my.db  进入数据库

   > .databases 查看所有数据库文件

   > . tables  查看该数据库的所有表

   > .quit 退出


 

 

    

猜你喜欢

转载自lydia-fly.iteye.com/blog/2145241