2010-12-09 70 views

回答

6

擴展SQLiteOpenHelper類,像這樣:

private static class MyDbHelper extends SQLiteOpenHelper 
{ 

    public MyDbHelper(Context context, String description, CursorFactory factory, int version) 
    { 
     super(context, description, factory, version);   
    } 

    @Override 
    public void onCreate(SQLiteDatabase _db) 
    { 
     _db.execSQL(CREATE_TABLE_1); 
     _db.execSQL(CREATE_TABLE_2); 
     _db.execSQL(CREATE_TABLE_3); 
     ..etc 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) 
    { 
     // Log the version upgrade. 
     Log.w("MyDbAdapter", "Upgrading from version " + oldVersion + " to " + 
       newVersion + ", which will destroy all old data."); 

     _db.execSQL("DROP TABLE IF EXISTS " + TBL_ONE); 
     _db.execSQL("DROP TABLE IF EXISTS " + TBL_TWO); 
     _db.execSQL("DROP TABLE IF EXISTS " + TBL_THREE); 

     onCreate(_db);   
    } 

} 

創建使用dbhelper數據適配器類:

private SQLiteDatabase db; 
private MyDbHelper dbHelper; 

public MyDbAdapter(Context context) 
{  
    dbHelper = new MyDbHelper(context, DATABASE_NAME, null, DB_VERSION); 
} 


public MyDbAdapter open() throws SQLException 
{ 
    try 
    { 
     db = dbHelper.getWritableDatabase(); 
    } 
    catch (SQLiteException ex) 
    { 
     db = dbHelper.getReadableDatabase(); 
    } 

    return this; 
} 

public void close() 
{ 
    db.close(); 
} 

然後可以這樣使用:

public class ListsDAO 
{ 
private Context mContext; 
private MyDbAdapter db; 

public ListsDAO(Context context) 
{ 
    mContext = context; 
    db = new MyDbAdapter(mContext); 
} 

public List<MyObject> getAllObjects() 
{ 
    List<MyObject> objects = new ArrayList<MyObject>(); 

    db.open(); 
    Cursor cursor = db.getAllObjects();  

    if (cursor.moveToFirst()) 
    { 

...等 一旦你將光標放在行列表中,你可以遍歷第他們得到表格中的各列:

例如description = cursor.getString(descriptionColumn);

靜態字符串(如CREATE_TABLE_1)基本上是用於創建表的SQL語句。您可能還需要一個不太簡單的數據庫升級路線,而不是簡單地刪除所有表並重新創建它們。

0

該死!約翰史密斯有一個更完整和更快的答案!

Android附帶SQLite,它是一個非常輕的車載數據庫引擎。

你可以找到大量的資源和教程對Android開發者網站或其他地方在互聯網上,例如:

下面是一些定義一個類來乾淨地管理數據庫的代碼(部分從上面引用的記事本教程中複製過來)。
要打開數據庫,請實例化一個MyDb對象,調用它的open()方法。

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class MyDb { 
    private class MyDbHelper extends SQLiteOpenHelper { 
     MyDbHelper(Context context) { 
      super(context, "myfirstdatabase", null, 1); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      String createDbRequest = "CREATE TABLE .... "; // write the SQL query to create the first table 
      db.execSQL(createDbRequest); 
      // add other queries for more tables 
     } 
    } 

    private MyDbHelper mDbHelper; 
    private SQLiteDatabase mDb; 
    private Context mContext; 

    /** 
    * Constructor - takes the context to allow the database to be 
    * opened/created 
    * 
    * @param ctx the Context within which to work 
    */ 
    public MyDb(Context ctx) { 
     mContext = ctx; 
    } 

    /** 
    * Open the database. If it cannot be opened, try to create a new 
    * instance of the database. If it cannot be created, throw an exception to 
    * signal the failure 
    * 
    * @return this (self reference, allowing this to be chained in an 
    *   initialization call) 
    * @throws SQLException if the database could be neither opened or created 
    */ 
    public MyDb open() throws SQLException { 
     mDbHelper = new MyDbHelper(mContext); 
     mDb = mDbHelper.getWritableDatabase(); 
     return this; 
    } 
}