Jig*_*iya 22 sqlite android assets oneplustwo
我正在创建一个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();
}
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误.
09-21 18:03:56.841: E/SQLiteLog(7850): (1) no such table: tbl_player
Run Code Online (Sandbox Code Playgroud)
但是这个表存在于资产文件中.所以我使用这种方法从手机中获取数据库文件.
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
我发现获取的数据库文件中没有表.
注意:只发生在这个问题上OnePlus Two
和做工精细Nexus 4
,Htc 820
,Moto E
,Galxy S3
和Galaxy Quottro
数据库路径在不同设备中可能不同.您需要使用Context.getDatabasePath(String)
才能获取数据库路径.
例如
File backupDB = context.getDatabasePath(backupDBPath);
Run Code Online (Sandbox Code Playgroud)
经过长时间的尝试和搜索,我被迫假设 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);
}
}
Run Code Online (Sandbox Code Playgroud)
这对我有用
谢谢!
归档时间: |
|
查看次数: |
1207 次 |
最近记录: |