SQLite数据库的使用
来源:互联网 发布:mysql primary unique 编辑:程序博客网 时间:2024/05/09 00:56
SQLite数据库
- 轻量级关系型数据库
创建数据库需要使用的api:SQLiteOpenHelper
必须定义一个构造方法:
//arg1:数据库文件的名字//arg2:游标工厂//arg3:数据库版本public MyOpenHelper(Context context, String name, CursorFactory factory, int version){}
- 数据库被创建时会调用:onCreate方法
- 数据库升级时会调用:onUpgrade方法
创建数据库
//创建OpenHelper对象MyOpenHelper oh = new MyOpenHelper(getContext(), "person.db", null, 1);//获得数据库对象,如果数据库不存在,先创建数据库,后获得,如果存在,则直接获得SQLiteDatabase db = oh.getWritableDatabase();
- getWritableDatabase():打开可读写的数据库
- getReadableDatabase():在磁盘空间不足时打开只读数据库,否则打开可读写数据库
在创建数据库时创建表
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))");}
数据库的增删改查
SQL语句
- insert into person (name, phone, money) values ('张三', '159874611', 2000);
- delete from person where name = '李四' and _id = 4;
- update person set money = 6000 where name = '李四';
- select name, phone from person where name = '张三';
执行SQL语句实现增删改查
//插入 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);}
使用api实现增删改查
插入
//以键值对的形式保存要存入数据库的数据ContentValues cv = new ContentValues();cv.put("name", "刘能");cv.put("phone", 1651646);cv.put("money", 3500);//返回值是改行的主键,如果出错返回-1long 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);}
事务
- 保证多条SQL语句要么同时成功,要么同时失败
- 最常见案例:银行转账
事务api
try { //开启事务 db.beginTransaction(); ........... //设置事务执行成功 db.setTransactionSuccessful();} finally{ //关闭事务 //如果此时已经设置事务执行成功,则sql语句生效,否则不生效 db.endTransaction();}
把数据库的数据显示至屏幕
- 任意插入一些数据
- 定义业务bean:Person.java
读取数据库的所有数据
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");
0 0
- SQLite数据库的使用
- sqlite数据库的使用
- Sqlite数据库的使用
- sqlite数据库的使用
- SQLite数据库的使用
- SQLite数据库的使用
- SQLite数据库的使用
- SQLite数据库的使用
- SQLite数据库的使用
- sqlite数据库的使用
- SQlite数据库的使用
- (数据库)SQLite的使用
- SQLite数据库的使用
- SQLite数据库的使用
- SQLite数据库的使用
- Sqlite数据库的使用体会
- Android使用的SQLite数据库
- Android SQLite数据库的使用
- 百度在线语音识别接入经验
- POJ-2386 Lake counting
- 【代码片段】隐藏与显示部分列表
- [Linux].vimrc配置
- MySQL Study之--MySQL压力测试工具mysqlslap
- SQLite数据库的使用
- VPS服务器如何配置伪静态化(.htaccess配置)
- Java 之Random用法及实例
- MySQL入门
- 史上最全的CSS hack方式一览
- mac之ssh免登陆验证、ssh-keygen
- ios 拍照 照片过小 UIImagePickerController
- 大学4年,毕业后迎接你的不应该是各种培训班
- [leetcode] Insertion Sort List