从日期和时间选择器android在sqlite数据库中插入datetime

时间:2014-07-04 17:45:51

标签: android sqlite datetime

开发Android ToDO List App 需要你的帮助 我创建了表格:

db.execSQL("CREATE TABLE todos (_id INTEGER PRIMARY KEY AUTOINCREMENT, todo TEXT NOT NULL, datetime DATETIME NOT NILL );");

并在此

中插入todo Text NOT NULL
// DAO
private TodoDAO dao;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_todo);

    // Create DAO object
    dao = new TodoDAO(this);

    todoText = (EditText) findViewById(R.id.newTodoText);
    addNewButton = (Button) findViewById(R.id.addNewTodoButton);

    datepicker = (DatePicker) findViewById(R.id.datePicker1);
    timepicker = (TimePicker) findViewById(R.id.timePicker1);

    addNewButton.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (addNewButton.isPressed()) {
        // Get entered text
        String todoTextValue = todoText.getText().toString();
        todoText.setText("");

        // Add text to the database
        dao.createTodo(todoTextValue);
        }
        }

然后如何获取此活动的日期时间值 并插入DataBase

TodoDAO.java

   public TodoDAO(Context context) {
       dbHelper = new TodoSQLiteHelper(context);
       db = dbHelper.getWritableDatabase();
   }

   // Close the db
   public void close() {
       db.close();
   }

   public void createTodo(String todoText) {
       ContentValues contentValues = new ContentValues();
       contentValues.put("todo", todoText);
       // Insert into DB
       db.insert("todos", null, contentValues);
   }

帮我将日期时间格式DATETIME插入数据库

1 个答案:

答案 0 :(得分:0)

您可以使用SimpleDateFormat以sqlite接受的格式获取日期并将其存储。请参阅此处的方法3:http://tips.androidhive.info/2013/10/android-insert-datetime-value-in-sqlite-database/