2010-12-09 74 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; 
    } 
}