插入代码:
ContentValues cvTimerDtl = new ContentValues();
cvTimerDtl.put("name", name);
cvTimerDtl.put("Timerpoint1", tPoint1);
cvTimerDtl.put("Timerpoint2", tPoint2);
cvTimerDtl.put("Timerpoint3", tPoint3);
cvTimerDtl.put("ServiceTime1", sTime1);
cvTimerDtl.put("ServiceTime2", sTime2);
db.insert("timerdetail", null, cvTimerDtl);
编辑:我认为它应该工作。但我仍然发布另一个代码:
String query = "INSERT INTO timerdetail (name, Timerpoint1, Timerpoint2, Timerpoint3, ServiceTime1, ServiceTime2) VALUES ('"
+ name + "', '" + tPoint1 + "', '" + tPoint2 + "', '" + tPoint3 + "', '" + sTime1 + "', '" + sTime2 + "');";
db.execSQL(query);
编辑:
您的 onClick 代码首先启动意图,然后您尝试将数据插入数据库:更改如下:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
db = SQLiteDatabase.openDatabase("data/data/com.example.timerdemo2/timer", null, SQLiteDatabase.CREATE_IF_NECESSARY);
//db = openOrCreateDatabase("timer", MODE_PRIVATE, null);
db.execSQL("create table timerdetail("
+ "project_id integer PRIMARY KEY autoincrement,"
+ "name text,"
+ "Timerpoint1 text,"
+ "Timerpoint2 text,"
+ "Timerpoint3 text,"
+ "ServiceTime1 text,"
+ "ServiceTime2 text);");
Toast.makeText(getApplicationContext(), "Table is created", 1).show();
// db.execSQL("insert into timerdetail(name,tmpoint1,tmpoint2,tmpoint3,servicetime1,servicetime2) values ();");
/* ContentValues cv = new ContentValues();
//cv.put("name", eta.getText().toString());
cv.put("timing_point1", eta.getText().toString());
cv.put("timing_point2", etb.getText().toString());
cv.put("timing_point3", etc.getText().toString());
int rowposition = (int) db.insert("timerdetail", null, cv); */
addRecord();
}
catch(Exception e){
e.printStackTrace();
}
Intent intent =new Intent(Timedetails.this,ListActivity.class);
Toast.makeText(getApplicationContext(), "Project has been added to List", 1).show();
startActivity(intent);
}
编辑:
改变
db.execSQL("create table timerdetail("
+ "project_id integer PRIMARY KEY autoincrement,"
+ "name text,"
+ "Timerpoint1 text,"
+ "Timerpoint2 text,"
+ "Timerpoint3 text,"
+ "ServiceTime1 text,"
+ "ServiceTime2 text);");
到
db.execSQL("create table IF NOT EXISTS timerdetail("
+ "project_id integer PRIMARY KEY autoincrement,"
+ "name text,"
+ "Timerpoint1 text,"
+ "Timerpoint2 text,"
+ "Timerpoint3 text,"
+ "ServiceTime1 text,"
+ "ServiceTime2 text);");
因此,如果该表已存在,则不会创建该表。
编辑:
如果您想放置 x 列,您的内容值将包含那么多键值对。因此,如果您也想在数据库中添加标题。将内容值更改为
ContentValues cvTimerDtl = new ContentValues();
cvTimerDtl.put("title", title);
cvTimerDtl.put("name", name);
cvTimerDtl.put("Timerpoint1", tPoint1);
cvTimerDtl.put("Timerpoint2", tPoint2);
cvTimerDtl.put("Timerpoint3", tPoint3);
cvTimerDtl.put("ServiceTime1", sTime1);
cvTimerDtl.put("ServiceTime2", sTime2);
db.insert("timerdetail", null, cvTimerDtl);