从Android内存中读取SQLITE数据库

时间:2012-08-05 06:42:34

标签: android database sqlite sqliteopenhelper

我在Java应用程序上创建了一个sqlite数据库并将其推送到我的android。我想读这个数据库(也可能稍后写入)。到目前为止,我发现的大多数教程都在android中创建了数据库...

我认为我不需要覆盖SQLiteOpenHelper类的onCreate()和onUpdate()方法吗?也是他在下面的教程中创建表的部分,因为我已经有了我的数据库

你们可能知道的任何教程,示例代码?

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "contactsManager";

    // Contacts table name
    private static final String TABLE_CONTACTS = "contacts";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PH_NO + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }

我只需要打开我在内存中的数据库,执行一个sql查询agianst我的数据库并获得结果。

1 个答案:

答案 0 :(得分:4)

您必须先将数据从资产复制到设备上的应用程序存储区域(或其他可访问位置),然后才能使用它。您不能直接在.apk文件中使用它。

如何解决这个问题的一个例子:

public class DBAdapter { 

// DB info 
public static final String MAIN_DATABASE_NAME = "yourDB"; 
public static String MAIN_DB_PATH = "/data/data/your.package.name/databases/"; 
public static final int MAIN_DATABASE_VERSION = 1; 

// database control 
private DatabaseHelper mDbHelper; 
private static SQLiteDatabase mDb; 
private static Context mCtx; 

private static class DatabaseHelper extends SQLiteOpenHelper { 
    DatabaseHelper(Context context, String dbname, int dbversion) { 
        super(context, dbname, null, dbversion); 
        if (checkDataBase(dbname)) { 
            openDataBase(dbname); 
        } else { 
            try { 
                this.getReadableDatabase(); 
                copyDataBase(dbname); 
                this.close(); 
                openDataBase(dbname); 
            } catch (IOException e) { 
                throw new Error("Error copying database"); 
            } 
            Toast.makeText(context, 
                    "Initial " + dbname + " database has been created", 
                    Toast.LENGTH_LONG).show(); 
        } 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    } 
} 

public DBAdapter(Context ctx) { 
    DBAdapter.mCtx = ctx; 
} 

public DBAdapter open(String dbname, int dbversion) throws SQLException { 
    mDbHelper = new DatabaseHelper(mCtx, dbname, dbversion); 
    mDb = mDbHelper.getWritableDatabase(); 
    return this; 
} 

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

private static void copyDataBase(String dbname) throws IOException { 
    InputStream myInput = mCtx.getAssets().open(dbname); 
    String outFileName = MAIN_DB_PATH + dbname; 
    OutputStream myOutput = new FileOutputStream(outFileName); 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myInput.read(buffer)) > 0) { 
        myOutput.write(buffer, 0, length); 
    } 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 
} 

private static boolean checkDataBase(String dbname) { 
    SQLiteDatabase checkDB = null; 
    boolean exist = false; 
    try { 
        String db = MAIN_DB_PATH + dbname; 
        checkDB = SQLiteDatabase.openDatabase(db, null, 
                SQLiteDatabase.OPEN_READONLY); 
    } catch (SQLiteException e) { 
        Log.v("db log", "database does't exist"); 
    } 
    if (checkDB != null) { 
        exist = true; 
        checkDB.close(); 
    } 
    return exist; 
} 

private static void openDataBase(String dbname) throws SQLException { 
    String dbPath = MAIN_DB_PATH + dbname; 
    mDb = SQLiteDatabase.openDatabase(dbPath, null, 
            SQLiteDatabase.OPEN_READWRITE); 
} 
}