0

我在按钮(保存)的 onclick 方法中创建数据库和表。单击保存按钮时,将创建数据库和表。我可以使用 5 个编辑文本进行输出。在这个程序中,用户不能输入数值。例如定时器控制。在计时器应用程序中,当用户按下开始按钮然后倒数计时器启动时,他按下停止按钮然后值停止并且当他按下保存按钮时该值存储在数据库中。所以我可以在保存按钮上创建数据库和表。请给我建议和代码,用于插入与用户在数据库中输入的值不同的输出值。我的代码如下:

代码:


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();
            }
        }
4

1 回答 1

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 代码首先启动意图,然后您尝试将数据插入数据库:更改如下:

@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);
于 2012-12-27T09:33:46.810 回答