2
我在三星設備上遇到了一些麻煩。在我的應用程序中,每當我上傳市場上的新版本時,我想更換數據庫...但在嘗試保存一些數據之前。該代碼可以在ADV和許多設備上正常工作,但Samsung Galaxy(S系列,標籤)等設備除外。三星設備上的數據庫錯誤
我已經嘗試做this機會this.getReadableDatabase()this.getWritableDatabase()但沒有任何更改。
我還檢查了這個類似的帖子Failed to create SQLite DB in my Samsung y duos device in Android?
有人能幫助我嗎?下面是基本的代碼和我嘗試
...
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
SQLiteDatabase db = null;
if(dbExist){
}else{
db = this.getReadableDatabase();
db.close();
try {
copyDataBase();
Log.e(TAG, "createDatabase database created");
} catch (IOException e) {
throw new Error("Erro ao copiar banco");
}
}
}
/*
Both file or a database check works
*/
private boolean checkDataBase(){
File checkDB = new File (Bd_path + Bd_name);
return checkDB.exists();
/*SQLiteDatabase checkDB = null;
try {
String myPath = Bd_path + Bd_name;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;*/
}
...
第一次嘗試 - 做工精細,除了三星設備
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
SQLiteDatabase db = null;
if(dbExist){
try {
openDataBase();
Cursor resul = bancoDados.rawQuery("Select ativo from configuracao", null);
resul.moveToFirst();
if (Integer.valueOf(resul.getString(resul.getColumnIndex("ativo"))) <= 1) {
copyDataBase();
Log.v("Data", "updated");
bancoDados.execSQL("UPDATE configuracao SET ativo = '2'");
}
resul.close();
bancoDados.close();
}catch (Exception e) {
e.printStackTrace();
}
...
第二個嘗試 - 做工精細,除了三星設備
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
SQLiteDatabase db = null;
if(dbExist){
try {
db = this.getReadableDatabase();
Cursor resul = db.rawQuery("Select ativo from configuracao", null);
resul.moveToFirst();
if (Integer.valueOf(resul.getString(resul.getColumnIndex("ativo"))) <= 1) {
copyDataBase();
Log.v("Data", "updated");
db.execSQL("UPDATE configuracao SET ativo = '2'");
}
resul.close();
db.close();
}catch (Exception e) {
e.printStackTrace();
}
...
我得到了當設備試圖讀取我的數據庫時發生流動錯誤
android.database.sqlite.SQLiteException: no such table: configuracao: , while compiling: Select ativo from configuracao
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:110)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:71)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
at com.ead.louvemosfinal.Dados_banco.createDataBase(Dados_banco.java:47)
at com.ead.louvemosfinal.DataAdapter.createDatabase(DataAdapter.java:31)
at com.ead.louvemosfinal.Inicio.construtor(Inicio.java:116)
at com.ead.louvemosfinal.Inicio.onCreateView(Inicio.java:97)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:279)
at android.view.View.dispatchAttachedToWindow(View.java:6177)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1266)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1271)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1271)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1271)
at android.view.ViewRoot.performTraversals(ViewRoot.java:771)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3709)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
當讀取數據庫,從該表上負載失敗上下面的類的數據(Inicio.class)
Code of Inicio.class
...
bancoDados = new DataAdapter(getActivity());
bancoDados.createDatabase();
bancoDados.open();
settings = bancoDados.carregaConfiguracao()
bancoDados.close();
...
我得到
android.database.sqlite.SQLiteException: no such table: configuracao: , while compiling: SELECT * FROM configuracao
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1357)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1325)
at com.ead.louvemosfinal.DataAdapter.carregaConfiguracao(DataAdapter.java:429)
at com.ead.louvemosfinal.Inicio.construtor(Inicio.java:118)
at com.ead.louvemosfinal.Inicio.onCreateView(Inicio.java:97)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:279)
at android.view.View.dispatchAttachedToWindow(View.java:6156)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1122)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
at android.view.ViewRoot.performTraversals(ViewRoot.java:773)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
自DataAdapter
的carregaConfiguracao碼的錯誤public String[] carregaConfiguracao(){
Cursor resul = mDb.rawQuery("SELECT * FROM configuracao", null);
String[] dados = new String[11];
resul.moveToFirst();
dados[0] = resul.getString(resul.getColumnIndex("_id"));
dados[1] = resul.getString(resul.getColumnIndex("fonte"));
dados[2] = resul.getString(resul.getColumnIndex("nome"));
dados[3] = resul.getString(resul.getColumnIndex("email"));
dados[4] = resul.getString(resul.getColumnIndex("cor_nota"));
dados[5] = resul.getString(resul.getColumnIndex("cor_letra"));
dados[6] = resul.getString(resul.getColumnIndex("contraste"));
dados[7] = resul.getString(resul.getColumnIndex("refrao"));
dados[8] = resul.getString(resul.getColumnIndex("acordes"));
dados[9] = resul.getString(resul.getColumnIndex("musica"));
dados[10] = resul.getString(resul.getColumnIndex("cantor"));
resul.close();
return dados;
}
你確定有一個叫configuracao的表嗎? –
是的......只有三星設備沒有看到這張表 –
請問您可以告訴您將數據庫複製到的路徑嗎? –