SQLite数据库的使用
第一步:继承SQLiteOpenHelper
?
package cn.edu.service;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import cn.edu.database.MySqliteHelper;public class ProductService {private Context context;public ProductService(Context context){this.context=context;}public void save(){MySqliteHelper helper=new MySqliteHelper(context,"test.db",null,1);SQLiteDatabase db=helper.getWritableDatabase();db.execSQL("insert into product(name,time) values('12','12')");db.close();}public void delete(){MySqliteHelper helper=new MySqliteHelper(context,"test.db",null,1);SQLiteDatabase db=helper.getWritableDatabase();db.execSQL("delete product where id=1");db.close();}public void findById(Integer id){MySqliteHelper helper=new MySqliteHelper(context,"test.db",null,1);SQLiteDatabase db=helper.getWritableDatabase();Cursor cursor=db.rawQuery("select * from product where id=?", new String[] {id.toString()});if(cursor.moveToFirst()){System.out.println(cursor.getInt(cursor.getColumnIndex("id")));System.out.println(cursor.getInt(cursor.getColumnIndex("name")));System.out.println(cursor.getInt(cursor.getColumnIndex("time")));}}public void update(){MySqliteHelper helper=new MySqliteHelper(context,"test.db",null,1);SQLiteDatabase db=helper.getWritableDatabase();db.execSQL("update product set name='何' where id=1");db.close();}public void transaction(){MySqliteHelper helper=new MySqliteHelper(context,"test.db",null,1);SQLiteDatabase db =helper.getWritableDatabase();db.beginTransaction();try{db.execSQL("");db.execSQL("");db.setTransactionSuccessful();//设置数万的标志为true}finally{db.endTransaction();}}}?