SQL INSERT语句中的错误

时间:2012-07-06 06:47:49

标签: android sql insert

我正在用引号做一个大混乱,我无法正确执行查询,看看有人可以帮我正确地做。

db.execSQL("INSERT INTO Studies (download_id, name, subject_id, current_date)" 
    + "VALUES('" 
    + studies.get(vEst.getId()).p2 
    + "', '" 
    + studies.get(vEst.getId()).p2 
    + '", "' 
    + studies.get(vEst.getId()).id 
    + '", "' 
    + dateFormat.format(date)"'))";

Thankss!

3 个答案:

答案 0 :(得分:2)

动词之间添加空格并平衡单引号/括号。

INSERT INTO Studies (download_id, name, subject_id, current_date) VALUES('" 
        + studies.get(vEst.getId()).p2 + "','" 
        + studies.get(vEst.getId()).p2 + "','"
        + studies.get(vEst.getId()).id + "','" 
        + dateFormat.format(date) + "')";

您可以使用String.format()来避免此类问题。

String sql=String.format("INSERT INTO TableName (Col1, Col2) VALUES (%d,'%s')", value1,value2);

答案 1 :(得分:1)

db.execSQL("INSERT INTO Studies (download_id, name, subject_id, current_date) VALUES ('" + studies.get(vEst.getId()).p2 + "', '" + studies.get(vEst.getId()).p2  + "', '"  + studies.get(vEst.getId()).id  + "', '" + dateFormat.format(date)+"')");

答案 2 :(得分:0)

试试这个:

  db.execSQL("INSERT INTO Studies (download_id, name, subject_id, current_date)"
 + "VALUES(" + studies.get(vEst.getId()).p2 + ", '" 
+ studies.get(vEst.getId()).p2 + '", " 
+ studies.get(vEst.getId()).id + ", "' 
+ dateFormat.format(date) + "')");