2013-07-10 89 views
3

我試圖將數據插入到數據庫SQLLite,但數據不會進入數據庫:Android的插入數據不存儲到數據庫

ContentValues cv = new ContentValues(); 
      cv.put("username", cust.getUsername()); 
      cv.put("password", cust.getPassword()); 
      cv.put("name", cust.getCustomername()); 
      long id = mDb.insert("user", null, cv); 
      Log.v(TAG, Long.toString(id)); 
      Log.e(TAG, "New Customer Created"); 
      return true; 

的ID返回5,任何想法,爲什麼? (幾次插入id返回7現在。)

我應該在這裏發佈什麼其他代碼?數據庫連接應該沒問題,因爲我能夠成功登錄。

+0

你可以發表你的數據庫onCreate方法 – Unknown

+0

請檢查您的列名如用戶名或密碼及名稱,請發表您的表結構 –

+0

將返回類型更改爲長數據類型 – Nandhiya

回答

0

要檢查什麼是真正插入從模擬器DB下載數據庫文件。您可以使用Android調試監控

Android Debug Monitor

然後打開數據庫的任何SQLite的客戶

1

正如您所提到的,您的數據庫存儲在您的資產文件夾中。資產或資源中的每個文件都是隻讀的。您必須將數據庫文件複製到另一個路徑才能將數據寫入其中。

下面是SQLiteOpenHelper一些代碼:

public static class DBHelper extends SQLiteOpenHelper{ 

    private static final String DATABASE_NAME = "database.db"; 
    private static final int DATABASE_VERSION = 1; 
    private static final String ASSET_DB_DIRECTORY = "db"; 
    private static final String DATABASE_RESOURCE_PATH = ASSET_DB_DIRECTORY + File.separator + DATABASE_NAME; 

    private final Context context; 
    private SQLiteDatabase db; 

    public DBHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     this.context = context; 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

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

    } 

    public void createDatabase() throws IOException{ 
     boolean dbExist = checkDatabase(); 

     if(dbExist){ 
      //do nothing - database already exist 
     }else{ 
      //By calling this method and empty database will be created into the default system path 
       //of your application so we are gonna be able to overwrite that database with our database. 
      this.getReadableDatabase(); 
      try { 
       copyDatabase(); 
      } catch (IOException e) { 
       throw new Error("Error copying database"); 
      } 
     } 
    } 

    public boolean checkDatabase(){ 
     SQLiteDatabase checkDB = null; 

     File file = context.getDatabasePath(DATABASE_NAME); 
     try { 
      checkDB = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
     } catch (Exception e) { 
      //database does't exist yet. 
     } 
     if(checkDB != null){ 
      checkDB.close(); 
      if(file.lastModified() != new File(DATABASE_RESOURCE_PATH).lastModified()) 
       return false; 
     } 
     return checkDB != null; 
    } 

    private void copyDatabase() throws IOException{ 
     //Open your local db as the input stream 
     InputStream myInput = context.getAssets().open(DATABASE_RESOURCE_PATH); 

     // Path to the just created empty db 
     String outFileName = context.getDatabasePath(DATABASE_NAME).getPath(); 

     //Open the empty db as the output stream 
     OutputStream myOutput = new FileOutputStream(outFileName); 

     //transfer bytes from the inputfile to the outputfile 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer))>0){ 
      myOutput.write(buffer, 0, length); 
     } 

     //Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 
    } 

    public SQLiteDatabase openDataBase() throws SQLException{ 
     //Open the database 
     String myPath = context.getDatabasePath(DATABASE_NAME).getPath(); 
     db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
     return db; 
    }