編輯:Android開發實例
代碼如下:
public class DBOpenHelper extends SQLiteOpenHelper {
// 類沒有實例化,不能用作父類構造器的參數,必須聲明為靜態
public DBOpenHelper(Context context, int version) {
super(context, "SQLite.db", null, version);
// 第三個參數CursorFactory指定在執行查詢時獲得一個游標實例的工廠類,設置為null代表使用系統默認的游標工廠;
}
@Override
public void onCreate(SQLiteDatabase db) {// 數據庫第一次被創建的時候被調用,最開始創建數據庫時賦的版本號值是0
// SQLiteDatabase用於生成數據庫表;數據庫存放路徑:<package name>/databases/
db.execSQL("create table person(personid integer primary key autoincrement, name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// 版本號升級的時候進行調用;如果數據庫不存在就不調用
db.execSQL("alter table person add phone varchar(12) null");// 往表中增加一列
}
}
DBOpenHelper dbOpenHelper = new DBOpenHelper(getContext(), 1);
dbOpenHelper.getWritableDatabase();// 會create數據庫表or/open數據庫表
public class PersonService {
private DBOpenHelper dbOpenHelper;
public PersonService(Context context) {
super();
this.dbOpenHelper = new DBOpenHelper(context, 2);
}
public void save(Person person) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
// SQLiteDatabase db2 = dbOpenHelper.getWritableDatabase();有緩存的功能,同一個Helper用此方法得到的對象為db;
db.execSQL("insert into person(name,phone) values(?,?)", new Object[] {person.getName(), person.getPhone() });
}
public void delete(Integer id) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL("delete from person where personid=?", new Object[] { id });
}
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() });
}
public Person find(Integer id) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
// 如果數據庫磁盤空間未滿,得到的實例即為getWritableDatabase()得到的對象;因為此方法會調用getWritableDatabase方法;
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;
}
/*
* 獲取分頁數據
*/
public List getScollData(int offest, int maxResult) {
List persons = new ArrayList();
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from person order by personid asc limit ?,?",new String[] { String.valueOf(offest),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;
}
public long getCount() {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from person", null);
cursor.moveToFirst();
long result = cursor.getLong(0);
return result;
}
}
public class OtherPersonService {
private DBOpenHelper dbOpenHelper;
public OtherPersonService(Context context) {
super();
this.dbOpenHelper = new DBOpenHelper(context, 2);
}
public void save(Person person) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();// ContentValues 專門用於保存字段的值
values.put("name", person.getName());
values.put("phone", person.getPhone());
db.insert("person", null, values);// 第三個參數為字段值,第二個參數為空值字段,如果第三個參數為null
}
public void delete(Integer id) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete("person", "personid=?", new String[] { id.toString() });
}
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() });
}
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;
}
/*
* 獲取分頁數據
*/
public List getScollData(int offest, int maxResult) {
List persons = new ArrayList();
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("person", null, null, null, null, null,"personid asc", offest + "," + maxResult);
//db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
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;
}
public long getCount() {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("person", new String[] { "count(*)" }, null, null, null, null, null);
//db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
cursor.moveToFirst();
long result = cursor.getLong(0);
return result;
}
}
本文實例講述了Android編程之菜單實現方法。分享給大家供大家參考,具體如下: 菜單是許多應用程序不可或缺的一部分,Android中更是如此,所有搭載Andro
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
可以使用該套 SDK開發適用於Android系統移動設備的地圖應用,通過調用地圖SDK接口,您可以輕松訪問百度地圖服務和數據,構建功能豐富、交互性強的LBS(地圖