sqlite 数据库的创建以及单元测试插入、删除、更改数据库信息

//创建数据库

package com.liyulei.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyOpenHelper extends SQLiteOpenHelper {

    public MyOpenHelper(Context context) {
        //传进来的上下文环境
        // 数据库文件的名字
        // 游标工厂,游标等同于结果集,传null使用默认工厂
        // 版本号,不能小于1,用于升级
        super(context, "people.db", null,1);
    }
    //创建数据库时调用
    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建表(_id主键)(autoincrement自增长)
        db.execSQL("create table people(_id integer primary key autoincrement,name char(10),phone char(20),salary integer(10))");
        //System.out.println("创建数据库");
    }
    //升级数据库时调用
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        System.out.println("升级数据库");
    }
}
//单元测试数据库,穿件表,插入、删除、更改表中信息

public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }

    private MyOpenHelper myOpenHelper ;
    private SQLiteDatabase db;

    //测试方法执行前调用
    @Override
    protected void setUp() throws Exception {
        myOpenHelper = new MyOpenHelper(getContext());
        db = myOpenHelper.getWritableDatabase();
    }
   /* //摧毁方法(落泪方法)
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        db.close();
    }*/

    public void test(){
        //获取虚拟上下文、数据库名字
        MyOpenHelper myOpenHelper = new MyOpenHelper(getContext());
        //如果数据库不存在,先创建再打开;如果存在,直接打开
        SQLiteDatabase db = myOpenHelper.getWritableDatabase();
    }
    public void insert() {
        db.execSQL("insert into people(name,phone,salary) values(?,?,?)",new Object[]{"小明","138765", 13000});
        db.execSQL("insert into people(name,phone,salary) values(?,?,?)",new Object[]{"小红","678393", 13130});
        db.execSQL("insert into people(name,phone,salary) values(?,?,?)",new Object[]{"小丽","344745", 43280});
        db.execSQL("insert into people(name,phone,salary) values(?,?,?)",new Object[]{"小朋","278393", 1430});
        db.execSQL("insert into people(name,phone,salary) values(?,?,?)",new Object[]{"小民","544745", 15380});
        db.close();
    }
    public void delete(){
        db.execSQL("delete from people where name=?",new Object[]{"小明"});
        db.close();
    }
    public void update(){
        db.execSQL("updata peole set salary=?where name=?",new Object[]{"100000","小红"});
        db.close();
    }
    public void select(){
        Cursor cursor = db.rawQuery("select name,phone from people ",null);
        //把指针移动到下一行
        while(cursor.moveToNext()){
            //先通过列名获取列索引
            String name = cursor.getString(Integer.parseInt(cursor.getColumnName(Integer.parseInt("name"))));
            String phone = cursor.getString(Integer.parseInt(cursor.getColumnName(Integer.parseInt("phone"))));
            System.out.println(name+";"+phone);
        }
    }
    public void insertApi(){
        ContentValues values = new ContentValues();
        values.put("name","小晓");
        values.put("phone","132443");
        values.put("salary","12342");
        db.insert("people",null,values);
    }
    public void deleteApi(){
        int i = db.delete("people","_id = ?",new String[]{"6"});
        System.out.println(i);
    }
    public void updataApi(){
        ContentValues values = new ContentValues();
        values.put("name","小民的汽车");
        int i = db.update("people", values ,"_id = ?",new String[]{"5"});
       System.out.println(i);
    }
    public void selectApi(){
        //arg1:查询的字段
        //arg1:查询的where条件
        //arg1:where条件的占位符
        //arg1:查询的字段
        Cursor cursor = db.query("people",null,null,null,null,null,null,null);
        while (cursor.moveToNext()){
            String name = cursor.getString(1);
            String phone = cursor.getString(2);
            String salary = cursor.getString(3);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值