//目錄結構
//編寫DbOpenHelper.java類,繼承SQLiteOpenHelper
- package sn.len.sqlite.service;
- import Android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DbOpenHelper extends SQLiteOpenHelper
- {
- private static final String DATABASENAME="database.db";
- private static final int DATABASVERSION=1;
- public DbOpenHelper(Context context)
- {
- //實例化父類,創建數據庫
- super(context, DATABASENAME, null, DATABASVERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db)
- {
- db.execSQL("create table sqlitetable(id integer primary key autoincrement,name varchar(20))");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
- {
-
- }
- /*
- *1.如果數據庫文件不存在,SQLiteOpenHelper在自動創建數據庫後會調用oncreate()方法,
- *在該方法中一般需要創建表、視圖等組件。在創建前數據庫一般是空的,因此不需要先刪除數據庫中相關的組件。
- *2.如果數據庫文件存在,並且當前版本號高於上次創建或升級的版本號,SQLiteOpenHelper會調用onUpdate()方法
- *調用該方法後會更新數據庫的版本號。在onupdate()方法中除了創建表、視圖等組件外,還需要先刪除這些相關的組件,
- *因此,在調用onupdate()方法前,數據庫是存在的,裡面還原許多數據庫組建。
- *詳細了解:請看:http://hi.baidu.com/%C8%F4%CB%AA/blog/item/3d839c132e80353bdc54017b.html
- * */
- }
編寫bean Person.java
- package sn.len.sqllite.domain;
-
- public class Person
- {
- private Integer id;
- private String 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;
- }
- public Person(Integer id, String name)
- {
- this.id = id;
- this.name = name;
- }
- public Person()
- {
-
- }
- @Override
- public String toString()
- {
- return "Person [id=" + id + ", name=" + name + "]";
- }
- }
//編寫具體實現功能的service層PersonServic.java
- package sn.len.sqlite.service;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import sn.len.sqllite.domain.Person;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
-
- public class PersonService
- {
- //拿到SQLiteDatabase對象
- private DbOpenHelper dbhelp;
- public PersonService(Context context)
- {
- this.dbhelp=new DbOpenHelper(context);
- }
- //保存
- public void save(Person person)
- {
- SQLiteDatabase dbopen=dbhelp.getWritableDatabase();
- dbopen.execSQL("insert into sqlitetable(name) values(?)", new Object[]{person.getName()});
- }
- //刪除
- public void del(Integer id)
- {
- SQLiteDatabase dbopen=dbhelp.getWritableDatabase();
- dbopen.execSQL("delete from sqlitetable where id=?", new Object[]{id});
- }
- //更新
- public void update(Person person)
- {
- SQLiteDatabase dbopen=dbhelp.getWritableDatabase();
- dbopen.execSQL("update sqlitetable set name=? where id=?", new Object[]{person.getName(),person.getId()});
- }
- //查找
- public Person select(Integer id)
- {
- SQLiteDatabase dbopen=dbhelp.getReadableDatabase();
- Cursor cursor=dbopen.rawQuery("select * from sqlitetable where id=?",new String[]{id.toString()});
- int db_id=cursor.getInt(cursor.getColumnIndex("id"));
- String name=cursor.getString(cursor.getColumnIndex("name"));
- return new Person(db_id,name);
- }
- //分頁
- public List<Person> getScrollData(Integer offset,Integer maxResult)
- {
- List<Person> p=new ArrayList<Person>();
- SQLiteDatabase dbopen=dbhelp.getReadableDatabase();
- Cursor cursor=dbopen.rawQuery("select * from sqlitetable limit ?,?", new String[]{offset.toString(),maxResult.toString()});
- while(cursor.moveToNext())
- {
- int id=cursor.getInt(cursor.getColumnIndex("id"));
- String name=cursor.getString(cursor.getColumnIndex("name"));
- Person persons=new Person(id,name);
- p.add(persons);
- }
- cursor.close();
- return p;
- }
- //得到總行數
- public long getCount()
- {
- SQLiteDatabase dbopen=dbhelp.getReadableDatabase();
- Cursor cusor=dbopen.rawQuery("select count(*) from sqlitetable", null);
- cusor.moveToFirst();//不用判斷,不管有沒能結果都會返回一條記錄
- long count=cusor.getLong(0);
- return count;
- }
-
- //操作數據,基本都用execSQL();
- //查詢數據,基本都用rawQuery();
- }