一、首先写一个类继承sqliteOpenHelper类
重写他的方法指定db的名称、版本,重写oncreat和onUpgrade方法,写SQL语句创建表
public class MysqLiteOpenhelper extends sqliteOpenHelper { private static String name = "person.db"; private static int version = 1; public MysqLiteOpenhelper(Context context){ super(context,name,null,version); } /* *数据库第一次被创建时调用的方法 *db是被创建的数据库 */ @OverrIDe public voID onCreate(sqliteDatabase db) { db.execsql("create table person(ID integer primary key autoincrement,name varchar(20),number varchar(20) )"); } /*当数据库版本更新时调用此方法*/ @OverrIDepublic voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { }
二、创建一个DAO类对外提供增删改查接口
其中执行增删改查的方法可以用SQL语句也可以使用系统给出的API,下面的代码中把两种方法都写了出来
public class Persondao { private MysqLiteOpenhelper helper; public Persondao(){ } public Persondao(Context context){ helper = new MysqLiteOpenhelper(context); } public voID add(String name,String number){ sqliteDatabase db = helper.getWritableDatabase(); db.execsql("insert into person (name,number)values(?,?)",new Object[]{name,number}); /*ContentValues values = new ContentValues(); values.put("number",number); values.put("name",name); long ID = db.insert("Person",values);*/ db.close(); } public boolean find(String name){ sqliteDatabase db = helper.getWritableDatabase(); //Cursor cursor = db.rawquery("select *from person where name=?",new String[]{name}); Cursor cursor = db.query("person","name=?",new String[]{name},null); boolean result =cursor.movetoNext(); cursor.close(); db.close(); return result; } public int update(String name,String newnumber){ sqliteDatabase db = helper.getWritableDatabase(); //db.execsql("update person set number=? where name=?",new Object[]{newnumber,name}); ContentValues values = new ContentValues(); values.put("number",newnumber); int number = db.update("person",values,new String[]{newnumber}); db.close(); return number; } public int delet(String name){ sqliteDatabase db = helper.getWritableDatabase(); //db.execsql("delete from person where name=?",new String[]{name}); int number = db.delete("person",new String[]{name}); db.close(); return number; } public List<Person> findAll(){ List<Person> persons = new ArrayList<Person>(); sqliteDatabase db = helper.getWritableDatabase(); //Cursor cursor = db.rawquery("select *from person",null); Cursor cursor = db.query("person",new String[]{"ID","name","number"},null); while (cursor.movetoNext()) { int ID = cursor.getInt(cursor.getColumnIndex("ID")); String name = cursor.getString(cursor.getColumnIndex("name")); String number = cursor.getString(cursor.getColumnIndex("number")); Person p = new Person(); persons.add(p); } db.close(); cursor.close(); return persons; }}
三、增删改查 *** 作
import androID.content.ContentValues;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper; public class TodoDB extends sqliteOpenHelper { private final static String DATABASE_name = "todo_db"; private final static int DATABASE_VERSION = 1; private final static String table_name = "todo_table"; public final static String FIELD_ID = "_ID"; public final static String FIELD_TEXT = "todo_text"; public TodoDB(Context context) { super(context,DATABASE_name,DATABASE_VERSION); } @OverrIDe public voID onCreate(sqliteDatabase db) { /* 建立table */ String sql = "CREATE table " + table_name + " (" + FIELD_ID + " INTEGER primary key autoincrement," + " " + FIELD_TEXT + " text)"; db.execsql(sql); } @OverrIDe public voID onUpgrade(sqliteDatabase db,int newVersion) { String sql = "DROP table IF EXISTS " + table_name; db.execsql(sql); onCreate(db); } public Cursor select() { sqliteDatabase db = this.getReadableDatabase(); Cursor cursor = db .query(table_name,null); return cursor; } public long insert(String text) { sqliteDatabase db = this.getWritableDatabase(); /* 将新增的值放入ContentValues */ ContentValues cv = new ContentValues(); cv.put(FIELD_TEXT,text); long row = db.insert(table_name,cv); return row; } public voID delete(int ID) { sqliteDatabase db = this.getWritableDatabase(); String where = FIELD_ID + " = ?"; String[] whereValue = { Integer.toString(ID) }; db.delete(table_name,where,whereValue); } public voID update(int ID,String text) { sqliteDatabase db = this.getWritableDatabase(); String where = FIELD_ID + " = ?"; String[] whereValue = { Integer.toString(ID) }; /* 将修改的值放入ContentValues */ ContentValues cv = new ContentValues(); cv.put(FIELD_TEXT,text); db.update(table_name,cv,whereValue); }}
四、写一个JavaBean设置他的get、set方法
public class Person { private int ID; private String name; private String number; public Person(){ } public Person(int ID,String name,String number) { this.ID = ID; this.name = name; this.number = number; } public int getID() { return ID; } public voID setID(int ID) { this.ID = ID; } public String getname() { return name; } public voID setname(String name) { this.name = name; } public String getNumber() { return number; } public voID setNumber(String number) { this.number = number; }}
五、mainactivity中使用它
private sqliteOpenHelper helper;Persondao dao = new Persondao();helper = new MysqLiteOpenhelper(this);helper.getWritableDatabase(); sqliteDatabase db = helper.getWritableDatabase();
六、关于数据库的事务处理
androID开发中数据库的 *** 作非常慢,将所有 *** 作打包成一个事务能够大大的提高处理速度,其中最重要的是保证了数据的一致性,让事务中的所有 *** 作都能成功执行,或者失败,或者这所有 *** 作都回滚。
sqliteDatabase db = helper.getWritableDatabase();db.beginTransaction();try{ //在这里执行多个数据库 *** 作,执行过程中可能会抛出异常 db.execsql("update person set number=? where name=?",new Object[]{"1",jacky}); db.execsql("update person set number=? where name=?",new Object[]{"2","sunny"}); db.setTransactionSuccessful();}catch{ //捕获异常 throw e;}finally{ //所有 *** 作完成结束一个事务 db.endTransaction(); db.close;}总结
以上是内存溢出为你收集整理的Android应用中内嵌SQLite数据库的基本 *** 作指南全部内容,希望文章能够帮你解决Android应用中内嵌SQLite数据库的基本 *** 作指南所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)