2015-12-14 32 views
0

我有問題在SQLite數據庫中插入數據。當我點擊按鈕保存,我沒有看到在SQLite編輯器中的數據.. 這個代碼有什麼問題?SQLite編輯器中沒有數據

1)AddMedicine.java

private void AddData() { 
    btn3.setOnClickListener(
     new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       HashMap queryValues = new HashMap(); 
       queryValues.put("medicine", medicine.getText().toString()); 
       queryValues.put("quantity", quantity.getText().toString()); 
       queryValues.put("date", date.getText().toString()); 
       queryValues.put("time", time.getText().toString()); 
       boolean isInserted = myDb.insertData(queryValues); 

       if (isInserted = true) { 
        Toast.makeText(AddMedicine.this, "Data inserted", Toast.LENGTH_LONG).show(); 
        Intent i = new Intent(AddMedicine.this, MainActivity.class); 
        startActivity(i); 
        // this.callHomeActivity(view); 

       } else 
        Toast.makeText(AddMedicine.this, "Data not inserted", Toast.LENGTH_LONG).show(); 
      } 
     } 
    ); 
} 

2)DatabaseHelper.java

public boolean insertData(String Name, String quantity, String Date, String Time) { 

    db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL_2, Name); 
    contentValues.put(COL_3, quantity); 
    contentValues.put(COL_4, Date); 
    contentValues.put(COL_5, Time); 
    long result = db.insert(TABLE_NAME, null, contentValues); 

    if (result == -1) { 
     return false; 
    } else { 
     return true; 
    } 
} 
+3

好吧,以'if(isInserted = true)'開始並沒有做你想做的事。它應該是'if(isInserted == true)'或者更簡單的'if(isInserted)'。 (同樣,你的返回語句後面可能只是'返回結果!= -1;',現在沒有錯,但比它需要的時間長。)通過這個改變,你得到了什麼祝酒? –

+0

我也強烈建議您避免使用原始類型,並遵循Java命名約定,並避免在所有內容中使用字符串 - 而且您向我們顯示的insertData方法不是您要調用的方法。 –

+0

如果(isInserted = true),我剛剛看到了這個代碼..謝謝你的提醒。我應該使用db.query不是嗎?不是db.rawQuery對不對?你是什​​麼意思insertData方法是不是你打來的? – BlurCode

回答

1

方法insertData需要4個參數的工作,但你通過HashMap作爲唯一的參數到這個方法。可能是你的問題?

希望它有幫助。

UPD

您有幾種可能性來改變這種狀況。 首先使用的代碼,由GreenRobo建議,或者您可以重寫您的方法insertData以符合您的HashMap參數。例如:

public boolean insertData(Map<String> map) { 
    db = this.getWritableDatabase();  
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL_2, map.get("name")); 
    contentValues.put(COL_3, map.get("quantity")); 
    contentValues.put(COL_4, map.get("date")); 
    contentValues.put(COL_5, map.get("time")); 
    long result = db.insert(TABLE_NAME, null, contentValues); 
    return (result != - 1); 
} 
+0

這就是說,我應該創建如下的methos HashMap:HashMap queryValues =新的HashMap(String m,String q,String d,String t); – BlurCode

+0

這應該是一個評論 - 如果OP的代碼完全在運行(它聽起來像是這樣),那麼推測可能是一個超負荷的地圖。 –

0

您可以使用此方法

myDb.insertData(medicine.getText().toString(), quantity.getText().toString(), date.getText().toString(), time.getText().toString()); 

。 根據你的方法實現

0

1你必須爲你的數據庫創建一個模型類來保存和檢索它的數據。

