android学习---SQLite数据库存储

来源:互联网 发布:windows系统日志备份 编辑:程序博客网 时间:2024/04/30 05:02

一、SQLite概述

  SQLite是一款轻量级的关系型数据库,它的运算速度非常快,占用资源很少,通常只需要几百KB的内存就足够了,因而特别适合在移动设备上使用。SQLite不仅支持标准的SQL语法,还遵循了数据库的ACID事务。

  SQLite比一般的数据库要简单的多,他甚至不用设置用户名和密码就可以使用。Android把这个功能极为强大的数据库嵌入到了系统当中,使得本地持久化的功能有了一次质的飞跃。

  SQLite数据库可以存储数据量大、结构性复杂的数据,比如:我们手机短信程序中的很多会话。这些都是文件存储和SharedPreferences存储很难做到的。

二、如何使用SQLite

1.创建数据库和数据表2.添加数据3.修改数据4.删除数据5.查询数据
创建数据库和数据表的步骤
1.新建类继承SQLiteOpenHelper
2.实现构造方法
  四个参数:1.Context context:上下文环境。2.String name:数据库名。3.CursorFactory factory:允许开发者查询数据的时候返回一个自定义的Cursor,一般传入null。4.int version:当前的数据库的版本号,可用于对数据库进行升级操作。
3.重写onCreate方法
4.重写onUpgrade方法
5.实例化SQLiteOpenHelper的子类对象
6.调用getReadableDatabase方法或getWritableDatabase方法
public class DBHelper extends SQLiteOpenHelper {private String sql_str="create table product(" +"id integer primary key not null," +"name text not null," +"price real not null," +"pic_url text not null," +"description text)";public DBHelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);}@Overridepublic void onCreate(SQLiteDatabase arg0) {arg0.execSQL(sql_str);}@Overridepublic void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {}}


添加数据
  SQLiteDatabase中提供了一个insert()方法,这个方法就是专门用于添加数据的。它接收3个参数,第一个参数是表名,我们希望向哪张表里添加数据,这里就传入该表的名字。第二个参数用于在未指定添加数据的情况下给某些可为空的列自动复制NULL,一般我们用不到这个功能,直接传入null即可。第三个参数是一个ContentValues对象,它提供了一系列的put()方法重载,用于向ContentValues中添加数据,只需要将表中的每个列名以及相应的待参加数据传入即可。

/* * 插入数据 */private void insertData() {// 实例化DBHelper对象DBHelper dbhelper = new DBHelper(MainActivity.this, "mydb_2016", null,1);// 得到SQLiteDatabase对象SQLiteDatabase db = dbhelper.getWritableDatabase();// 创建ContentValues对象ContentValues cv = new ContentValues();cv.put("name", "酱油");cv.put("price", "9.9");cv.put("pic_url", "abc.jpg");// 调用insert方法插入数据db.insert("product", null, cv);}

修改数据

  SQLiteDatabase中也提供了一个update()方法,用于对数据进行更新,这个方法接收4个参数,第一个参数也是表名,在这里指定去更新哪张表里的数据。第二个参数是ContentValues对象,要把更新数据在这里组装进去。第三、四个参数用于约束更新某一行或某几行的数据,不指定的话默认就是更新所有行。

/* * 修改数据 */private void modifyData() {DBHelper dbHelper=new DBHelper(this, "mydb_2016", null, 1);SQLiteDatabase db=dbHelper.getWritableDatabase();ContentValues cv=new ContentValues();cv.put("name", "大米");//调用update方法对数据库进行修改db.update("product", cv, "id>? and id<?", new String[]{"2","5"});}
删除数据

  SQLiteDatabase中提供了一个delete()方法,专门用于删除数据,这个方法接收3个参数,第一个参数也是表名,在这里指定去删除哪张表里的数据。第二、三个参数用于约束删除某一行或某几行的数据,不指定的话默认就是删除所有行。

/* * 删除数据 */private void deleteData() {DBHelper dbHelper=new DBHelper(this, "mydb_2016", null, 1);SQLiteDatabase db=dbHelper.getWritableDatabase();//调用delete方法对数据库进行删除db.delete("product", "id>?", new String[]{"2"});}

查询数据

  SQLiteDatabase中还提供了一个query()方法用于对数据进行查询,这个方法需要传入7个参数,第一个参数还是表名,在这里指定去查询哪张表里的数据。第二个参数用于指定去查询哪几列,如果不指定则默认查询所有列。第三、四个参数用于约束查询某一行或某几行的数据,不指定的话默认就是查询所有行。第五个参数用于指定需要去group by的列,不指定则表示不对查询结果进行group by操作。第六个参数用于对group by之后的数据进行进一步的过滤,不指定则表示不进行过滤。第七个参数用于指定查询结果的排序方法,不指定则表示使用默认的排序方法。

/* * 查询数据 */private void queryData() { list = new ArrayList<ProductSelect>();  DBHelper dbHelper=new DBHelper(this, "mydb_2016", null, 1); SQLiteDatabase db=dbHelper.getReadableDatabase(); Cursor cursor=db.query("product", null, null, null, null, null, null); cursor.moveToFirst(); do{ int id=cursor.getInt(cursor.getColumnIndex("id")); String name=cursor.getString(cursor.getColumnIndex("name")); String pic_url=cursor.getString(cursor.getColumnIndex("pic_url"));double price=cursor.getDouble(cursor.getColumnIndex("price"));String desc=cursor.getString(cursor.getColumnIndex("description")); ProductSelect p=new ProductSelect(); p.setId(id); p.setName(name); p.setPic(pic_url); p.setPrice(price);  list.add(p); } while(cursor.moveToNext()); cursor.close();  adapter = new ProductSelectAdapter(MainActivity.this, list);listView.setAdapter(adapter);}





原创粉丝点击