SQLite数据库总结
来源:互联网 发布:电脑办公软件自学 编辑:程序博客网 时间:2024/05/18 01:03
SQLite数据库
- 轻量级关系型数据库
创建数据库需要使用的api:SQLiteOpenHelper
public class MyOpenHelper extends SQLiteOpenHelper {//必须定义一个构造方法://arg1:数据库文件的名字//arg2:游标工厂//arg3:数据库版本public MyOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub}//数据库创建时,此方法会调用@Overridepublic void onCreate(SQLiteDatabase db) { //通常来说,创建数据库的同时,创建表 db.execSQL("create table person(_id integer primary key autoincrement, name char(10), salary char(20), phone integer(20))");}//数据库升级时,此方法会调用@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("数据库升级了");}}
创建数据库
//创建OpenHelper对象,这句话执行完不代表数据库已经建立,只是拿到对象而已,下个一句代码才是MyOpenHelper oh = new MyOpenHelper(getContext(), "person.db", null, 1);//获得数据库对象,如果数据库不存在,先创建数据库,后获得,如果存在,则直接获得SQLiteDatabase db = oh.getWritableDatabase();
- getWritableDatabase():打开可读写的数据库
- getReadableDatabase():在磁盘空间不足时打开只读数据库,否则打开可读写数据库
数据库的增删改查
SQL语句
//以下增删改查必须基于这个两句代码MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);SQLiteDatabase db = oh.getWritableDatabase();public void insert(){ 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(){ //arg1:SQL语句 //arg2:占位符数组 Cursor cursor = db.rawQuery("select name, salary from person", null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name"));//先通过获取列索引cursor.getColumnIndex("name")再获取列的值 String salary = cursor.getString(salary); System.out.println(name + ";" + salary); }}
使用api实现增删改查
插入
public void insertApi(){//把要插入的数据全部封装至ContentValues对象,以键值对的形式保存要存入数据库的数据ContentValues values = new ContentValues();values.put("name", "游天龙");values.put("phone", "15999");values.put("salary", 16000);//arg1:表名//arg2:这个参数不用管,默认传入空就好//arg3:数据库版本long i =db.insert("person", null, values);//返回值是改行的主键,如果出错返回-1}
- 删除
//arg1:表名//arg2:where条件,String类型//arg3:占位符填充数组public void deleteApi(){int i = db.delete("person", "name = ? and _id = ?", new String[]{"小志的儿子", "3"});//返回值是改行的主键,如果出错返回-1System.out.println(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);}
- 查询
//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
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; //加入这个,事务会失败 //设置 事务执行成功 db.setTransactionSuccessful();}finally{ //关闭事务,同时提交,如果已经设置事务执行成功,那么sql语句就生效了,反之,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")
public class MainActivity extends Activity { List<Person> personList; 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(); //db.query(String table, String[] columns, String selection, String[] selectionArgs,String groupBy, String having, String orderBy, String limit); 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); }}}
1 0
- SQLite数据库使用总结
- Qt数据库sqlite总结
- SQLite数据库总结
- android-SQLite数据库总结
- Qt数据库sqlite总结
- android SQLite数据库总结
- Qt数据库sqlite总结
- Qt数据库sqlite总结
- Qt数据库sqlite总结
- sqlite数据库总结
- SQLite数据库方法总结
- SQLite数据库总结一
- SQLite数据库总结二
- Qt数据库sqlite总结
- SQLite数据库总结
- SQlite数据库使用总结
- Qt数据库sqlite总结
- Qt数据库sqlite总结
- 110. Balanced Binary Tree
- Linux常用命令
- php设计模式 策略模式。
- 深度解析求结构体sizeof问题
- shell中条件判断if中的-z到-d的意思
- SQLite数据库总结
- 前端进阶-让你升级的网络知识
- bzoj 4537: [Hnoi2016]最小公倍数 分块
- static
- ajax写法
- Hadoop安装与启动(CDH5.6)
- 错误解决-----Tomcat环境变量配置完成,但是cmd输入startup无法启动
- PDO 指南
- 制作网页---CSS定位