获取SQLiteException:接近“Nov”:语法错误(代码1)

时间:2016-11-16 12:02:11

标签: android android-sqlite android-date

我正在尝试使用Active android开发一个本地数据库。在模型类中我有一个名为updated_date的字段,它是一个字符串。我已将字符串updated_date转换为Date格式并尝试将字段与今天的日期进行比较在where子句中。但是在执行此操作时,我在Active android的where()中得到了一个SQLiteException。

模型类

@Column(name = "Updated_date")
public String updated_date = "2016-11-16";

DBUtil类

public static int getTotalPreVisitCount() throws ParseException {

    int i = new Select()
            .from(Lead.class)
            .where(Utility.getDateFromString(new Lead().updated_date) + " >= ?",Utility.getDateFromString(Utility.todayDate()))
            .where(new Lead().lead_status + " = ",LMSUtil.PRE_VISIT)
            .execute().size();
    return i;
}

将字符串字段转换为日期

public static Date getDateFromString(String dbdate) throws ParseException {

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date date = formatter.parse(dbdate);
    return date;
}

**Logcat**

11-16 18:11:16.232 29852-29987/com.purplepropshop.lms E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.purplepropshop.lms, PID: 29852
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.purplepropshop.lms/com.purplepropshop.lms.ToDoListActivity}: android.database.sqlite.SQLiteException: near "Nov": syntax error (code 1): , while compiling: SELECT * FROM Leads WHERE Wed Nov 16 00:00:00 GMT+05:30 2016 >=
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440)
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                        at android.os.Looper.loop(Looper.java:193)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5344)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
                                                                        at dalvik.system.NativeStart.main(Native Method)
                                                                     Caused by: android.database.sqlite.SQLiteException: near "Nov": syntax error (code 1): , while compiling: SELECT * FROM Leads WHERE Wed Nov 16 00:00:00 GMT+05:30 2016 >=
                                                                        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
                                                                        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
                                                                        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                        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:1339)
                                                                        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1278)
                                                                        at com.activeandroid.Model.rawQuery(Model.java:349)
                                                                        at com.activeandroid.query.From.execute(From.java:155)
                                                                        at com.purplepropshop.lms.Utility.DBUtil.getTotalPreVisitCount(DBUtil.java:50)
                                                                        at com.purplepropshop.lms.ToDoListActivity.getLeadCount(ToDoListActivity.java:297)
                                                                        at com.purplepropshop.lms.ToDoListActivity.onCreate(ToDoListActivity.java:78)
                                                                        at android.app.Activity.performCreate(Activity.java:5361)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                        at android.os.Looper.loop(Looper.java:193) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5344) 
                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676) 
                                                                        at dalvik.system.NativeStart.main(Native Method) 

请帮我解决这个问题。我不明白我哪里弄错了。

1 个答案:

答案 0 :(得分:0)

在这一行:

where(Utility.getDateFromString(new Lead().updated_date) + " >= ?",Utility.getDateFromString(Utility.todayDate()))

您正在尝试让SQLite直接比较两个Date对象,这是不可能的。尝试比较他们的UNIX时间戳:

where(Utility.getDateFromString(new Lead().updated_date).getTime() + " >= ?",Utility.getDateFromString(Utility.todayDate()).getTime())