Android的SQLite数据库的操作

SQLite数据库
* 轻量级关系型数据库
* 创建数据库需要使用的api:SQLiteOpenHelper
创建SQLite数据库必须要继承SQLiteOpenHelper (superclass父类)
定义一个数据库的打开帮助器MyOpenHelper需要实现其三个方法:

public class MyOpenHelper extends SQLiteOpenHelper{
public  MyOpenHelper(Context context, String name,CursorFactory factory,int version){
  super(context,name,factory,version);     //构造函数  new 
}
//数据库一创建就会调用此方法
public  void  onCreate(SQLiteDatabase db) {
} 
//数据库升级时调用
public  void  onUpgrade(SQLiteDatabase db, Int   oldVersion , Int  newVersion ){
}
}

构造函数里面的参数的意思:
Parameters:
context : to use to open or create the database
name: of the database file, or null for an in-memory database //数据库的名字;
factory: to use for creating cursor objects, or null for the default //cursor 游标 在这里可以理解为resultset 结果集 一般使用默认的null
version: number of the database (starting at 1); if the database is older, onUpgrade will be used to upgrade the database; if the database is newer, onDowngrade will be used to downgrade the database //版本 if version<1 抛出IllegalArgumentException .参数异常
* 数据库被创建时会调用:onCreate方法
* 数据库升级时会调用:onUpgrade方法


数据库建立在测试单元中时,系统没有启用,context上下文对象不存在。安卓为了能满足能在测试状态下测试数据库的正确性虚拟了一个context上下文对象。在androidTestCase 中有一种获取虚拟上下文的方法 getContext();

public class TestCase extends AndroidTestCase {

    public void test() {
                   //getContext() 获取虚拟的上下文对象。
        MyOpenHelper  oh = new MyOpenHelper(getContext(), "mydb.db", null, 1);
        //oh.getReadableDatabase();
        //如果数据库不存在就创建数据库并获取可读可写的数据库对象,如果数据库存在就直接打开数据库。
         SQLiteDatabase db= oh.getWritableDatabase();
    }
}

getReadableDatabase与getWritableDatabase用法基本一样区别就是当(unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. 可读可写不能正常工作)
SQLiteOpenHelper
数据库对象创建在手机的内部存储空间内。data/data/包名文件夹/databases/可以通过FileExplorer文件浏览器查看


数据库创建好的同时实际当中我们的表单也就创建出来了,创建表的过程通常是在onCreate方法中创建出来的。
这里面需要注意的是数据库创建好之后就不会再调用onCreate
数据库增删改常用语句:
insert into tablename (字段名)values();
增加表中的行数据:
insert into person(name,salary,phone)values(“zhangsan”,15000,153623);
删除数据:
delete from person name=”zhangsan”where id=6;
修改数据:
update person set name=”lisi”where id=8;
查询数据:
select name ,salary from person ;
SQlite数据库的sql语句的增删该查:

import com.zh.sqlitedatabase.MyOpenHelper;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

public class TestCase extends AndroidTestCase {

    private MyOpenHelper oh;
    private SQLiteDatabase db;

    public void test() {
        MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
        // 如果数据库不存在就创建数据库,并获取可读可写的数据库对象,如果数据库存在就直接打开数据库。
        SQLiteDatabase db = oh.getWritableDatabase();

    }

    // 测试框架执行完毕,测试方法执行之前进行调用。
    protected void setUp() throws Exception {
        // TODO Auto-generated method stub
        super.setUp();
        oh = new MyOpenHelper(getContext(), "people.db", null, 1);
        db = oh.getWritableDatabase();
    }

    // 摧毁 此方法是在方法执行完毕后才调用
    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('张三',13000,139138)");

 db.execSQL("insert into person(name,salary,phone)values(?,?,?)", newObject[] { "小志","12000",159006750 });                           


        db.execSQL("insert  into person(name,salary,phone)values(?,?,?)", new Object[] { "小志的老婆", "25000", 1323 });

    }
     //改  update tablename set name= ? where id=? 修改id是xx将其名字设为xx.
    public void update() {

        db.execSQL("update person set name=?where _id=3 ", new Object[] { "小志的儿子" });
        db.execSQL("update person set name=? where name=?", new Object[] { "李冰冰", "小辉" });  //修改将小辉改为李冰冰。

    }

    public void delete() {

        // db.execSQL("delete from person where _id=3");
        // db.execSQL("delete from person where name=?", new Object[] { "小志" });
        db.execSQL("delete from person where name=? and salary= ?", new Object[] { "小辉", 25000 });
        db.execSQL("delete from person where name=?", new Object[] { "张三" });

    }

    public void select() {
     //注意查询时由于得到有返回值,db.execSQL()语句的返回值为空所以不能再用该方法。
        Cursor cusor = db.rawQuery("select name,salary from person", null);
        while (cusor.moveToNext()) {
            String name = cusor.getString(cusor.getColumnIndex("name"));
            String salary = cusor.getString(cusor.getColumnIndex("salary"));
            System.out.println(name + ";" + salary);

        }

    }
}

