Java 我的数据库没有出现
我创建了下一个数据库:Java 我的数据库没有出现,java,android,Java,Android,我创建了下一个数据库:encuestadoSQLiteHelper encuestado=new encuestadoSQLiteHelper(这个“DBEncuestado”,null,1); final SQLiteDatabase db=encuestado.getWritableDatabase() 在这里: Intent pas = new Intent(encuesta.this, MainActivity.class); Toast.makeText
encuestadoSQLiteHelper encuestado=new encuestadoSQLiteHelper(这个“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);
}
您还可以在menifest文件中访问SD卡:
<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();
}
您是否尝试获取数据并检查其是否存在?是否以设备为根?可以放入Stetho,查看是否获得存储在SQLite DB中的数据。什么是获取数据。我不知道我的设备是否是根目录。为了将数据库从我的应用程序复制到另一个文件夹,我必须编写什么代码?因此,以下代码:
我必须在哪里编写它?在Menifest文件中。您的数据库名为DBEncuestado
。我的包名不起作用。它是package com.example.pablo.myapplication代码>,但我写了它,它说“表达式预期-重命名引用
”,我必须在其他类中这样做,或者我可以在我创建的类中这样做,即encuesta.java
?它可以在同一个类中,但请确保您有上下文
好的,兄弟,我要测试它。我有这样的代码:stringsql=“createtableencuestado(nombre TEXT)”;public encuestadoSQLiteHelper(上下文上下文、字符串名称、游标工厂、int版本){super(上下文、名称、工厂、版本);}@Override public void onCreate(SQLiteDatabase db){db.execSQL(sql);}
我的数据库名称是Encuestado
或db
或sql
?