使用嵌入式关系型SQLite数据库存储数据
package com.example.service; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper { public DBOpenHelper(Context context) { super(context, "example.db", null, 2);//在<包>/databases/下 } @Override public void onCreate(SQLiteDatabase db) {//数据库每次创建是被调用 db.execSQL("CREATE TABLE person (personid integer primary key autoincrement, name varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {//数据库版本发生变化是被调用 db.execSQL(" ALTER TABLE person ADD phone VARCHAR(12) NULL "); //往表中增加一列 // DROP TABLE IF EXISTS person 删除表 } }
下面代码完成增删改查等操作
package com.example.service; import java.util.ArrayList; import java.util.List; import com.example.domain.Person; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class PersonService { private DBOpenHelper dbOpenHelper; public PersonService(Context context) { this.dbOpenHelper = new DBOpenHelper(context); } /** * 新增记录 * @param person */ public void save(Person person){ SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); db.execSQL("insert into person(name, phone) values(?,?)", new Object[]{person.getName(),person.getPhone()}); } /** * 删除记录 * @param person */ public void delete(Integer id){ SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); db.execSQL("delete from person where personid=?", new Object[]{id}); } /** * 更新记录 * @param person */ public void update(Person person){ SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); db.execSQL("update person set name=? ,phone=? where personid=?", new Object[]{person.getName(),person.getPhone(),person.getId()}); } /** * 查找记录 * @param id 记录ID * @return */ public Person find(Integer id){ SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); Cursor cursor=db.rawQuery("select * from person where personid=?", new String[]{id.toString()}); if(cursor.moveToFirst()){ int personid=cursor.getInt(cursor.getColumnIndex("personid")); String name=cursor.getString(cursor.getColumnIndex("name")); String phone=cursor.getString(cursor.getColumnIndex("phone")); return new Person(personid, name, phone); } cursor.close(); return null; } /** * 分页获取记录 * @param offset 跳过前面多少条记录 * @param maxResult 每页显示多少条记录 * @return */ public List<Person> getScrollData(int offset,int maxResult){ SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); List<Person> persons=new ArrayList<Person>(); Cursor cursor=db.rawQuery("select * from person order by personid asc limit?,?", new String[]{String.valueOf(offset),String.valueOf(maxResult)}); while(cursor.moveToNext()){ int personid=cursor.getInt(cursor.getColumnIndex("personid")); String name=cursor.getString(cursor.getColumnIndex("name")); String phone=cursor.getString(cursor.getColumnIndex("phone")); persons.add(new Person(personid, name, phone)); } cursor.close(); return persons; } /** * 获取记录总条数 * @return */ public long getCount(){ SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); Cursor cursor=db.rawQuery("select count(*) from person ", null); cursor.moveToFirst(); long result=cursor.getLong(0); cursor.close(); return result; } }
也可以使用系统给的insert、delete、udate等方法实现增删改查
package com.example.service; import java.util.ArrayList; import java.util.List; import com.example.domain.Person; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class OtherPersonService { private DBOpenHelper dbOpenHelper; public OtherPersonService(Context context) { this.dbOpenHelper = new DBOpenHelper(context); } /** * 新增记录 * @param person */ public void save(Person person){ SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name", person.getName()); values.put("phone", person.getPhone()); db.insert("person", null, values); } /** * 删除记录 * @param person */ public void delete(Integer id){ SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); db.delete("person", "personid=?",new String[]{id.toString()} ); } /** * 更新记录 * @param person */ public void update(Person person){ SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name", person.getName()); values.put("phone", person.getPhone()); db.update("person", values, "personid=?", new String[]{person.getId().toString()}); } /** * 查找记录 * @param id 记录ID * @return */ public Person find(Integer id){ SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); Cursor cursor=db.query("person", null, "personid=?", new String[]{id.toString()}, null, null, null); if(cursor.moveToFirst()){ int personid=cursor.getInt(cursor.getColumnIndex("personid")); String name=cursor.getString(cursor.getColumnIndex("name")); String phone=cursor.getString(cursor.getColumnIndex("phone")); return new Person(personid, name, phone); } cursor.close(); return null; } /** * 分页获取记录 * @param offset 跳过前面多少条记录 * @param maxResult 每页显示多少条记录 * @return */ public List<Person> getScrollData(int offset,int maxResult){ SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); List<Person> persons=new ArrayList<Person>(); Cursor cursor=db.query("person", null, null, null,null,null, "personid asc", offset+","+maxResult); while(cursor.moveToNext()){ int personid=cursor.getInt(cursor.getColumnIndex("personid")); String name=cursor.getString(cursor.getColumnIndex("name")); String phone=cursor.getString(cursor.getColumnIndex("phone")); persons.add(new Person(personid, name, phone)); } cursor.close(); return persons; } /** * 获取记录总条数 * @return */ public long getCount(){ SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); Cursor cursor=db.query("person", new String[]{"count(*)"}, null, null, null, null, null); cursor.moveToFirst(); long result=cursor.getLong(0); cursor.close(); return result; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。