public class User { 
private int id; 
private String medicine; 
private String quantity; 
private String date; 
private String time; 

public User(String time, String medicine, String quantity, String date) { 
    this.time = time; 
    this.medicine = medicine; 
    this.quantity = quantity; 
    this.date = date; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getMedicine() { 
    return medicine; 
} 

public void setMedicine(String medicine) { 
    this.medicine = medicine; 
} 

public String getQuantity() { 
    return quantity; 
} 

public void setQuantity(String quantity) { 
    this.quantity = quantity; 
} 

public String getDate() { 
    return date; 
} 

public void setDate(String date) { 
    this.date = date; 
} 

public String getTime() { 
    return time; 
} 

public void setTime(String time) { 
    this.time = time; 
} 

} 和你Adddata方法是這樣的:

btn3.setOnClickListener(

new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      String e1 = medicine.getText().toString(); 
      String e2 = quantity.getText().toString(); 
      String e3 = date.getText().toString(); 
      String e4 = time.getText().toString(); 

      User user = new User(); 
      user.setMedicine(e1); 
      user.setQuantity(e2); 
      user.setDate(e3); 
      user.setTime(e4); 



      Databasehandler db = new Databasehandler(getActivity()); 
      db.insert(user); 

     } 
    }); 

並與replce您insertdata方法在數據庫處理器:

boolean insertData(User user){ 
    SQLiteDatabase db=getWritableDatabase(); 
    ContentValues cv=new ContentValues(); 
    cv.put(COL_2,user.getMedicine()); 
    cv.put(COL_3,user.getQuantity()); 
    cv.put(COL_4, user.getDate()); 
    cv.put(COL_5, user.getTime()); 
    /// from here you can check the data you getting 
    System.out.println("Medicine-------------" + user.getMedicine()); 
    System.out.println("Quan-------------" + user.getMedicine()); 
    System.out.println("Date-------------" + user.getDate()); 
    System.out.println("Time-------------"+user.getTime()); 
    long result = db.insert(TABLE_NAME, null, cv); 

    if (result == -1) { 
     return false; 
    } else { 
     return true; 
    } 

} 
+0

是什麼讓你認爲OP *有*創建模型類?這是一種設計方法,但不是唯一的 - 我不明白你的答案如何解釋OP目前所看到的。 –

+0

http://stackoverflow.com/users/22656/jon-skeet:這是使用模型類理解SQLite最簡單的方法,它有助於在任何時間點保存和檢索數據 –

+0

我不是說它是一個糟糕的主意 - 我只是說你的聲明「你必須」是不準確的,而且這不能解決用戶目前實際看到的問題。你的答案中沒有什麼解釋當前的行爲。 –

0

對於您使用的方法...: 這些是變化你必須這樣做:

new View.OnClickListener() { 
    @Override 
    public void onClick (View view){ 
    String e1 = medicine.getText().toString(); 
    String e2 = quantity.getText().toString(); 
    String e3 = date.getText().toString(); 
    String e4 = time.getText().toString(); 
    HashMap queryValues = new HashMap(); 

    queryValues.put("medicine", e1); 
    queryValues.put("quantity", e2); 
    queryValues.put("date", e3); 
    queryValues.put("time", e4); 
    boolean isInserted = myDb.insertData(queryValues); 

    if (isInserted == true) { 
     Toast.makeText(AddMedicine.this, "Data inserted", Toast.LENGTH_LONG).show(); 
     Intent i = new Intent(AddMedicine.this, MainActivity.class); 
     startActivity(i); 
     // this.callHomeActivity(view); 

    } else 
     Toast.makeText(AddMedicine.this, "Data not inserted", Toast.LENGTH_LONG).show(); 
} 
} 

public boolean insertData(HashMap h) { 

    db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL_2, h.get("medicine")); 
    contentValues.put(COL_3, h.get("quantity")); 
    contentValues.put(COL_4, h.get("date")); 
    contentValues.put(COL_5, h.get("time")); 
    long result = db.insert(TABLE_NAME, null, contentValues); 

    if (result == -1) { 
     return false; 
    } else { 
     return true; 
    } 
} 

直到現在,你從hashmap獲取數據的方法是錯誤的。 另外一個apporch可以是你可以直接在方法中傳遞e1,e2,e3,e4,並且休息你的insertData將保持不變。

+0

謝謝..但是,我在這部分有錯誤:我應該聲明COL_3,COL_4,COL_5嗎? contentValues.put(COL_2,h.get(「medicine」)); contentValues.put(COL_3,h.get(「quantity」)); contentValues.put(COL_4,h.get(「date」)); contentValues.put(COL_5,h.get(「time」)); – BlurCode

+0

這裏的COL_1,COL_2等是在創建的@ onCreate表中相同的字段名稱。更多..請發佈您的Dbhandler類快照。 –