Android_03_数据库的操作

来源:转载

MyOpenHelper.java

 

package com.itheima.sqlitedatabase;import java.sql.ResultSet;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class MyOpenHelper extends SQLiteOpenHelper { public MyOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub }
 //数据库创建时,此方法会调用
 //version的值必须大于或等于, @Override public void onCreate(SQLiteDatabase db) { db.execSQL(create table person(_id integer primary key autoincrement, name char(10), salary char(20), phone integer(20))); }
 //当version的值变得更大时,此时数据库就会升级 //数据库升级时,此方法会调用 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println(数据库升级了); } }

TestCase.java

 

 

package com.itheima.sqlitedatabase.test;import com.itheima.sqlitedatabase.MyOpenHelper;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.test.AndroidTestCase;public class TestCase extends AndroidTestCase { //这里提供了两种方法:一种是自己手动写SQL语句,一种是通过调用系统的API //此时测试框架还没有初始化完毕,没有虚拟上下文context对象// 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); } 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-finally是为了保证不管事务是否执行成功,最后都必须保证事务关闭 try{ //事务的作用是保证在开启事务和关闭事务之间的代码要么全部执行,要么回滚,相当于都没执行 //开启事务 db.beginTransaction(); ContentValues values = new ContentValues(); values.put(salary, 12000); db.update(person, values, name = ?, new String[]{小志}); //clear()的作用是清空values里边的所有数据 values.clear(); values.put(salary, 16000); db.update(person, values, name = ?, new String[]{小志的儿子}); //设置 事务执行成功 db.setTransactionSuccessful(); } finally{ //关闭事务,同时提交,如果已经设置事务执行成功,那么sql语句就生效了,反之,sql语句回滚 db.endTransaction(); } }}

 

注:在这里我们是用单元测试框架来做的测试,所以在清单文件中,记得加上两条属性:

android:name=android.test.InstrumentationTestRunner
android:targetPackage=com.itheima.sqlitedatabase
>

 

 

注:在这里,我们在MyOpenHelper.java文件中仅仅创建了一张空的数据库

在测试单元TestCase.java中才进行增删改查数据库的操作

 


分享给朋友:
您可能感兴趣的文章:
随机阅读: