2014-01-21 35 views
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; 

} 
+0

你確定有一個叫configuracao的表嗎? –

+0

是的......只有三星設備沒有看到這張表 –

+0

請問您可以告訴您將數據庫複製到的路徑嗎? –

回答

0

使用這個DataBaseHandler Calss.it是我所有的android deveices的工作。

import java.util.ArrayList; 
import java.util.List; 

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.SQLiteOpenHelper; 

public class DatabaseHandler { 

    // Database 
    private static final String DATABASE_NAME = "Heat"; 
    private static final int DATABASE_VERSION = 1; 

    // field 
    private static final String KEY_ID = "id"; 

    private static final String KEY_TAGS_COLOR = "tag_color"; 
    private static final String KEY_TAGS_TAG_ID = "tag_id"; 
    private static final String KEY_TAGS_DESCRIPTION = "tag_description"; 
    private static final String KEY_TAGS_FLAG_ACTIVE = "isActive"; 

    // TABLES 
    private static final String TABLE_NAME_USER_DETAIL = "user_details"; 
    private static final String TABLE_NAME_TAGS = "tags"; 

    // CREATE TABLES 

    private static final String CREATE_TAGS_TABLE = "create table " 
      + TABLE_NAME_TAGS + " (" + KEY_ID 
      + " integer primary key autoincrement," + KEY_TAGS_TAG_ID 
      + " integer," + KEY_TAGS_DESCRIPTION + " text ," + KEY_TAGS_COLOR 
      + " text ," + KEY_TAGS_FLAG_ACTIVE + " text); "; 

    private DatabaseHelper dbHelper; 
    private SQLiteDatabase db; 

    private static final String TAG = "DatabaseHandler"; 

    public DatabaseHandler(Context context) { 
     dbHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper { 
     DatabaseHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 

      db.execSQL(CREATE_TAGS_TABLE); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      onCreate(db); 
     } 
    } 

    public DatabaseHandler open() throws SQLException { 
     db = dbHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close() { 
     dbHelper.close(); 
    } 

    public void insertTagList(List<YourModelCalss> tag_list) { 
     if (tag_list == null) { 
      return; 
     } 
     for (YourModelCalss tags : tag_list) { 
      insertTag(tags); 
     } 
    } 

    private void insertTag(YourModelCalss tags) { 
     // AppLog.Log(TAG, "Inserting into Database TAG :: " + tags.getDesc()); 

     ContentValues values = new ContentValues(); 

     values.put(KEY_TAGS_TAG_ID, tags.getId()); 
     values.put(KEY_TAGS_DESCRIPTION, tags.getDesc()); 
     values.put(KEY_TAGS_COLOR, tags.getColor()); 
     int active_flag = 0; 

     if (tags.getFlag_active() != null 
       && tags.getFlag_active().equalsIgnoreCase("1")) { 
      active_flag = 1; 
     } 
     values.put(KEY_TAGS_FLAG_ACTIVE, active_flag); 
     try { 
      open(); 
      db.insert(TABLE_NAME_TAGS, null, values); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     close(); 
    } 

    public ArrayList<YourModelCalss> getTagList() { 
     ArrayList<YourModelCalss> tagList = new ArrayList<YourModelCalss>(); 

     open(); 

     Cursor cursor = db.query(TABLE_NAME_TAGS, new String[] { 
       KEY_TAGS_TAG_ID, KEY_TAGS_DESCRIPTION, KEY_TAGS_COLOR, 
       KEY_TAGS_FLAG_ACTIVE }, null, null, null, null, null); 

     if (cursor != null && cursor.getCount() == 0) { 
     } else { 
      cursor.moveToFirst(); 
      YourModelCalss tag; 
      do { 
       tag = new YourModelCalss(); 
       tag.setId(cursor.getInt(0)); 
       tag.setDesc(cursor.getString(1)); 
       tag.setColor(cursor.getString(2)); 
       tag.setFlag_active(cursor.getString(3)); 

       tagList.add(tag); 
      } while (cursor.moveToNext()); 

     } 

     cursor.close(); 
     cursor = null; 
     close(); 

     return tagList; 
    } 

    public void clearTagData() { 
     try { 
      open(); 
      db.delete(TABLE_NAME_TAGS, null, null); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      close(); 
     } 

    } 

}