[英]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.