2012-06-15 79 views
0

我在表中插入了兩個字段,即truck_id和employee_id。Android:硬編碼數據的Sqlite插入

對不起提問這個問題。但是,因爲我是Android新手,請幫助我。

public void insert(String[] employeeName,int[] truck_id){ 

    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues vals = new ContentValues(); 

    vals.put(COLUMN_TRUCKID, "CM-3456"); 
    vals.put(COLUMN_NAME,"Sachin"); 
    //vals.put(KEY_PIN, "12345"); 

    vals.put(COLUMN_TRUCKID, "HR-6788"); 
    vals.put(COLUMN_NAME,"Sameer"); 
    //vals.put(KEY_PIN, "54321"); 

    vals.put(COLUMN_ID, "AM-6123"); 
    vals.put(COLUMN_NAME,"Sahu"); 
    //vals.put(KEY_PIN, "3452"); 

    db.insert("Login", "John", 
      vals); 
    db.insert("Login", "Sameer", 
      vals); 

    db.execSQL(COLUMN_NAME); 

    //myDataBase.close(); 
} 
+0

那麼,什麼是問題? –

回答

0

ContentValuesMap,所以它只能包含在同一時間爲每個列名的一個條目。所以,您需要在設置其值後分別插入每個項目。

vals.put(COLUMN_TRUCKID, "CM-3456"); 
vals.put(COLUMN_NAME,"Sachin"); 
db.insert("Login", null, vals); 

vals.put(COLUMN_TRUCKID, "HR-6788"); 
vals.put(COLUMN_NAME,"Sameer"); 
db.insert("Login", null, vals); 

刪除此行,否則不好的事情會發生:

db.execSQL(COLUMN_NAME); 
+0

請解釋「壞事」? – BlueLeaf