作者:pierce2502910693 | 来源:互联网 | 2023-01-25 11:03
Android中内置的是SQLite小型数据库,类似access一样是以文件方式存在的,操作起来十分方便。android操作SQLite需要继承自SQLiteOpenHelper来得到
Android中内置的是SQLite小型数据库,类似access一样是以文件方式存在的,操作起来十分方便。android操作SQLite需要继承自SQLiteOpenHelper 来得到连接。实现了2个方法:onCreate和onUpgrade。第一次获得对数据库的读或写操作的时候调用onCreate,所以这里面写创建数据表的语句;在数据库版本变化的时候会调用onUpgrade,所以如果表结构变化了就在这里修改。
下面的代码继承了SQLiteOpenHelper ,并且自己写了几个增删改查方法,将对数据库操作的DAO模块继承到一起,以后操作数据库用这一个java文件基本就OK啦。
[java] view plain copy
- package cn.itcast.service;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
-
- public class DBOpenHelper extends SQLiteOpenHelper {
- private static final String DATABASENAME = "test.db";
- private static final int DATABASEVERSION = 1;
-
- public DBOpenHelper(Context context) {
- super(context, DATABASENAME, null, DATABASEVERSION);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE person (personid integer primary key autoincrement, name varchar(20), amount integer)");
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS person");
- onCreate(db);
- }
-
- public void payment(){
- SQLiteDatabase db = getWritableDatabase();
- db.beginTransaction();
- try{
- db.execSQL("update person set amount=amount-10 where persOnid=?", new Object[]{1});
- db.execSQL("update person set amount=amount+10 where persOnid=?", new Object[]{2});
- db.setTransactionSuccessful();
- }finally{
- db.endTransaction();
- }
- }
-
- public void save(Person person){
-
- SQLiteDatabase db = getWritableDatabase();
- db.execSQL("insert into person (name,amount) values(?,?)",
- new Object[]{person.getName(),person.getAmount()});
- }
-
- public void update(Person person){
- SQLiteDatabase db = getWritableDatabase();
- db.execSQL("update person set name=? where persOnid=?",
- new Object[]{person.getName(),person.getId()});
- }
-
- public void delete(Integer id){
- SQLiteDatabase db = getWritableDatabase();
- db.execSQL("delete from person where persOnid=?", new Object[]{id.toString()});
- }
-
- public Person find(Integer id){
-
- SQLiteDatabase db = 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"));
- int amount = cursor.getInt(cursor.getColumnIndex("amount"));
- Person person = new Person(personid, name);
- person.setAmount(amount);
- return person;
- }
- return null;
- }
-
- public List getScrollData(Integer offset, Integer maxResult){
- List persons = new ArrayList();
- SQLiteDatabase db = getReadableDatabase();
- Cursor cursor = db.rawQuery("select * from person limit ?,?",
- new String[]{offset.toString(), maxResult.toString()});
- while(cursor.moveToNext()){
- int personid = cursor.getInt(cursor.getColumnIndex("personid"));
- String name = cursor.getString(cursor.getColumnIndex("name"));
- int amount = cursor.getInt(cursor.getColumnIndex("amount"));
- Person person = new Person(personid, name);
- person.setAmount(amount);
- persons.add(person);
- }
- cursor.close();
- return persons;
- }
-
- public Cursor getCursorScrollData(Integer offset, Integer maxResult){
- SQLiteDatabase db = getReadableDatabase();
- return db.rawQuery("select personid as _id, name, amount from person limit ?,?",
- new String[]{offset.toString(), maxResult.toString()});
- }
-
- public long getCount() {
- SQLiteDatabase db = getReadableDatabase();
- Cursor cursor = db.rawQuery("select count(*) from person", null);
- cursor.moveToFirst();
- return cursor.getLong(0);
- }
- }
涉及到的bean
[java] view plain copy
- class Person {
- private Integer id;
- private String name;
- private Integer amount;
-
- public Integer getAmount() {
- return amount;
- }
-
- public void setAmount(Integer amount) {
- this.amount = amount;
- }
-
- public Person(){}
-
- public Person(Integer id, String name) {
- this.id = id;
- this.name = name;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public String toString() {
- return "Person [amount=" + amount + ", id=" + id + ", name=" + name
- + "]";
- }
-
-
- }
摘自:http://www.apkbus.com/blog-58672-39951.html