数据库未在 OnePlus Two 中正确复制
Database not getting copied properly in OnePlus Two
我正在创建一个 Android 应用程序,我在其中使用了 sqlite 数据库。为此,我在项目的资产文件夹中放置了一个 sqlite 文件,并且在我第一次使用下面的代码执行应用程序时将此文件复制到 phone。
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
但是这个 table 存在于资产文件中。所以我使用这种方法从 phone 中获取了数据库文件。
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) {
}
}
我发现在获取的数据库文件中没有table。
注意:此问题仅在 OnePlus Two
中出现,在 Nexus 4
、Htc 820
、Moto E
、Galxy S3
和 [=18= 中工作正常]
不同设备的数据库路径可能不同。
您需要使用 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 两个设备。
我正在创建一个 Android 应用程序,我在其中使用了 sqlite 数据库。为此,我在项目的资产文件夹中放置了一个 sqlite 文件,并且在我第一次使用下面的代码执行应用程序时将此文件复制到 phone。
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
但是这个 table 存在于资产文件中。所以我使用这种方法从 phone 中获取了数据库文件。
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) {
}
}
我发现在获取的数据库文件中没有table。
注意:此问题仅在 OnePlus Two
中出现,在 Nexus 4
、Htc 820
、Moto E
、Galxy S3
和 [=18= 中工作正常]
不同设备的数据库路径可能不同。
您需要使用 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 两个设备。