实体类:
com.itheima28.sqlitedemo.entities包
public class Person {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person() {
super();
}
public Person(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
数据库帮助类
com.itheima28.sqlitedemo.db包:
/**
* @author andong
* 数据库帮助类, 用于创建和管理数据库的.
*/
public class PersonSQLiteOpenHelper extends SQLiteOpenHelper {
private static final String TAG = "PersonSQLiteOpenHelper";
/**
* 数据库的构造函数 :用来定义数据库的名称 ,数据库查询的结果,数据库的版本
* @param context :作用:数据库在哪里进行创建
*
* name 数据库名称
* factory 游标工程
* version 数据库的版本号 不可以小于1
*/
public PersonSQLiteOpenHelper(Context context) {
super(context, "itheima28.db", null, 2);
}
/**
* 数据库第一次创建时回调此方法.
* 初始化一些表
*/
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table person(_id integer primary key, name varchar(20), age integer);";
db.execSQL(sql);
}
/**
* 数据库的版本号更新时回调此方法,
* 更新数据库的内容(删除表, 添加表, 修改表)
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion == 1 && newVersion == 2) {
Log.i(TAG, "数据库更新啦");
db.execSQL("alter table person add balance integer;");
}
}
}
DAO类
com.itheima28.sqlitedemo.dao:
public class PersonDao {
private PersonSQLiteOpenHelper mOpenHelper;
public PersonDao(Context context) {
mOpenHelper = new PersonSQLiteOpenHelper(context);
}
/**
* 添加到person表一条数据
* @param person
*/
public void insert(Person person) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) {
db.execSQL("insert into person(name, age) values(?, ?);", new Object[]{person.getName(), person.getAge()});
db.close();
}
}
/**
* 更据id删除记录
* @param id
*/
public void delete(int id) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) {
db.execSQL("delete from person where _id = ?;", new Integer[]{id});
db.close();
}
}
/**
* 根据id找到记录, 并且修改姓名
* @param id
* @param name
*/
public void update(int id, String name) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) {
db.execSQL("update person set name = ? where _id = ?;", new Object[]{name, id});
db.close();
}
}
public List queryAll() {
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
if(db.isOpen()) {
Cursor cursor = db.rawQuery("select _id, name, age from person;", null);
if(cursor != null && cursor.getCount() > 0) {
List personList = new ArrayList();
int id;
String name;
int age;
while(cursor.moveToNext()) {
id = cursor.getInt(cursor.getColumnIndex("_id"));
name = cursor.getString(cursor.getColumnIndex("name"));
age = cursor.getInt(cursor.getColumnIndex("age"));
personList.add(new Person(id, name, age));
}
cursor.close();
db.close();
return personList;
}
db.close();
}
return null;
}
/**
* 根据id查询人
* @param id
* @return
*/
public Person queryItem(int id) {
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
if(db.isOpen()) {
Cursor cursor = db.rawQuery("select _id, name, age from person where _id = ?;", new String[]{String.valueOf(id)});
if(cursor != null && cursor.moveToFirst()) {
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
cursor.close();
db.close();
return new Person(_id, name, age);
}
db.close();
}
return null;
}
}
public class PersonDao2 {
private static final String TAG = "PersonDao2";
private PersonSQLiteOpenHelper mOpenHelper;
public PersonDao2(Context context) {
mOpenHelper = new PersonSQLiteOpenHelper(context);
}
/**
* 添加到person表一条数据
* @param person
*/
public void insert(Person person) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) {
ContentValues values = new ContentValues();
values.put("name", person.getName());
values.put("age", person.getAge());
long id = db.insert("person", "name", values);
Log.i(TAG, "id: " + id);
db.close();
}
}
/**
* 更据id删除记录
* @param id
*/
public void delete(int id) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) {
String whereClause = "_id = ?";
String[] whereArgs = {id + ""};
int count = db.delete("person", whereClause, whereArgs);
Log.i(TAG, "删除了: " + count + "行");
db.close();
}
}
/**
* 根据id找到记录, 并且修改姓名
* @param id
* @param name
*/
public void update(int id, String name) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) {
ContentValues values = new ContentValues();
values.put("name", name);
int count = db.update("person", values, "_id = ?", new String[]{id + ""});
Log.i(TAG, "修改了: " + count + "行");
db.close();
}
}
public List queryAll() {
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
if(db.isOpen()) {
String[] columns = {"_id", "name", "age"};
String selection = null;
String[] selectionArgs = null;
String groupBy = null;
String having = null;
String orderBy = null;
Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);
int id;
String name;
int age;
if(cursor != null && cursor.getCount() > 0) {
List personList = new ArrayList();
while(cursor.moveToNext()) {
id = cursor.getInt(0);
name = cursor.getString(1);
age = cursor.getInt(2);
personList.add(new Person(id, name, age));
}
db.close();
return personList;
}
db.close();
}
return null;
}
/**
* 根据id查询人
* @param id
* @return
*/
public Person queryItem(int id) {
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
if(db.isOpen()) {
String[] columns = {"_id", "name", "age"};
String selection = "_id = ?";
String[] selectionArgs = {id + ""};
String groupBy = null;
String having = null;
String orderBy = null;
Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);
if(cursor != null && cursor.moveToFirst()) {
int _id = cursor.getInt(0);
String name = cursor.getString(1);
int age = cursor.getInt(2);
db.close();
return new Person(_id, name, age);
}
db.close();
}
return null;
}
}
测试类
com.itheima28.sqlitedemo.test
public class TestCase extends AndroidTestCase {
private static final String TAG = "TestCase";
public void test() {
PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext());
openHelper.getReadableDatabase();
}
public void testInsert() {
PersonDao dao = new PersonDao(getContext());
dao.insert(new Person(1, "冠希", 28));
}
public void testDelete() {
PersonDao dao = new PersonDao(getContext());
dao.delete(1);
}
public void testUpdate() {
PersonDao dao = new PersonDao(getContext());
dao.update(3, "凤姐");
}
public void testQueryAll() {
PersonDao dao = new PersonDao(getContext());
List personList = dao.queryAll();
for (Person person : personList) {
Log.i(TAG, person.toString());
}
}
public void testQueryItem() {
PersonDao dao = new PersonDao(getContext());
Person person = dao.queryItem(4);
Log.i(TAG, person.toString());
}
public void testTransaction() {
PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext());
SQLiteDatabase db = openHelper.getWritableDatabase();
if(db.isOpen()) {
try {
db.beginTransaction();
db.execSQL("update person set balance = balance - 1000 where name = 'zhangsan';");
db.execSQL("update person set balance = balance + 1000 where name = 'lisi';");
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
db.close();
}
}
public void testTransactionInsert() {
PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext());
SQLiteDatabase db = openHelper.getWritableDatabase();
if(db.isOpen()) {
long start = System.currentTimeMillis();
try {
db.beginTransaction();
for (int i = 0; i < 10000; i++) {
db.execSQL("insert into person(name, age, balance) values('wang" + i + "', " + (10 + i) + ", " + (10000 + i) + ")");
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
long end = System.currentTimeMillis();
long diff = end - start;
Log.i(TAG, "耗时: " + diff + "毫秒");
db.close();
}
}
}