数据丢失后,我会从资产中复制数据库
这是android studio,可以从资产中复制数据库
错误是=没有这样的表:tbltexts(代码1 SQLITE_ERROR):,而在编译时:SELECT * FROM tbltexts
我的dbname = texts.db
此错误是在ANDROID PIE上
public class Databasehelper extends SQLiteOpenHelper {
public static String DB_PATH = "/data/data/com.nooshindroid.Rosary/databases/";
public static final String DATABASE_NAME = "texts.db";
private static final int DATABASE_VERSION = 1;
public static final String tblname ="tbltexts";
public static final String id="id";
public static final String arabic ="Arabic_text";
public static final String english ="English_text";
public static final String counter ="Counter";
public static final String goal ="Goal";
private Context context;
private SQLiteDatabase database;
public Databasehelper(Context context) {
super(context, DATABASE_NAME, null,DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void copyDataBase() throws IOException {
InputStream myInput = context.getAssets().open(DATABASE_NAME);
String outFileName = DB_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
}
public class DbHandler {
private SQLiteDatabase database;
private String name;
private Databasehelper dbHelper;
public DbHandler(Context context){
dbHelper=new Databasehelper(context);
}
public void open(){
database=dbHelper.getWritableDatabase();
}
public void close(){
database.close();
}
public String display(int row,int field){
Cursor cursor=database.query(dbHelper.tblname,null,null,null,null,null,null,null);
cursor.moveToPosition(row);
name=cursor.getString(field);
return name;
}
public boolean insert(String txtarab,String txtenglish,int goal) {
ContentValues cv = new ContentValues();
cv.put(dbHelper.arabic, txtarab);
cv.put(dbHelper.english, txtenglish);
cv.put(dbHelper.goal,goal);
long result = database.insert(dbHelper.tblname, null, cv);
database.close();
if (result == -1) {
return false;
} else {
return true;
}
}
public Integer count(){
Cursor cursor=database.query(dbHelper.tblname,null,null,null,null,null,null);
int s=cursor.getCount();
return s;
}
public void delete(int id){
database.delete(dbHelper.tblname,"id="+id,null);
}
public void update(String arab, String englsih, int id){
ContentValues contentValues=new ContentValues();
contentValues.put(dbHelper.arabic,arab);
contentValues.put(dbHelper.english,englsih);
database.update(dbHelper.tblname,contentValues,"id="+id,null);
}
public void counterupdate(int id,int counter){
ContentValues contentValues=new ContentValues();
contentValues.put(dbHelper.counter,counter);
database.update(dbHelper.tblname,contentValues,"id="+id,null);
}
}
答案 0 :(得分:2)
您不应该对数据库路径进行硬编码,没有保证它将是/data/data/<package_id>/databases
,例如,在我的手机(Android Pie,API 26)上,它是/data/user/<user_id>/<package_id>/databases
。该文档甚至警告说,路径可能会改变,因为应用程序可以移动到采用的存储中。
如果路径错误(并且捕获了copyDataBase()
的异常),您将得到SQLiteOpenHelper
自动创建的空数据库。
一种解决方案是替换:
String outFileName = DB_PATH + DATABASE_NAME;
与:
String outFileName = context.getDatabasePath(DATABASE_NAME);