1.SQLite的特点
比较小巧,适合在小型设备上使用
Android手机自带SQLite
SQLite除了主键意外,不区分数据类型
SQLite语句和MySQL语句基本相同
2.创建数据库
定义类继承SQLiteOpenHelper,该类没有无参的构造函数,必须调用有参数的构造函数,分别传入:环境对象、数据库名称、游标工厂和数据库版本。
需要重写两个抽象方法:onCreate()和onUpgrade(),用于创建或更新修改数据库。
使用数据库:创建该类的对象,调用getWritableDatabase()或者getReadableDatabase()方法即可获取到SQLiteDatabase对象,该对象可以执行SQL语句;
情况1:数据库不存在时,创建数据库执行onCreate()方法
情况2:数据库存在,版本未变,直接打开数据库,不执行任何方法
情况3:数据库存在,版本发生改变,打开数据库,执行onUpgrade()方法
3.增删改查
增删改时需要使用getWritableDatabase()方法得到SQLiteDatabase对象,然后调用execSQL()方法来执行SQL语句。
查询时要使用getReadableDatabase()方法来得到SQLiteDatabase对象,然后调用rawQuery()方法查询得到Cursor,然后调用moveToNext()方法移动Cursor,使用getString()、getInt()等方法来获取对应数据。
public class MyOpenHelper extends SQLiteOpenHelper { public MyOpenHelper(Context context) { super(context, "xxx.db", null, 1); /* * 参数1:Context,用来确认数据库文件的位置 * 参数2:数据库文件的名称 * 参数3:用来创建游标对象的工厂,null代表使用默认工厂 * 参数4:数据库版本,从1开始 */ } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE person(id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("ALTER TABLE person ADD balance INTEGER"); } }
public class PersonDaoClassic { private MyOpenHelper helper; public PersonDaoClassic(Context context){ helper = new MyOpenHelper(context); } public void insert(Person p){ SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("INSERT INTO person(name,balance) VALUES(?,?)",new Object[]{p.getName(),p.getBalance()}); db.close(); } public void delete(int id){ SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("DELETE FROM person WHERE id=?",new Object[]{id}); db.close(); } public void update(Person p){ SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("UPDATE person SET name=?,balance=? WHERE id=?",new Object[]{p.getName(),p.getBalance(),p.getId()}); db.close(); } public Person query(int id){ Person p = null; SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.rawQuery("SELECT name,balance FROM person WHERE id=?", new String[]{id+""});//查询获得游标(结果集) if(c.moveToNext()){//把游标向后移一位,判断是否有数据 String name = c.getString(c.getColumnIndex("name"));//先根据列名获得索引,再根据索引获取数据 int balance = c.getInt(1);//由于知道balance的索引,所以直接通过索引获取数据 p = new Person(id, name, balance); } c.close(); db.close(); return p; } public List<Person> queryAll(){ List<Person> persons = new ArrayList<Person>(); SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.rawQuery("SELECT * FROM person", null); while(c.moveToNext()){ int id = c.getInt(c.getColumnIndex("id")); String name = c.getString(c.getColumnIndex("name")); int balance = c.getInt(c.getColumnIndex("balance")); persons.add(new Person(id, name, balance)); } c.close(); db.close(); return persons; } public List<Person> queryPage(int pageNum,int pageSize){ int offset = (pageNum-1)*pageSize; List<Person> persons = new ArrayList<Person>(); SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.rawQuery("SELECT * FROM person LIMIT ?,?", new String[]{offset+"",pageSize+""}); while(c.moveToNext()){ int id = c.getInt(c.getColumnIndex("id")); String name=c.getString(c.getColumnIndex("name")); int balance = c.getInt(c.getColumnIndex("balance")); persons.add(new Person(id, name, balance)); } c.close(); db.close(); return persons; } }
public class Person { private Integer id; private String name; private Integer balance; public Person() { super(); } public Person(String name, Integer balance) { super(); this.name = name; this.balance = balance; } public Person(Integer id, String name, Integer balance) { super(); this.id = id; this.name = name; this.balance = balance; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getBalance() { return balance; } public void setBalance(Integer balance) { this.balance = balance; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", balance=" + balance+"]"; } }
4.另一种增删改查
使用insert()、delete()、update()、query()四个方法,而不是用SQL语句,通过传入一些参数,方法内部自动拼装SQL语句。
public class PersonDao { private MyOpenHelper helper; public PersonDao(Context context){ helper = new MyOpenHelper(context); } public long insert(Person p){ SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues();//这是一个Map,用来装载要插入的列名和值 values.put("name", p.getName()); values.put("balance", p.getBalance()); long id = db.insert("person", "id", values);//向person表中插入values中的数据,得到新纪录的id。第二个参数可以为null //如果values中的键值都为null时,此时插入的是空记录(第二个参数不能为null) db.close(); return id; } public int delete(int id){ SQLiteDatabase db = helper.getWritableDatabase(); int rows = db.delete("person", "id=?", new String[]{id+""});//删除指定条件的记录,返回值为影响到的行数 db.close(); return rows; } public int update(Person p){ SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", p.getName()); values.put("balance", p.getBalance()); int rows = db.update("person", values, "id=?", new String[]{p.getId()+""}); db.close(); return rows; } public Person query(int id){ Person p = null; SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.query("person", new String[]{"name","balance"}, "id=?", new String[]{id+""}, null, null, null); if(c.moveToNext()){//把游标向后移一位,判断是否有数据 String name = c.getString(c.getColumnIndex("name"));//先根据列名获得索引,再根据索引获取数据 int balance = c.getInt(1);//由于知道balance的索引,所以直接通过索引获取数据 p = new Person(id, name, balance); } c.close(); db.close(); return p; } public List<Person> queryAll(){ List<Person> persons = new ArrayList<Person>(); SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.query("person", null, null, null,null, null, "balance DESC", null); while(c.moveToNext()){ int id = c.getInt(c.getColumnIndex("id")); String name = c.getString(c.getColumnIndex("name")); int balance = c.getInt(c.getColumnIndex("balance")); persons.add(new Person(id, name, balance)); } c.close(); db.close(); return persons; } public List<Person> queryPage(int pageNum,int pageSize){ int offset = (pageNum-1)*pageSize; List<Person> persons = new ArrayList<Person>(); SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.query("person", null, null, null,null, null, null, offset+","+pageSize); while(c.moveToNext()){ int id = c.getInt(c.getColumnIndex("id")); String name=c.getString(c.getColumnIndex("name")); int balance = c.getInt(c.getColumnIndex("balance")); persons.add(new Person(id, name, balance)); } c.close(); db.close(); return persons; } public Cursor queryAllCusor(){ SQLiteDatabase db = helper.getReadableDatabase(); return db.query("person", new String[]{"id as _id","name","balance"}, null, null, null, null, null); } }
该方法适用于ContentProvider中,在ContentProvider中的一些回调函数的参数就是表名和数据,直接调用这四个方法执行操作即可。