1、这里注意的是将myopenhelper提升为全局变量的方法:Ctrl+1 找到设置全局变量选项,(需要选中要提升的对象) (ctrl+1快速修复)
提升全局变量后会抛出空指针异常,原因是androidtextcase未加载的时候全局变量已经建立,getContex获取的上下文虚拟对象为空,source 找到androidtextcase的setup方法,进行从写(set up测试框架执行完毕,测试方法执行之前进行调用。)此时能保证得到上下文对象。 db.close 数据的关闭方法 teardown(摧毁 此方法是在方法执行完毕后才调用).
2、在mysql数据库增加表格中的记录insert into 其中into可以省略,但SQLite数据库不能省略,否则会报错。
3、注意占位符 new Object []{} 设置占位符的值。
4、SQLite数据库查询时,不能用执行sql语句的方法 用 db.rawQuery() 原始查询 得到一个Cursor对象,然后进行循环
cusor.moveToNext() getString(Index columnindex)
cusor.getColumnIndex(“name”)通过列索引获得列的值。


用Android封装好的api进行数据库的增删该查:
insert:增加记录

    public void insertApi(){
        //把要插入的数据放入到contenValues 对象当中
        ContentValues  values = new ContentValues();
        values.put("name", "张大辉");
        values.put("salary","19000");

    db.insert("person", null, values);  

    }

删除:delete
db.delete(table, whereClause, whereArgs) //删除的表名,删除条件,和条件的参数字符串数组

public void deleteApi(){

         int i =db.delete("person", "name=? and _id=?", new String[]{"李四","4"}); //删除id是4name李四
        System.out.println(i);

更改数据库中的数据:update
db.update(table, values, whereClause, whereArgs)参数以字符串数组形式表示
}

public void updateApi(){
//这里的values是改变后新的数据存放在contentvalues对象中 以键值对的形式存放
Contentvalues values= new Contentvalues();
values.put("name","王五")
//将表person中_id=4,name="李四"的记录更改为王五
db.update("person",values,"name=? and _id=?",new String[]{"李四","4"});


}

查询:query
db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)

Parameters:
table: The table name to compile the query against.
columns :A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn’t going to be used.
selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
groupBy A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.
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 How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
limit Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Returns:
A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.
db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)

public void selectApi(){
// (String)表名,  要查询的字段(String[]), 查询条件(String),查询条件的参数(String[]),,,orderBy 通过_id 进行desc降序排列,分页
Cursor  cursor= db.query("person",new String[]{"name","salary","_id"},"name=?",new String[]{"王五"},null,null,"_id desc",null);
    while(cursor.moveToNext())
    { 
      String name= cursor.getString(cursor.getColumnIndex("name"));
      String salary = cursor.getString(cursor.getColumnIndex("salary"));
      String id = cursor.getString(cursor.getColumnIndex("_id"));
      System.out.println(name+";"+salary+";"+_id);
   }
}

查询时得到的Cursor对象
cursor.moveToNext() boolean类型的 它的意思是
Move the cursor to the next row. 移动到下一行 这里的cursor 将相当于结果集。
This method will return false if the cursor is already past the last entry in the result set.


SQLite 也支持事物 Transaction:
事务

保证所有sql语句要么一起成功,要么一起失败
格式:
开启事务: db.beginTransaction();
设置事务成功: db.setTransactionSuccessful();
最后 finally 中 : db.endTransaction; 关闭事务 同时提交事务, 如果上面的设置事务执行成功,那么sql语句就会生效,否则Sql语句就会回滚。
通常情况下开启事务,和设置事务成功位于try语句块中,关闭事务位于finally语句块中。

public void transaction(){
  try{
    db.beginTransaction();
    ContentValues values = new ContentValues();
    values.put("salary","20000");
    db.update("person",values,"_id=?",new String[]{"8"});
    values.clear();   //这一步很重要将上面的数据清空,然后下面的数据进行在赋值。
    values.put("salary","18000");
    db.update("person",values,"name=?",new String[]{"王五"});
    db.setTransactionSuccessful();   //设置 事务成功  位于执行更改的最下边 注意此时还未提交事务
    }
    finally
     { //关闭事务,同时提交事务,如果上面的设置事务成功语句执行,那么sql语句就生效了,否则sql语句回滚。就不能正常执行了。
       db.endTransaction();
     }



}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值