編輯:高級開發
在手機系統領域中,谷歌的android操作系統算是一個新起之秀。但是其優秀的性能以及開源性,使其一經推出就伸手廣大用戶的好評。在這裡我們可以從android數據庫的相關操作來體驗這一系統給我們帶來的好處。
一個好的習慣是創建一個輔助類來簡化你的數據庫交互。
考慮創建一個數據庫適配器,來添加一個與數據庫交互的包裝層。它應該提供直觀的、強類型的方法,如添加、刪除和更新項目。數據庫適配器還應該處理查詢和對創建、打開和關閉數據庫的包裝。
它還常用靜態的android數據庫常量來定義表的名字、列的名字和列的索引。
下面的代碼片段顯示了一個標准數據庫適配器類的框架。它包括一個SQLiteOpenHelper類的擴展類,用於簡化打開、創建和更新android數據庫。
- import android.content.Context;
- import android.database.*;
- import android.database.sqlite.*;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.util.Log;
- public class MyDBAdapter
- {
- private static final String DATABASE_NAME = “myDatabase.db”;
- private static final String DATABASE_TABLE = “mainTable”;
- private static final int DATABASE_VERSION = 1;
- // The index (key) column name for use in where clauses.
- public static final String KEY_ID=”_id”;
- // The name and column index of each column in your database.
- public static final String KEY_NAME=”name”;
- public static final int NAME_COLUMN = 1;
- // TODO: Create public fIEld for each column in your table.
- // SQL Statement to create a new database.
- private static final String DATABASE_CREATE = “create table “ +
- DATABASE_TABLE + “ (“ + KEY_ID + “ integer primary key autoincrement, “ +
- KEY_NAME + “ text not null);”;
- // Variable to hold the database instance
- private SQLiteDatabase db;
- // Context of the application using the database.
- private final Context context;
- // Database open/upgrade helpe
- private myDbHelper dbHelper;
- public MyDBAdapter(Context _context) {
- context = _context;
- dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- public MyDBAdapter open() throws SQLException {
- db = dbHelper.getWritableDatabase();
- return this;
- }
- public void close() {
- db.close();
- }
- public long insertEntry(MyObject _myObject) {
- ContentValues contentValues = new ContentValues();
- // TODO fill in ContentValues to represent the new row
- return db.insert(DATABASE_TABLE, null, contentValues);
- }
- public boolean removeEntry(long _rowIndex) {
- return db.delete(DATABASE_TABLE, KEY_ID + “=” + _rowIndex, null) > 0;
- }
- public Cursor getAllEntrIEs () {
- return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME},
- null, null, null, null, null);
- }
- public MyObject getEntry(long _rowIndex) {
- MyObject objectInstance = new MyObject();
- // TODO Return a cursor to a row from the database and
- // use the values to populate an instance of MyObject
- return objectInstance;
- }
- public int updateEntry(long _rowIndex, MyObject _myObject) {
- String where = KEY_ID + “=” + _rowIndex;
- ContentValues contentValues = new ContentValues();
- // TODO fill in the ContentValue based on the new object
- return db.update(DATABASE_TABLE, contentValues, where, null);
- }
- private static class myDbHelper extends SQLiteOpenHelper
- {
- public myDbHelper(Context context, String name, CursorFactory factory,
int version) {- super(context, name, factory, version);
- }
- // Called when no database exists in
- // disk and the helper class needs
- // to create a new one.
- @Override
- public void onCreate(SQLiteDatabase _db) {
- _db.execSQL(DATABASE_CREATE);
- }
- // Called when there is a database version mismatch meaning that
- // the version of the database on disk needs to be upgraded to
- // the current version.
- @Override
- public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
int _newVersion) {- // Log the version upgrade.
- Log.w(“TaskDBAdapter”, “Upgrading from version “ +
- _oldVersion + “ to “ + _newVersion +
- “, which will destroy all old data”);
- // Upgrade the existing database to conform to the new version.
- // Multiple previous versions can be handled by comparing
- // _oldVersion and _newVersion values.
- // The simplest case is to drop the old table and create a
- // new one.
- _db.execSQL(“DROP TABLE IF EXISTS “ + DATABASE_TABLE);
- // Create a new one.
- onCreate(_db);
- }
- }
- }
android數據庫的相關操作就為大家介紹到這裡。
做出相應的處理,比如掛掉電話、比如靜音等等。 Demo 分析: 下面通過一個小DEMO 來講解一下廣播在android 中如何編寫,在Demo中我們設置了一個按鈕為
理解布局對於良好的android程序設計來說很重要。在這個教程中,你將學習到所有關於線性布局的東西,它在屏幕上垂直地或水平地組織用戶界面控件或者小工具。使用得當,線性布
編者按:在人們的印象中,Windows系統通常只是運行於系統顯示“我的電腦”上的磁盤(手機內存)擴展運用:以Windows 98作為例子,在進入到系統後,可以通過添加磁
android在中國的前景十分廣闊,首先是有成熟的消費者,android核心系統擁有速度快,可以在黑暗中拍照,支持谷歌語音搜索等強大的功能。隨後互聯網曝光了