根据 android sqlite 中的当前时间显示 table 字段总线时间中的值
To Display the values in the table field Bus-time based on current time in android sqlite
我的问题是我需要根据 Bus-Time 字段显示数据库中的所有数据,并与当前时间进行比较,即如果当前时间是 6:30 PM,那么它应该显示所有大于或等于当前时间的总线时间字段值(6:30).
我在 android studio 的 DatabaseHandler.java 中写了一个 SQL 查询,但我在查询中遇到错误,你能帮忙解决吗?
查询的代码是。
public List<Contact> getBus(String t)
{
List<Contact> contactList = new ArrayList<Contact>();
String query = "SELECT * FROM contacts WHERE CAST( bus_time AS TIME ) >= CAST(" + t +" AS TIME )";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
我得到的错误是
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.database, PID: 4539
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: android.database.sqlite.SQLiteException: near ":27": syntax error (code 1): , while compiling: SELECT * FROM contacts WHERE CAST( bus_time AS TIME ) >= CAST(18:27 AS TIME )
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
在 SQLite 中没有数据类型 TIME
,任何日期 and/or 时间值都应被视为 TEXT
。
使用函数 STRFTIME()
获取格式为 HH:MM
:
的当前时间
SELECT *
FROM contacts
WHERE SUBSTR('0' || bus_time, -5) >= STRFTIME('%H:%M', 'now', 'localtime')
如果您将时间保存在 bus_time
列中,格式为 HH:MM
(小时为 2 位数字),则不需要在左侧填充的函数 SUBSTR()
a 0
如果小时只有 1 位数字,则代码为:
WHERE bus_time >= STRFTIME('%H:%M', 'now', 'localtime')
我的问题是我需要根据 Bus-Time 字段显示数据库中的所有数据,并与当前时间进行比较,即如果当前时间是 6:30 PM,那么它应该显示所有大于或等于当前时间的总线时间字段值(6:30).
我在 android studio 的 DatabaseHandler.java 中写了一个 SQL 查询,但我在查询中遇到错误,你能帮忙解决吗?
查询的代码是。
public List<Contact> getBus(String t)
{
List<Contact> contactList = new ArrayList<Contact>();
String query = "SELECT * FROM contacts WHERE CAST( bus_time AS TIME ) >= CAST(" + t +" AS TIME )";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
我得到的错误是
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.database, PID: 4539
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: android.database.sqlite.SQLiteException: near ":27": syntax error (code 1): , while compiling: SELECT * FROM contacts WHERE CAST( bus_time AS TIME ) >= CAST(18:27 AS TIME )
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
在 SQLite 中没有数据类型 TIME
,任何日期 and/or 时间值都应被视为 TEXT
。
使用函数 STRFTIME()
获取格式为 HH:MM
:
SELECT *
FROM contacts
WHERE SUBSTR('0' || bus_time, -5) >= STRFTIME('%H:%M', 'now', 'localtime')
如果您将时间保存在 bus_time
列中,格式为 HH:MM
(小时为 2 位数字),则不需要在左侧填充的函数 SUBSTR()
a 0
如果小时只有 1 位数字,则代码为:
WHERE bus_time >= STRFTIME('%H:%M', 'now', 'localtime')