Android中Sqlite的操作(SQLiteDataBase自带方法)

来源:互联网 发布:淘宝推广返利 编辑:程序博客网 时间:2024/05/20 10:24

主要的步骤:

1.写一个类extends SQLiteOpenHelper,并重写构造函数、oncreate()、onupdate();

public voidonCreate(SQLiteDatabase db) {
//数据库第一次被创建的时候执行如下sql语句创建一个person
db.execSQL("create table person(_id integer primary key
autoincrement, name varchar(20), phone varchar(20), money
integer(20),age integer(10));"
);
}


public voidonUpgrade(SQLiteDatabase db, intoldVersion, int newVersion) {
//数据库的版本更新的时候执行
if (oldVersion == 1 && newVersion == 2) {
db.execSQL(
"alter table person add column
balance integer"
);
}
}
 


2.创建这个类对象

3.实现crud

//测试添加数据
public void insert() {
SQLiteDatabase dataBase = getDataBase();
ContentValues values =
newContentValues();
values.put(
"name","heima");
values.put(
"age", 5);
values.put(
"phone","010-82826816");
/*
*
第一个参数 table,代表要将数据插入哪家表 第二个参数
*
nullColumnHack
,字符串类型,指明如果某一字段没有值,那么会将该字段的值设为N
ULL
*
,一般给该参数传递null就行如果没有特殊要求
*
,在这里我传递了phone字符串,也就是说当我的ContentValuesphone字段为空的
时候系统自动给其值设置为
NULL
*
第三个参数ContentValues
类似一个Map<key,value>的数据结构,key是表中的字段, value是值
*/
dataBase.insert(
"person","phone", values);
}


//
测试删除数据
public void delete() {
SQLiteDatabase database = getDataBase();
/*
*
第一个参数 table,代表要删除的表名
* 第二个参数whereClause
,
选择的条件选项,如果为null则删除表中的所有数据
* 第三个参数whereArgs
,
如果有条件选项,对应的条件选项的具体参数,没有写null
*
删除名字为"heima"的记录
*/
database.delete(
"person","name=?",new
String[]{"pl"});
}


//测试修改数据
public void update() {
SQLiteDatabase database = getDataBase();
ContentValues values =
newContentValues();
values.put(
"age","100");
/*
*
第一个参数table,要更新的表名
* 第二个参数ContentValues
-设置要修改的字段的新值,没有涉及到的字段则默认不修改
*- 第三、四个参数的含义同方法delete
*/
database.update("person", values ,"name=?",new
String[]{"heima"});
}


//测试查询单个数据
public void query() {
SQLiteDatabase database = getDataBase();
/*
*
第一个参数 table,查询的表名
* 第二个参数columns,要查询的字段
* 第三个参数selection过滤字段
* 第四个参数selectionArgs过滤字段的值
* 第五个参数groupBy分组字段, null代表不分组
* 第六个参数having
* A filter declare which row groups to include in the
cursor,
* if row grouping is being used, formatted as an SQL
HAVING clause
* (excluding the HAVING itself). Passing null will
cause all row groups
* to be included, and is required when row grouping
is not being used.
*
第七个参数orderBy
排序字段, asc正序,desc倒序,null代表自然顺序
*/
Cursor cursor = database.query("person",new String[]{"name,age,phone"},"name=?",new String[]{"heima"},null,null, null);
int id = cursor.getInt(0);
String name = cursor.getString(1);
String phone = cursor.getString(2);
System.
out.println(id +"_" + name +"_" +phone);
}



//测试查询所有数据
public void queryAll(){
SQLiteDatabase database = getDataBase();
List<Person> persons =
newArrayList<Person>();
/*
*
该方法跟query()方法是完全一样的,因此参数的含义不在介绍
*/

Cursor cursor = database.query("person",new

String[]{"name,age,phone"},null, null,null, null,null);

while(cursor.moveToNext()){
Person p =

Person p = newPerson();
String name = cursor.getString(0);

String name = cursor.getString(0);

int age = cursor.getInt(1);
String phone = cursor.getString(2);
p.setName(name);
p.setAge(age);
p.setPhone(phone);
persons.add(p);
}
}

String phone = cursor.getString(2);

p.setName(name);

p.setAge(age);

p.setPhone(phone);

persons.add(p);

}
}
 


事务:

执行一段代码,要么同时成功,失败时回滚

beginTransaction(): 开启一个事务
setTransactionSuccessful():设置事务成功标记
endTransaction(): 
结束事务,包括提交和回滚,需要放在
finally中执行,否则事
务只有到超时的时候才自动结束,会降低数据库并发效率




0 0
原创粉丝点击