2012-08-05 74 views
3

我已經在Java應用程序上創建了一個sqlite數據庫並將它推送到了我的android。我想讀這個數據庫(也許以後再寫入)。目前爲止我發現的大多數教程都是在android中創建數據庫...從Android內存中讀取SQLITE數據庫

我不認爲我需要重寫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查詢並獲得結果。

回答

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); 
} 
} 
+0

這正是我需要的。幾個問題,因爲我是Android和sqlite的新手。爲什麼我需要copyDataBase方法?它是否在數據庫不可寫或什麼的情況下?另外我不確定數據庫的版本?我一直認爲它是1,但是非常感謝。 – Achilles 2012-08-05 07:23:46

+0

'copyDataBase'方法將數據庫從應用程序文件中取出,並將其存入您的應用程序數據庫目錄,您可以在其中實際使用它。更改架構(或內容)時更改版本號。如果你使用它並把代碼放在'onUpgrade'方法中,你可以更新你的數據庫。 – Barak 2012-08-05 07:37:04

相關問題