2014-12-02 46 views
1

我有一個.sqllite數據庫文件和我嘗試打開該數據庫並插入一些values.but當我運行應用程序,我必須登錄貓error.can't打開數據庫 這是我的代碼機器人無法從資產文件夾中打開SQLite數據庫

public class IrocDBController extends SQLiteOpenHelper { 

String DB_PATH = null; 
private static String DB_NAME = "CredoDatabase.sqlite"; 
private static String usm_Users = "Usm_Users"; 

private SQLiteDatabase myDataBase; 

private final Context myContext; 
public IrocDBController(Context context) { 

    super(context, DB_NAME, null, 2); 
    this.myContext = context; 
    DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/"; 
} 
public void InsertToLoginTable(String username, String password, 
     String LoanOfficerId, String BranchId) { 
    ContentValues newValues = new ContentValues(); 
    newValues.put("UserName", username); 
    newValues.put("Password", password); 
    newValues.put("LoanOfficerId", LoanOfficerId); 
    newValues.put("BranchId", BranchId); 

    myDataBase.insert(usm_Users, null, newValues); 
} 


public boolean DublicateValues(int LoanOfficerId) { 
    myDataBase = this.getWritableDatabase(); 
    Cursor cursor = myDataBase.rawQuery("select * from " + usm_Users + " where " 
      + "LoanOfficerId" + " == " + LoanOfficerId, null); 
    boolean exists = (cursor.getCount() > 0); 
    Log.e("aaaaaaaaaaaaaaaaaaaa", String.valueOf(cursor.getCount())); 
    cursor.close(); 
    return exists; 
} 

public void createDataBase() throws IOException { 
    boolean dbExist = checkDataBase(); 
    if (dbExist) { 
     // do nothing - database already exist 
    } else { 
     this.getReadableDatabase(); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      throw new Error("Error copying database"); 

     } 
    } 

} 

public String getNameFildeEntry(String username) { 
    Cursor cursor = myDataBase.query(usm_Users, null, " UserName=?", 
      new String[] { username }, null, null, null); 
    if (cursor.getCount() < 1) // UserName Not Exist 
    { 
     cursor.close(); 
     return "NOT EXIST"; 
    } 
    cursor.moveToFirst(); 
    String getUserName = cursor 
      .getString(cursor.getColumnIndex("UserName")); 

    cursor.close(); 
    return getUserName; 
} 

public String getPasswordFildeEntry(String Password) { 
    Cursor cursor = myDataBase.query(usm_Users, null, " Password=?", 
      new String[] { Password }, null, null, null); 
    if (cursor.getCount() < 1) // UserName Not Exist 
    { 
     cursor.close(); 
     return "NOT EXIST"; 
    } 
    cursor.moveToFirst(); 
    String getUserPassword = cursor.getString(cursor 
      .getColumnIndex("Password")); 

    cursor.close(); 
    return getUserPassword; 
} 


private boolean checkDataBase() { 
    SQLiteDatabase checkDB = null; 
    try { 
     String myPath = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READWRITE); 
    } catch (SQLiteException e) { 
    } 
    if (checkDB != null) { 
     checkDB.close(); 
    } 
    return checkDB != null ? true : false; 
} 


private void copyDataBase() throws IOException { 

    InputStream myInput = myContext.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(); 
} 

public void openDataBase() throws SQLException { 


    String path = DB_PATH + DB_NAME; 
    myDataBase = SQLiteDatabase.openDatabase(path, null, 
      SQLiteDatabase.OPEN_READWRITE); 
} 

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



@Override 
public void onCreate(SQLiteDatabase db) { 

} 

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

} 

}

我使用nativec TU創建SQLite數據庫和我的數據庫中,我創建了一些表,等這個數據庫I插入資產的文件夾 我不知道我在做什麼錯誤。 如果有人知道的解決方案,請幫我 這是我的日誌貓錯誤代碼 enter image description here

回答

0

這可能是因爲SQL文件是在你的資源文件夾。我用一個exe這是在我的資源文件夾也有類似的問題。我不得不把它複製到手機存儲,當我想用​​它,然後將其刪除時,我知道我不會再使用它。 它可以通過一個事實,即在手機上的應用程序是一個.apk文件(這就像一個zip文件),除非您使用的Adroid資產幫手,你不能存取權限裏面的文件引起的。

相關問題