[英]Database not getting copied properly in OnePlus Two
我正在創建一個Android應用程序,我正在使用sqlite數據庫。 因為我已經在項目的資產文件夾中放置了一個sqlite文件,我在使用下面的代碼首次執行應用程序時將此文件復制到手機中。
private void copyDataBase() throws IOException {
new File(DB_PATH).mkdirs();
InputStream myInput = appContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
但我得到了這個錯誤。
09-21 18:03:56.841: E/SQLiteLog(7850): (1) no such table: tbl_player
但是這個表存在於資產文件中。 所以我使用這種方法從手機中獲取數據庫文件。
public static void exportDB(String databaseName, Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + context.getPackageName()
+ "//databases//" + databaseName + "";
String backupDBPath = "sensor_game.db";
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) {
}
}
我發現獲取的數據庫文件中沒有表。
注意:此問題僅在OnePlus Two
發生,並且在Nexus 4
, Htc 820
, Moto E
, Galaxy Quottro
Galxy S3
和Galaxy Quottro
數據庫路徑在不同設備中可能不同。 您需要使用Context.getDatabasePath(String)
才能獲取數據庫路徑。
例如
File backupDB = context.getDatabasePath(backupDBPath);
經過長時間的嘗試和搜索后,我不得不假設OP2制造中應該有一個錯誤,因為它在所有其他設備中都能正常工作。
我改變了使用查詢創建數據庫的方法,而不是從資產中復制數據庫文件。 像下面的代碼
import java.io.File;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper implements DBConstants {
private static MySQLiteHelper mInstance = null;
private SQLiteDatabase myDataBase;
private static String DB_PATH = "";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
try {
if (checkDataBase())
openDataBase();
else
myDataBase = this.getReadableDatabase();
} catch (Exception e) {
}
}
public static MySQLiteHelper instance(Context context) {
File outFile = context.getDatabasePath(DATABASE_NAME);
DB_PATH = outFile.getPath();
if (mInstance == null) {
mInstance = new MySQLiteHelper(context);
}
return mInstance;
}
private void openDataBase() throws Exception {
try {
myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLException e) {
// TODO: handle exception
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DB_PATH, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
/** database does't exist yet. */
} catch (Exception e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v("log_tag", "onCreate");
myDataBase = db;
// creating a sample table
String CREATE_DEVICE_TABLE = "CREATE TABLE " + DEVICE + " ("
+ KEY_DEVICEID + " TEXT, " + KEY_OPERATOR + " TEXT, "
+ KEY_DEVICENAME + " TEXT, " + KEY_DEVICETOTALMEMORY
+ " INTEGER, " + KEY_SCREENWIDTH + " INTEGER, "
+ KEY_SCREENHEIGHT + " INTEGER, " + KEY_OPERATINGSYSTEM
+ " TEXT)";
db.execSQL(CREATE_DEVICE_TABLE);
// other tables also can be created from here.
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DEVICE);
// Create tables again (as per requirement)
this.onCreate(db);
}
public Cursor rawQuery(String qry) {
return myDataBase.rawQuery(qry, null);
}
public long insert(String tableName, ContentValues cv) {
return myDataBase.insert(tableName, null, cv);
}
public void insertWithOnConflict(String tableName, ContentValues cv,
int flag) {
myDataBase.insertWithOnConflict(tableName, null, cv, flag);
}
public long update(String tableName, ContentValues cv, String whereClose) {
return myDataBase.update(tableName, cv, whereClose, null);
}
public int deleteData(String table_name, String whereClause) {
return (int) myDataBase.delete(table_name, whereClause, null);
}
}
它對我有用
謝謝!
我找到了解決方案,我使用了以下功能:
public void createDb() {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
// call close() for properly copy database file
this.getReadableDatabase().close();
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Error copying database");
}
}
}
根據這篇文章,我們需要調用close()
,你也可以將數據庫推送到Oneplus兩個設備。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.