首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

SQLite数据库的施用

2012-06-27 
SQLite数据库的使用第一步:继承SQLiteOpenHelper?package cn.edu.serviceimport android.content.Context

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();}}}
?

热点排行