簡體   English   中英

我的數據庫沒有出現

[英]My database is not appearing

我創建了下一個數據庫: encuestadoSQLiteHelper encuestado = new encuestadoSQLiteHelper(this, "DBEncuestado", null, 1); final SQLiteDatabase db = encuestado.getWritableDatabase(); encuestadoSQLiteHelper encuestado = new encuestadoSQLiteHelper(this, "DBEncuestado", null, 1); final SQLiteDatabase db = encuestado.getWritableDatabase();

和這里:

Intent pas = new Intent(encuesta.this, MainActivity.class);
                Toast.makeText(context,"¡Encuesta enviada!",Toast.LENGTH_LONG).show();
                startActivity(pas);
                String nombre = "Pablo";
                db.execSQL("INSERT INTO Encuestado (nombre) " +
                        "VALUES ('" + nombre +"')");
                db.close();

未創建數據庫,因為我轉到documents / sdcard / data /,而我的應用程序包未出現。 我需要幫助,謝謝!

數據庫已創建,但用戶不可訪問數據庫。 只有創建此數據庫的應用程序可以訪問它。

如果要訪問數據庫,則有兩種方法,將手機掛起或編寫代碼以將數據庫從應用程序復制到另一個文件夾。

編輯:

將數據庫從代碼復制到SD卡

try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "/data/"+YOUR_PACKAGE_NAME+"/databases/"+DATABASE_NAME;
            String backupDBPath = "backdatabase.sqlite";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
     System.out.println("error in data base copy:"+e);
    }

而且,您在清單文件中也具有訪問SDCard的權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

您可以使用此代碼導出文件以在存儲中進行訪問

private void exportDB() throws Exception {
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                File sd = context.getExternalCacheDir();
                File data = Environment.getDataDirectory();
                FileChannel source = null;
                FileChannel destination = null;
                StringBuilder localPath = new StringBuilder();
                localPath.append(context.getExternalCacheDir().toString()).append("/");
                String currentDBPath = "/data/" + YOUR_PACKAGE_HERE + "/databases/" + DATABASE_NAME;
                String backupDBPath = DATABASE_NAME;
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);
                source = new FileInputStream(currentDB).getChannel();
                destination = new FileOutputStream(backupDB).getChannel();
                destination.transferFrom(source, 0, source.size());
                source.close();
                destination.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM