如何在CURRENT_TIMESTAMP列上执行删除?

问题描述

问题:

  • 我有一个名为[ INFOTABLE ] [INTEGER]数据类型的时间戳列的表DATETIMESTAMP
  • 我需要使用此列删除记录,但删除查询会引发错误
  • 主要是因为捕获的时间戳之间的T [TIME指示符]。 [例如:] 2020-01-01T17:45:31.127452+03:00[Asia/Riyadh]

sqlITE表:

  String create_sql3 = "CREATE TABLE IF NOT EXISTS " + Tablename3 + "("
                    + SNO + " INTEGER NOT NULL," + ITEM + " INTEGER NOT NULL,"
                    + DATETIMESTAMP + " INTEGER DEFAULT CURRENT_TIMESTAMP )";

删除查询

db.execsql("DELETE FROM " + Tablename3 + " WHERE DATETIMESTAMP = " + Integer.parseInt(deltimestamp.get(y))  + ";");

已记录例外情况

android.database.sqlite.sqliteException:
unrecognized token: "01T17" (code 1):,while compiling: DELETE
FROM INFOTABLE WHERE DATATIMESTAMP = 2020-01-01T17:45:31.127452+03:00[Asia/Riyadh];

这里deltimestamp一个字符串数组列表,其日期时间戳记为字符串。感谢有关删除CURRENT_TIMESTAMP列的查询更正的帮助!

解决方法

将表另存为时间戳(长值为1231413123),这将使您在删除过程中更容易查询相关值。

样品表;

CREATE TABLE MyTable(
        ID INTEGER PRIMARY KEY,Foo TEXT,Bar TEXT,Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
    );
,

解决了该问题!

查询(错误):

db.execSQL("DELETE FROM " + Tablename3 + " WHERE DATETIMESTAMP = " + Integer.parseInt(deltimestamp.get(y))  + ";");

查询(已修复):

db.execSQL("DELETE FROM " + Tablename3 + " WHERE DATETIMESTAMP = " + "'" + deltimestamp.get(y)  + "'" + ";");

OR

db.execSQL("DELETE FROM " + Tablename3 + " WHERE DATETIMESTAMP = "+ "'" + cursor.getString(7) + "'" + ";");