2017-08-02 84 views
0

我已經在我正在導入到我的android應用程序的sqlite工作室中創建數據庫。 SQLite的助手代碼如下:從sqlite數據庫的物理副本創建數據庫時SQL(查詢)錯誤或缺少數據庫

public class DatabaseHelper extends SQLiteOpenHelper { 


String DB_PATH = null; 
private static String DB_NAME = "mydatabases"; 
private static SQLiteDatabase myDataBase; 
private final Context myContext; 

public DatabaseHelper(Context context) { 
    super(context, DB_NAME, null, 21); 
    this.myContext = context; 
    this.DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/"; 

    Log.e("Path 1", DB_PATH); 
} 



@Override 
public synchronized void close() { 
    if (myDataBase != null) 
     myDataBase.close(); 
    super.close(); 
} 


@Override 
public void onCreate(SQLiteDatabase db) { 
    try { 
     InputStream myInput = myContext.getAssets().open(DB_NAME); 
     String outFileName = DB_PATH + DB_NAME; 
     OutputStream myOutput = new FileOutputStream(outFileName); 
     byte[] buffer = new byte[10]; 
     int length; 
     while ((length = myInput.read(buffer)) > 0) { 
      myOutput.write(buffer, 0, length); 
     } 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 
    } 
    catch(IOException ex){} 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    if (newVersion > oldVersion){ 
     try { 
      InputStream myInput = myContext.getAssets().open(DB_NAME); 
      String outFileName = DB_PATH + DB_NAME; 
      OutputStream myOutput = new FileOutputStream(outFileName); 
      byte[] buffer = new byte[10]; 
      int length; 
      while ((length = myInput.read(buffer)) > 0) { 
       myOutput.write(buffer, 0, length); 
      } 
      myOutput.flush(); 
      myOutput.close(); 
      myInput.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 

     } 
    } 
} 

現在我想獲取從數據庫中的數據,下面是代碼在活動負荷檢索數據:

onActivityCreated代碼:

public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 
    final ArrayList<String> strArary; 
    myDbHelper = new DatabaseHelper(getActivity()); 
    c = myDbHelper.getReadableDatabase().rawQuery("SELECT * FROM categories",null) ; // myDbHelper.query("categories",null,null,null,null,null,null); 

if (c.moveToFirst()) { 
     do { 
      strArary1.add(c.getString(0)); 
      strArary.add(c.getString(1)); 

     } while (c.moveToNext()); 
    } 

定的代碼加薪題目中提到的一些錯誤。

請幫我解決問題。

+1

考慮使用[sqlite的資產輔助(https://github.com/jgilfelt/android-sqlite-asset-helper)來代替。 – laalto

+0

我認爲你需要在'onCreate'之前這麼做,因爲只有當文件不存在時才調用onCreate',這種情況下數據庫文件被創建,然後調用'onCreate'中的代碼。 – MikeT

回答

-1

這是我的類和我工作得很好:

public class DBHandler extends SQLiteOpenHelper { 

    private static final String DataBase_Name = "yourdb"; 
    private static final int DataBase_Version = 3; 
    private static final String Path ="/data/data/" + context.getPackageName() + "/" + "databases/"; 
    public Cursor cursor; 
    protected Context context; 
    SQLiteDatabase DB; 
    String url; 

    public DBHandler(Context context) { 
     super(context, DataBase_Name, null, DataBase_Version); 
     this.context = context; 

     try { 
      OpenDataBase(); 
      Close(); 
     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
     } 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
    } 

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

    private void CopyDataBase() throws IOException { 

     InputStream myinput = context.getAssets().open(DataBase_Name); 
     String output = Path + DataBase_Name; 

     OutputStream myoutputstream = new FileOutputStream(output); 
     byte[] buffer = new byte[1024]; 
     int length; 

     try { 
      while ((length = myinput.read(buffer)) >= 0) { 
       myoutputstream.write(buffer, 0, length); 
      } 
      myoutputstream.flush(); 
      myoutputstream.close(); 
      myinput.close(); 
     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
     } 

    } 

    protected boolean ChekDB() { 
     SQLiteDatabase exist = null; 
     try { 
      String DBPath = Path + DataBase_Name; 
      exist = SQLiteDatabase.openDatabase(DBPath, null, SQLiteDatabase.OPEN_READWRITE); 
      if (DataBase_Version > exist.getVersion()) { 
       exist.close(); 
       CopyDataBase(); 
       exist = SQLiteDatabase.openDatabase(DBPath, null, SQLiteDatabase.OPEN_READWRITE); 
       exist.setVersion(DataBase_Version); 
       Util.saveDataToShared(context, "sync", "fixedInserted", ""); 
      } 


     } catch (SQLiteException e) { 
      System.out.println(e.getMessage()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     if (exist != null) { 
      exist.close(); 
     } 
     return exist != null ? true : false; 

    } 

    private void OpenDataBase() throws SQLDataException { 

     if (ChekDB()) { 

      String DBPath = Path + DataBase_Name; 
      DB = SQLiteDatabase.openDatabase(DBPath, null, SQLiteDatabase.OPEN_READWRITE); 
     } else { 
      this.getWritableDatabase(); 
      try { 
       CopyDataBase(); 
      } catch (IOException e) { 
       System.out.println(e.getMessage()); 
      } 
     } 


    } 

    public Cursor Select(String Query) { 

     try { 
      OpenDataBase(); 
      cursor = DB.rawQuery(Query, null); 
     } catch (SQLDataException e) { 
      if (e!=null&&e.getMessage()!=null) 
       Log.e("Handler - Select :",e.getMessage()); 
     } 

     return cursor; 
    } 

    /** 
    * @param tableName 
    * @param contentValues 
    * @param whereClause 
    * @param args 
    * @return Rows Effected 
    */ 
    protected int update(String tableName, ContentValues contentValues, String whereClause, String[] args) { 
     try { 
      OpenDataBase(); 
     } catch (SQLDataException e) { 
      System.out.println(e.getMessage()); 
     } 
     int result = DB.update(tableName, contentValues, whereClause, null); 
     DB.close(); 
     return result; 
    } 

    /** 
    * @param tableName 
    * @param whereClause 
    * @param args 
    * @return Rows Effected 
    */ 
    protected int delete(String tableName, String whereClause, String[] args) { 
     try { 
      OpenDataBase(); 
     } catch (SQLDataException e) { 
      System.out.println(e.getMessage()); 
     } 
     int result = DB.delete(tableName, whereClause, args); 
     DB.close(); 
     return result; 
    } 

    protected long insert(String Table_Name, ContentValues V) { 
     long rowid = -1; 
     try { 
      OpenDataBase(); 
      rowid = DB.insert(Table_Name, null, V); 
      System.out.println(rowid); 
      DB.close(); 
     } catch (Exception e) { 
      if (e!=null&&e.getMessage()!=null) 
       Log.e("Inserting to Table "+Table_Name,e.getMessage()); 
      DB.close(); 
     } 
     return rowid; 
    } 

    protected void ExecuteQuery(String Q) { 
     try { 
      OpenDataBase(); 
      DB.execSQL(Q); 
      DB.close(); 
     } catch (Exception e) { 
      System.out.println("ExecuteQuery " + e.getMessage()); 
     } 
    } 

    public void Close() { 
     if (DB != null) 
      DB.close(); 
     if (cursor != null && !cursor.isClosed()) 
      cursor.close(); 
    } 

    public void WhenUpGrade() { 
     try { 
      String assetPath = context.getAssets().toString() + "/" + DataBase_Name; 
      SQLiteDatabase assetDB = SQLiteDatabase.openDatabase(assetPath, null, SQLiteDatabase.OPEN_READWRITE); 
      SQLiteDatabase oldDB = SQLiteDatabase.openDatabase(Path + DataBase_Name, null, SQLiteDatabase.OPEN_READWRITE); 
      Log.d("", assetDB != null ? "" : ""); 
      Log.d("", oldDB != null ? "" : ""); 
     } catch (Exception e) { 
      if (e != null && e.getMessage() != null) 
       Log.e("upgradingDB", e.getMessage()); 
     } 
    } 


} 
+0

類似的代碼也適用於我,但是當您想在生產環境中更改數據庫或表時,它不起作用,用戶必須先取消應用程序以獲取新更改的反應。 –

+0

沒有我更新它檢查數據庫版本,如果版本超過當前它將recopy數據庫 –