SQLite - 按TEXT格式存储的日期过滤数据的查询

时间:2018-02-12 10:30:00

标签: android mysql sqlite android-sqlite

我有一个SQLite数据库,用于存储客户在商店中生成的发票数据。我有一个存储发票号码的表格以及创建它们的日期和时间。日期列的数据类型为TEXT,格式为YYYY-mm-dd hh: MM: ss (2018-02-12 03:02:59),如下所示。我想根据用户提供的日期过滤结果。如果用户选择2018-02-01作为查询日期,我想显示在2018-02-01 00:00:00到当前日期2017-02-12 23:59:59之间生成的所有发票编号。我在网上搜索了很多但我无法找到相关答案。一些答案说要将整个数据库更改为UNIX纪元时间格式,但在我的情况下现在不可能。我应该如何创建我的选择查询以获得所需的结果?

我使用了以下SQLite命令

SELECT bill_type, bill_amount, bill_date, bill_person_id, _id, partial_amount, bill_payment_status, bill_payment_date FROM bill_data_details WHERE bill_type = 1002 and bill_person_id = 3 and bill_date BETWEEN '2018-02-12 00:00:00 and 2018-02-12 11:59:59' ORDER BY bill_date ASC)

但我的应用程序崩溃并显示错误

> FATAL EXCEPTION: AsyncTask #1
>                   Process: com.yourbusinessassistant.stocks, PID: 25275
>                   java.lang.RuntimeException: An error occured while executing doInBackground()
>                       at android.os.AsyncTask$3.done(AsyncTask.java:304)
>                       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
>                       at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
>                       at java.util.concurrent.FutureTask.run(FutureTask.java:242)
>                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
>                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
>                       at java.lang.Thread.run(Thread.java:818)
>                    Caused by: android.database.sqlite.SQLiteException: near "ORDER": syntax error (code 1): , while compiling: SELECT
> bill_type, bill_amount, bill_date, bill_person_id, _id,
> partial_amount, bill_payment_status, bill_payment_date FROM
> bill_data_details WHERE bill_type = 1002 and bill_person_id = 3 and
> bill_date BETWEEN '2018-02-12 00:00:00 and 2018-02-12 11:59:59' ORDER
> BY bill_date ASC
>                   #################################################################
>                   Error Code : 1 (SQLITE_ERROR)
>                   Caused By : SQL(query) error or missing database.
>                       (near "ORDER": syntax error (code 1): , while compiling: SELECT bill_type, bill_amount, bill_date, bill_person_id,
> _id, partial_amount, bill_payment_status, bill_payment_date FROM bill_data_details WHERE bill_type = 1002 and bill_person_id = 3 and
> bill_date BETWEEN '2018-02-12 00:00:00 and 2018-02-12 11:59:59' ORDER
> BY bill_date ASC)
>                   #################################################################
>                       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
> Method)
>                       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
>                       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
>                       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
>                       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
>                       at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
>                       at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
>                       at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1454)
>                       at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1301)
>                       at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1172)
>                       at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1340)
>                       at com.yourbusinessassistant.stocks.database.billsdata.BillDataProvider.query(BillDataProvider.java:47)
>                       at android.content.ContentProvider.query(ContentProvider.java:1007)
>                       at android.content.ContentProvider$Transport.query(ContentProvider.java:218)
>                       at android.content.ContentResolver.query(ContentResolver.java:489)
>                       at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
>                       at android.content.CursorLoader.loadInBackground(CursorLoader.java:42)
>                       at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
>                       at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
>                       at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
>                       at android.os.AsyncTask$2.call(AsyncTask.java:292)
>                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)

2 个答案:

答案 0 :(得分:0)

使用此查询:

SELECT * FROM YourTableName WHERE DateTimeColumnName BETWEEN UserInputDate AND DATETIME('NOW', 'LOCALTIME')

这将导致所有行的日期时间在当前时间和用户输入的日期之间。 LOCALTIME用于补偿您的时区,SQLite datetime函数使用UTC时区。

在查询中出现错误后编辑问题:
您的查询使用了throws语法错误,因为BETWEENAND一起使用,您在单引号内使用了AND。将您的查询修改为:

SELECT bill_type, bill_amount, bill_date, bill_person_id, _id, partial_amount, bill_payment_status, bill_payment_date FROM bill_data_details WHERE bill_type = 1002 and bill_person_id = 3 and bill_date BETWEEN '2018-02-12 00:00:00' AND '2018-02-12 11:59:59' ORDER BY bill_date ASC)

如果您想在查询中使用datetime功能,请将其用作:

SELECT bill_type, bill_amount, bill_date, bill_person_id, _id, partial_amount, bill_payment_status, bill_payment_date FROM bill_data_details WHERE bill_type = 1002 and bill_person_id = 3 and bill_date BETWEEN '2018-02-12 00:00:00' AND DATETIME('NOW', 'LOCALTIME') ORDER BY bill_date ASC)

答案 1 :(得分:0)

试试这个:

SELECT bill_type, bill_amount, bill_date, 
bill_person_id, _id, partial_amount, 
bill_payment_status, bill_payment_date FROM 
bill_data_details WHERE bill_type = 1002 and 
bill_person_id = 3 and bill_date BETWEEN
strftime('%Y-%m-%d %H:%M:%S', '2018-02-12 00:00:00') and 
strftime('%Y-%m-%d %H:%M:%S', date('now')) 
ORDER BY bill_date ASC);