简体   繁体   English

我的数据库没有出现

[英]My database is not appearing

I created the next database: 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(); encuestadoSQLiteHelper encuestado = new encuestadoSQLiteHelper(this, "DBEncuestado", null, 1); final SQLiteDatabase db = encuestado.getWritableDatabase();

And here: 和这里:

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();

Database is not being created, because i go to documents/sdcard/data/ and my application package don't appears. 未创建数据库,因为我转到documents / sdcard / data /,而我的应用程序包未出现。 I need help, thanks! 我需要帮助,谢谢!

Database is created, but Database is not accessible for user. 数据库已创建,但用户不可访问数据库。 it is only accessible for app which created this database. 只有创建此数据库的应用程序可以访问它。

If you want to access the database then there are two ways, root your phone or write code to copy database from your application to another folder. 如果要访问数据库,则有两种方法,将手机挂起或编写代码以将数据库从应用程序复制到另一个文件夹。

EDIT: 编辑:

Copy database from code to Sdcard 将数据库从代码复制到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);
    }

And also you have permission in menifest file for accessing SDCard: 而且,您在清单文件中也具有访问SDCard的权限:

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

You can use this code to export the file to be accessible in your 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