我正在开发一个包含100个元素的数据库的小型应用程序。我导入数据库,但只有一个模拟器(我有3个女巫)可以正确运行。我发现它运行起来没有问题,因为“ Songs.db”数据库存在于data / data / myapppackage / databases /文件夹中,而如果不生根设备,我将无法访问它。
我通过互联网搜索解决此问题的不同方法和解决方案,但是没有任何效果。我是android编程的新手,针对这种问题,没有任何教程。
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Songs.db";
public static final String TABLE_NAME = "songs_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "TITLE";
public DatabaseHelper (Context context) {
super( context, DATABASE_NAME, null, 1 );
SQLiteDatabase db = this.getWritableDatabase();
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select TITLE from songs_table where ID="+id+"", null );
return res;
}
}
以及PlayerTurn类
myDb = new DatabaseHelper( this );
Cursor rs = db.getData( b );
rs.moveToFirst();
tit = rs.getString( rs.getColumnIndex( db.COL_2 ) );
我大多数时候收到的错误消息是android.database.sqlite.SQLiteException: no such table: songs_table (code 1):
谁能帮我?我花了将近15个小时...
答案 0 :(得分:0)
您可以将数据库复制到SD卡中,从SD卡中,您始终可以访问数据库
尝试以下代码:
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "data/"+sPackageName+"/databases/"+sDBName;
String backupDBPath = "/.appname-external-data-cache/"+sDBName; //"{database name}";
File dir = new File(sd,backupDBPath.replace(sDBName,""));
if(dir.mkdir()) {
}
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) {
}