Dee*_*pzz 5 sqlite android assets
我的文件assets夹中有一个.db文件.我已将其复制到data/data/<packagename>/databases/模拟器中的文件夹中,并且工作正常.
但是当我在设备上运行时,它会强行关闭.它正在显示
SQLite exception: no such table: tbl_user
Run Code Online (Sandbox Code Playgroud)
这是我的代码..
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper() {
super(dataContext, DATABASE_NAME, null, 1);
DB_PATH = "/data/data/"
+ dataContext.getApplicationContext().getPackageName()
+ "/databases/";
Log.d("PATH", DB_PATH);
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.d("Error", e.toString());
}
}
}
private void copyDataBase() throws IOException {
// TODO Auto-generated method stub
InputStream inFile = dataContext.getAssets().open(DATABASE_NAME);
String outFileName = DB_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = inFile.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
inFile.close();
}
private boolean checkDataBase() {
// TODO Auto-generated method stub
File dbFile = new File(DB_PATH + DATABASE_NAME);
return dbFile.exists();
}
Run Code Online (Sandbox Code Playgroud)
我是否应该做其他事情将该数据库复制到设备?
谢谢..
得到了答案...... :)
从这里..
这是版本2.3.6的问题...它正在与其他设备一起工作......只需添加三行来解决问题......
boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null;
if (!dbExist)
{
db_Read = this.getReadableDatabase();
db_Read.close();
try
{
copyDataBase();
}
catch (IOException e)
{
Log.d("Error", e.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1665 次 |
| 最近记录: |