【问题标题】:Retrieve Date from SQLite Android Studio java从 SQLite Android Studio java 中检索日期
【发布时间】:2020-04-10 07:16:34
【问题描述】:

我将当前日期保存在 SQLite 中,然后从 SQLite 中检索日期,但每次结果为零时问题出在哪里??

我获取当前日期的代码是

 private String getDate() {

        Calendar calendar = Calendar.getInstance();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat mdformat = new SimpleDateFormat("dd / MM / yyy ");
        return mdformat.format(calendar.getTime());
 }

我的代码保存到 SQLite 中

           PrayDBHelper mDBHelper = new PrayDBHelper(PrayActivity.this);
           SQLiteDatabase db = mDBHelper.getWritableDatabase();

           db.execSQL("INSERT INTO " + prayEntry.TABLE_NAME + " " +prayEntry.DATE +  " VALUES " +
                     getDate());

我从 SQLite DB 中检索日期的代码

    dbConnection();
    Cursor cursor= db.query(prayEntry.TABLE_NAME,prayEntry.DATE,null,null, null,
            null,null);
    while (cursor.moveToNext()) {
        String date = (cursor.getString(0));// get Date
        labelDATE.setText(date);
    }

【问题讨论】:

    标签: java android sqlite date android-sqlite


    【解决方案1】:

    切勿在 SQLite 中以不同于 YYYY-MM-DD 的格式存储日期。
    在查询、比较和按此日期排序时,除此格式之外的任何其他内容都会给您带来麻烦。
    对于您的代码,此语句(包括您的代码中必须存在的列名周围的括号,否则将引发异常,所以我猜这只是一个错字):

    "INSERT INTO "+prayEntry.TABLE_NAME+" ("+prayEntry.DATE +") VALUES "+getDate()
    

    0 保存在表中,因为getDate() 返回的值类似于:19 / 12 / 2019,它被解释为数字 19、12 和 2019 之间的整数除法,结果为 0
    原因是您没有将日期括在单引号内,例如:

    "INSERT INTO "+prayEntry.TABLE_NAME+" ("+prayEntry.DATE +") VALUES '"+getDate()+"'"
    

    但这不是推荐的插入方式。
    像这样使用insert()ContentValues() 方法:

    PrayDBHelper mDBHelper = new PrayDBHelper(PrayActivity.this);
    SQLiteDatabase db = mDBHelper.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(prayEntry.DATE, getDate());
    db.insert(prayEntry.TABLE_NAME, cv);
    db.close();
    

    【讨论】:

      【解决方案2】:

      我相信您的问题在于 INSERT,因为 TABLE 名称后面的值、要放置数据的列应该在括号内。 VALUES 本身也应该在括号内,值内的字符串应该用单引号括起来,所以不是

      db.execSQL("INSERT INTO " + prayEntry.TABLE_NAME + " " +prayEntry.DATE +  " VALUES " +
                       getDate());
      

      你应该使用:-

      db.execSQL("INSERT INTO " + prayEntry.TABLE_NAME + "(" +prayEntry.DATE +  ") VALUES ('" +
                       getDate() + "'));
      

      如果您使用 insert 便捷方法,这将代表您构建正确的 SQL。因此,建议您使用:-

      PrayDBHelper mDBHelper = new PrayDBHelper(PrayActivity.this);
      SQLiteDatabase db = mDBHelper.getWritableDatabase();
      ContentValues cv = new ContentValues();
      cv.put(prayEntry.DATE,getDate());
      db.insert(prayEntry.TABLE_NAME,null,cv);
      

      还建议您在检索数据时不要直接使用列偏移量,而是使用 Cursor 的 getColumnIndex 方法来获取偏移量。

      所以你可以使用 String date = (cursor.getString(0)); 代替:-

      String date = (cursor.getString(cursor.getColumnIndex(prayEntry.DATE)));
      

      关于日期,如果您以 SQlite 识别的格式存储日期,例如YYYY-MM-DD(2019-01-01 为 2019 年 1 月 1 日),这种格式的日期可以直接排序,并允许 SQlite 的日期和时间函数直接使用它们。因此建议您使用SimpleDateFormat("yyyy-MM-dd")

      【讨论】:

        猜你喜欢
        • 2015-05-27
        • 1970-01-01
        • 1970-01-01
        • 2019-07-22
        • 2013-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多