从资产访问数据库并从中获取数据

时间:2012-03-09 01:03:11

标签: android database

嘿伙计们我的问题是我的应用程序需要已经创建的外部数据库,我已经查看并找到了此解决方案http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ 这个副本数据库,但我想访问表和表中的数据来设置一些文本字段

      private static String DB_PATH = "/data/data/mill.to.want.who/databases/";
    private static String DB_NAME = "QuestionDatabase.dp";
      public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }
    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();
        SQLiteDatabase db_Read = null;
        if (dbExist) {          
        } else {
            db_Read=this.getReadableDatabase();
            db_Read.close();
            try {
                copyDataBase();

            } catch (IOException e) {
            }   }   }
    private void copyDataBase() throws IOException {
        // Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;
        // Open the empty db as the output stream
        OutputStream myOutput =   new FileOutputStream(outFileName);        

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }
           public void openDataBase() throws SQLException {
        // Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);}

感谢我挣扎了几个小时

1 个答案:

答案 0 :(得分:0)

我在处理android中的sqlite数据库时遵循了这篇文章。

http://www.vogella.de/articles/AndroidSQLite/article.html