我正在开发一个使用SQlite数据库的应用程序。我的应用程序在每个操作系统版本和所有模拟器中运行良好,但它只会在操作系统版本2.3.3 的设备中产生问题。我收到CREATE TABLE android_metadata消息,我的数据库代码崩溃。
我正在使用Google Nexus和Android OS 2.3.3
请告诉我实际问题是什么以及如何解决此设备相关问题。我也在下面发布我的代码。:
public class ECatalogueDatabase {
private static final String DB_PATH = "/data/data/com.weg.ecatalogue/databases/";
public static final String DATABASE_NAME = "ECatalogue";
public static final String DATABASE_TABLE = "T_Electrical";
public static final int DATABASE_VERSION = 1;
public static final String KEY_ROWID="id";
public static final String KEY_PRODUCT_LINE="productline";
public static final String KEY_VOLTAGE="voltage";
public static final String KEY_OUTPUTHP="outputhp";
public static final String KEY_FRAME="frame";
public static final String KEY_RPM="rpm";
private Context context=null;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
/**
* Database creation sql statement
*/
private static final String CREAT_DATABASE="Create Table if not exists "+ DATABASE_TABLE+"("+ KEY_ROWID +" INTEGER PRIMARY KEY NOT NULL,"
+KEY_PRODUCT_LINE +" nvarchar ,"+ KEY_OUTPUTHP+" numeric ,"+ KEY_RPM +" nvarchar ,"+KEY_VOLTAGE +" nvarchar ," +KEY_FRAME +" nvarchar"+")";
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx the Context within which to work
*/
public ECatalogueDatabase(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
//Helper class
private static class DatabaseHelper extends SQLiteOpenHelper
{
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREAT_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
public ECatalogueDatabase open() //throws SQLException
{
try
{
db=DBHelper.getWritableDatabase();
}catch(Exception exception)
{
exception.printStackTrace();
}
return null;
}
public void close()
{
DBHelper.close();
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
@SuppressWarnings("unused")
boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null;
if(dbExist){
//DO NOTHING IN THIS CASE
}else{
db_Read = DBHelper.getReadableDatabase();
db_Read.close();
}
//=================================
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
try{
String myPath = DB_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(db != null){
db.close();
}
return db != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
InputStream myInput = context.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
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);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public int getDatabaseCount(){
int count = 0;
Cursor cursor = db.rawQuery("Select * from " + DATABASE_TABLE, null);
if(cursor!=null){
count = cursor.getCount();
}
cursor.deactivate();
cursor.close();
return count;
}
}
我的记录:
5CREATE TABLE android_metadata failed
Failed to setLocale() when constructing, closing the database
a
at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java1987)
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java1855)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java820)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java854)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java847)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java567)
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java203)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java118)
at com.weg.ecatalogue.database.ECatalogueDatabase.open(ECatalogueDatabase.java76)
at com.weg.ecatalogue.HomeScreen.onCreate(HomeScreen.java219)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java1722)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java1784)
at android.app.ActivityThread.access$00(ActivityThread.java123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java939)
at android.os.Handler.dispatchMessage(Handler.java99)
at android.os.Looper.loop(Looper.java123)
at android.app.ActivityThread.main(ActivityThread.java3839)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java599)
at dalvik.system.NativeStart.main(Native Method)
Deleting and re-creating corrupt database /data/data/com.weg.ecatalogus/ECatalogue
a
at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java1987)
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java1855)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java820)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java854)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java847)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java567)
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java203)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java118)
at com.weg.ecatalogue.database.ECatalogueDatabase.open(ECatalogueDatabase.java76)
at com.weg.ecatalogue.HomeScreen.onCreate(HomeScreen.java219)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java1722)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java1784)
at android.app.ActivityThread.access$00(ActivityThread.java123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java939)
at android.os.Handler.dispatchMessage(Handler.java99)
at android.os.Looper.loop(Looper.java123)
at android.app.ActivityThread.main(ActivityThread.java3839)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java599)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:8)
这是完全不同的问题,我只在少数设备如HTC Nexus ONE设备中得到它并且它适用于所有操作系统和所有其他设备。
当我们创建数据库时,我们得到一个自动生成的名为“android_table”的表,我删除了该表并在我的SQLite管理器中手动重新创建了该表。通过以下两个查询步骤:
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')
和
INSERT INTO "android_metadata" VALUES ('en_US')
在我运行代码的这一步之后,我在圣诞节前得到了惊喜。我的申请现在工作正常。
所有功劳归功于我的长期R&amp; D和经过这么多努力后得到的这个链接: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
答案 1 :(得分:1)
尝试使用:
SQLiteDatabase.openDatabase(your_path, null, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);