标签:
创建数据库需要使用的api:SQLiteOpenHelper(抽象类,sqlite打开帮助器)
必须定义一个构造方法:
//arg1:数据库文件的名字
//arg2:游标工厂(传null,使用默认)
//arg3:数据库版本(比如刚开始三个字段,然后需要四个版本号,这里给高。这一不能降级)
public MyOpenHelper(Context context, String name, CursorFactory factory, int version){}
//创建OpenHelper对象
MyOpenHelper oh = new MyOpenHelper(getContext(), "person.db", null, 1);
//获得数据库对象,如果数据库不存在,先创建数据库,后获得,如果存在,则直接获得
SQLiteDatabase db = oh.getWritableDatabase();
在创建数据库时创建表
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table person (_id integer primary key autoincrement, name char(10), phone char(20), money integer(20))");//primary主键,autoincrement自增长
}
//(SQLite是简化的字符串,这里的char,integer,是给程序员看的,其实内部都是String,传值时不会检查,但如果传的是错误的数据类型,也不会保存)
//如果下边传的是(‘张三’, ‘159874611’, “abc”),查表,存的实际是0,但不会保存
//插入
db.execSQL("insert into person (name, phone, money) values (?, ?, ?);", new Object[]{"张三", 15987461, 75000});
//查找
Cursor cs = db.rawQuery("select _id, name, money from person where name = ?;", new String[]{"张三"});
* 测试方法执行前会调用此方法
protected void setUp() throws Exception {
super.setUp();
// 获取虚拟上下文对象
oh = new MyOpenHelper(getContext(), "people.db", null, 1);
}
* 这里是我的测试源码
//数据库测试
public class TestCase extends AndroidTestCase {
//此时测试框架还没有初始化完毕,没有虚拟上下文对象
// private MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
private MyOpenHelper oh;
private SQLiteDatabase db;
public void test(){
//getContext():获取一个虚拟的上下文
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
//如果数据库不存在,先创建数据库,再获取可读可写的数据库对象,如果数据库存在,就直接打开
SQLiteDatabase db = oh.getWritableDatabase();
//如果存储空间满了,那么返回只读数据库对象
// SQLiteDatabase db = oh.getReadableDatabase();
}
//测试框架初始化完毕之后,在测试方法执行之前,此方法调用
@Override
protected void setUp() throws Exception {
super.setUp();
oh = new MyOpenHelper(getContext(), "people.db", null, 1);
db = oh.getWritableDatabase();
}
//测试方法执行完毕之后,此方法调用
@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
db.close();
}
public void insert(){
// db.execSQL("insert into person (name, salary, phone)values(?, ?, ?)", new Object[]{"老婆[1]", "13000", 138438});
// db.execSQL("insert into person (name, salary, phone)values(?, ?, ?)", new Object[]{"儿子", 14000, "13888"});
db.execSQL("insert into person (name, salary, phone)values(?, ?, ?)", new Object[]{"小小", 14000, "13888"});
}
public void delete(){
db.execSQL("delete from person where name = ?", new Object[]{"小小"});
}
public void update(){
db.execSQL("update person set phone = ? where name = ?", new Object[]{186666, "儿子"});
}
public void select(){
Cursor cursor = db.rawQuery("select name, salary from person", null);
while(cursor.moveToNext()){
//通过列索引获取列的值
String name = cursor.getString(cursor.getColumnIndex("name"));
String salary = cursor.getString(1);
System.out.println(name + ";" + salary);
}
}
public void insertApi(){
//把要插入的数据全部封装至ContentValues对象
ContentValues values = new ContentValues();
values.put("name", "夜煞部落");
values.put("phone", "15999");
values.put("salary", 16000);
db.insert("person", null, values);
}
public void deleteApi(){
int i = db.delete("person", "name = ? and _id = ?", new String[]{"儿子", "3"});
System.out.println(i);//返回值i是影响的行的数量
}
public void updateApi(){
ContentValues values = new ContentValues();
values.put("salary", 26000);
int i = db.update("person", values, "name = ?", new String[]{"夜煞部落"});
System.out.println(i);
}
public void selectApi(){
Cursor cursor = db.query("person", null, null, null, null, null, null, null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String salary = cursor.getString(cursor.getColumnIndex("salary"));
System.out.println(name + ";" + phone + ";" + salary);
}
}
public void transaction(){
try{
//开启事务
db.beginTransaction();
ContentValues values = new ContentValues();
values.put("salary", 12000);
db.update("person", values, "name = ?", new String[]{"小小"});
values.clear();
values.put("salary", 16000);
db.update("person", values, "name = ?", new String[]{"儿子"});
int i = 3/0;//如果没有执行下行代码,SQL语句回滚
//设置 事务执行成功
db.setTransactionSuccessful();
}
finally{
//关闭事务,同时提交,如果已经设置事务执行成功,那么sql语句就生效了,反之,sql语句回滚
db.endTransaction();
}
}
}
插入
//以键值对的形式保存要存入数据库的数据
ContentValues cv = new ContentValues();
cv.put("name", "刘能");
cv.put("phone", 1651646);
cv.put("money", 3500);
//返回值是改行的主键,如果出错返回-1
long i = db.insert("person", null, cv);
删除
//返回值是删除的行数
int i = db.delete("person", "_id = ? and name = ?", new String[]{"1", "张三"});
修改
ContentValues cv = new ContentValues();
cv.put("money", 25000);
int i = db.update("person", cv, "name = ?", new String[]{"赵四"});
查询
//arg1:要查询的字段
//arg2:查询条件
//arg3:填充查询条件的占位符
Cursor cs = db.query("person", new String[]{"name", "money"}, "name = ?", new String[]{"张三"}, null, null, null);
while(cs.moveToNext()){
// 获取指定列的索引值
String name = cs.getString(cs.getColumnIndex("name"));
String money = cs.getString(cs.getColumnIndex("money"));
System.out.println(name + ";" + money);
}
事务api
try {
//开启事务
db.beginTransaction();
...........
//设置事务执行成功
db.setTransactionSuccessful();
} finally{
//关闭事务
//如果此时已经设置事务执行成功,则sql语句生效,否则不生效
db.endTransaction();
}
任意插入一些数据
读取数据库的所有数据
Cursor cs = db.query("person", null, null, null, null, null, null);
while(cs.moveToNext()){
String name = cs.getString(cs.getColumnIndex("name"));
String phone = cs.getString(cs.getColumnIndex("phone"));
String money = cs.getString(cs.getColumnIndex("money"));
//把读到的数据封装至Person对象
Person p = new Person(name, phone, money);
//把person对象保存至集合中
people.add(p);
把集合中的数据显示至屏幕
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
for(Person p : people){
//创建TextView,每条数据用一个文本框显示
TextView tv = new TextView(this);
tv.setText(p.toString());
//把文本框设置为ll的子节点
ll.addView(tv);
}
分页查询
Cursor cs = db.query("person", null, null, null, null, null, null, "0, 10");
必须实现的两个方法
第一个
//系统调用此方法,用来获知模型层有多少条数据
@Override
public int getCount() {
return people.size();
}
第二个
//系统调用此方法,获取要显示至ListView的View对象
//position:是return的View对象所对应的数据在集合中的位置
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getView方法调用" + position);
TextView tv = new TextView(MainActivity.this);
//拿到集合中的元素
Person p = people.get(position);
tv.setText(p.toString());
//把TextView的对象返回出去,它会变成ListView的条目
return tv;
}
MainActivity.java
public class MainActivity extends Activity {
List<Person> personList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personList = new ArrayList<Person>();
//把数据库的数据查询出来
MyOpenHelper oh = new MyOpenHelper(this);
SQLiteDatabase db = oh.getWritableDatabase();
Cursor cursor = db.query("person", null, null, null, null, null, null, null);
while(cursor.moveToNext()){
String _id = cursor.getString(0);
String name = cursor.getString(1);
String salary = cursor.getString(2);
String phone = cursor.getString(3);
Person p = new Person(_id, name, phone, salary);
personList.add(p);
}
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
//把数据显示至屏幕
for (Person p : personList) {
//1.集合中每有一条元素,就new一个textView
TextView tv = new TextView(this);
//2.把人物的信息设置为文本框的内容
tv.setText(p.toString());
tv.setTextSize(18);
//3.把textView设置为线性布局的子节点
ll.addView(tv);
}
}
}
标签:
原文地址:http://blog.csdn.net/u013475983/article/details/51897513