标签:
可以把各种类型数据保存在任何字段,不用关心字段声明的类型。
Integer PRIMARY KEY只能保存64位整数
声明的类型长度无效
当调用getReadableDatabase()或getWritableDatabase()时才会创建数据库。
查询分页:
select * from Account limit 3 5
跳过前3条数据 取后面5条数据
用户第一次使用软件时,自动创建数据库
extends SQLiteOpenHelper
onCreate()第一次创建数据库使用,仅执行一次
onUpdate()版本号改变,数据库版本升级时调用
一个数据库以一个文件存在
class DBUtils extends SQLiteOpenHelper{
public DBUtils(Context context, String name, CursorFactory factory,int version) {
super(context, "lyj.db", null, 2);//null使用系统自带游标工厂
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("alter table person add phone varchar(12) null");
}
}
都可以获取一个用于操作数据库的SQLiteDatabase实例
getReadableDatabase()方法中会调用getWritableDatabase()方法
数据库可设置最大容量,getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用的是getWritableDatabase() 方法就会出错
public void saveDB(Person person) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
// 1
db.execSQL("insert into person(name,age,phone) values (?,?,?)",new Object[] { person.getName(), person.getAge(), person.getPhone() });
// 2
ContentValues values = new ContentValues();
values.put("name", person.getName());
values.put("age", String.valueOf(person.getAge()));
db.insert("person", null, values);
// 如果第3个字段为null就会拼接sql语句为如下:(如确定第3个字段一定不为null,第2字段可写为null)
// db.insert("person", "name", null); ==>insert into person(name) values(null)
// db.close(); //本用户本应用使用,关不关不大影响性能
}
public void deleteDB(Integer id) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL("delete from person where personid = ?", new Object[] { id });
// db.delete("person", "personid = ?", new String[] { id.toString() });
}
public void updateDB(Person person) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
// 1
db.execSQL("update person set name = ?,age = ?,phone = ? where personid = ?)",
new Object[] { person.getName(), person.getAge(), person.getPhone(), person.getId() });
// 2
ContentValues values = new ContentValues();
values.put("name", person.getName());
values.put("phone", person.getPhone().toString());
db.update("person", values, "personid = ?", new String[] { String.valueOf(person.getId())});
}
public Person findDB(Integer id) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
// 1
Cursor cursor = db.rawQuery("select * from person where personid = ?)",new String[] { id.toString() });
// 2 第2参数:null返回所有字段
Cursor cursor2 = db.query("person", null, "personid = ?", new String[] { id.toString() },null, null, null);
if (cursor.moveToFirst()) {// 仅有一条记录
String name = cursor.getString(cursor.getColumnIndex("name"));
Integer age = cursor.getInt(cursor.getColumnIndex("age"));
// ...
cursor.close();
return new Person(name, age);
}
return person;
}
public List<Person> getScrollData(int offset, int maxSize) {
List<Person> persons = new ArrayList<Person>();
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from person limit ?,?)",
new String[] { String.valueOf(offset), String.valueOf(maxSize) });
//按personid 排序 limit 3,5
Cursor cursor2 = db.query("person", null, null, null, null, null, "personid asc", offset+ "," + maxSize);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
Integer age = cursor.getInt(cursor.getColumnIndex("age"));
// ...
persons.add(new Person(name, age));
}
cursor.close();
return persons;
}
// 统计
public long getCount() {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from person)", null);
Cursor cursor2 = db.query("person", new String[]{"count(*)"}, null, null, null, null, null);
if (cursor.moveToFirst()) {// 仅有一条记录
long count = cursor.getColumnCount();
cursor.close();
return count;
}
return 0;
}
数据库事务
// 事务
public void transferAccount() {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
try {
db.beginTransaction();
db.execSQL("update person set amount= amount-10 where personid =2");
db.execSQL("update person set amount= amount+10 where personid =3");
db.setTransactionSuccessful();
} finally {
//保证结束事务一定执行
db.endTransaction();// 默认:回滚(事务标识为true为提交)
}
}
标签:
原文地址:http://www.cnblogs.com/liyuejiao/p/4257006.html