我在按钮的onclick方法中创建数据库和表(保存)。单击“保存”按钮时,将创建数据库和表。我可以采取5 edittext输出。在此程序中,用户无法输入值。例如定时器控制。在计时器应用程序中,当用户按下开始按钮然后启动倒数计时器时,他按下停止按钮然后停止值,并在按下保存按钮时将值存储在数据库中。所以我可以在保存按钮上创建数据库和表。请给我一些建议和代码,用于插入outout值,而不是像数据库中用户输入的值。 我的代码如下:
代码:
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent =new Intent(Timedetails.this,ListActivity.class);
Toast.makeText(getApplicationContext(), "Project has been added to List", 1).show();
startActivity(intent);
try{
SQLiteDatabase db;
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();
}
catch(Exception e){
e.printStackTrace();
}
}
答案 0 :(得分:0)
插入代码:
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代码首先启动intent,然后尝试将数据插入数据库: 改变如下:
@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);