简体   繁体   English

使用数据库列值到Date()sqlite android时出现问题

[英]problem while using database column value into Date() sqlite android


i have small problem, i googled it many times but i couldn't get it... 我有一个小问题,我用Google搜索了很多遍,但是我听不懂...
i'm using a query (sqlite) to retrieve some data .. 我正在使用查询(sqlite)检索一些数据..
in this query their is Date() used to increase certain date dynamic number of days (coming from column in the same table) .. 在此查询中,它们的Date()用于增加某些日期动态天数(来自同一表的列)。
if i put this days static (1,2,3,.....) it works fine but if i put column name it doesn't. 如果我把这几天静态(1,2,3,.....),它工作正常,但如果我把列名,它没有。
this query fails and i want it to work: 此查询失败,我希望它能工作:

String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
            + DATE + " = date('" + targetDate + "',' " + REPETITIONS + " day') ";

this works fine : 这工作正常:

String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
            + DATE + " = date('" + targetDate + "','2 day') ";

where 哪里

targetDate: date entered by user to get events of that date targetDate:用户输入的获取该日期事件的日期

DATE: date of every event in the table DATE:表格中每个事件的日期

REPETETIONS: dynamic number (column in the same table) 注释:动态数(同一表中的列)

the problem is using REPETITIONS in the date function... 问题是在日期函数中使用了REPETITIONS ...

create statement 创建语句

String SQL_CREATE_EVENT_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " +
            ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            TITLE + " TEXT ," +
            DATE + " TEXT , " +
            IS_NOTIFY + " INTEGER , " +
            NOTIFICATION_TIME + " TEXT ," +
            REPEAT + " INTEGER ," +
            REPEAT_DURATION + " INTEGER ," +
            REPETITIONS + " INTEGER ," +
            CERTAIN_DATE + " TEXT ," +
            NOTE + " TEXT ," +
            IS_SPOKEN + " INTEGER " +
            " );";

and this is the selecting statement 这是选择语句

String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE " + DATE + " = '" + targetDate
            + "' OR (( " + REPEAT + " = '1' AND " + REPEAT_DURATION + " = '0' ) AND " + DATE + " <= '" + targetDate + "')"
            + " OR( " + REPEAT + " = '1' AND " + REPEAT_DURATION + " = '3' ) AND " + DATE + " <= '" + targetDate + "' AND " + CERTAIN_DATE + " >= '" + targetDate + "'"
            + " OR (" + REPEAT + " = '1' AND " + REPEAT_DURATION + "= '2' ) AND " + targetDate + " >= '" + DATE + "' AND "+ targetDate+ " <= date('" + DATE + "','" + REPETITIONS + " days')";

The variable REPETITIONS in this statement: 此语句中的变量REPETITIONS

String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
            + DATE + " = date('" + targetDate + "',' " + REPETITIONS + " day') ";

should be a number and not a column name. 应该是数字而不是列名。
The date function in SQLite has various syntaxes but you use this one: SQLite中的date函数具有多种语法,但是您可以使用以下语法:

SELECT date('2014-10-23','+7 day');

you must supply a number before day . 您必须在day前提供一个电话号码。

Edit try this: 编辑试试这个:

String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
            + DATE + " = date('" + targetDate + "', " + REPETITIONS + " || ' day') ";

In sqlite date() function , the NNN days is a string modifier and not an expression. 在sqlite date()函数中NNN days是字符串修饰符,而不是表达式。 Therefore you cannot use column names or even || 因此,您不能使用列名,甚至不能使用|| string concatenation there. 字符串串联在那里。

You can format the modifier string in your code and place it in the SQL. 您可以在代码中设置修饰符字符串的格式,并将其放置在SQL中。 This requires additional query to the database, which is likely not what you want. 这需要对数据库进行其他查询,而这可能不是您想要的。

If you can assume each day is 24 hours (which is not always true, consider eg DST events), you could try something like 如果您可以假设每天都是24小时(并非总是如此,请考虑DST事件),则可以尝试类似

... date(strftime('%s', '" + targetDate + "', " + REPETITIONS + "*24*60*60, 'unixepoch') ...

where strftime('%s', ...) converts to seconds and date(..., 'unixepoch') converts back to datestamp. 其中strftime('%s', ...)转换为秒, date(..., 'unixepoch')转换回datestamp。

But seriously I'd consider redesigning the schema to better support your functional requirements. 但认真的我会考虑重新设计架构,以更好地支持您的功能需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM