gpt4 book ai didi

java - 在 ORMLite queryRaw 方法中获取两个日期字符串之间的日期时出现 SQLException

转载 作者:行者123 更新时间:2023-12-02 04:29:21 26 4
gpt4 key购买 nike

我是 android ORMLite 的初学者,我有一个查询,它根据其“日期”字段值应在给定字符串日期之间的条件,使用其模型类从两个表中选择特定数据。下面是我的查询。

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
int year = Calendar.getInstance().get(Calendar.YEAR);
String startDate="",endDate="";
startDate=1+"/"+month+"/"+year;
endDate =31+"/"+month+"/"+year;
String[] resultArray;
if(parid==4){
GenericRawResults<String[]> valueResult=dbHelper.getTransDetDao().queryRaw(
"SELECT T.amount FROM TransactionDetails T,TransactionHeaders H "
+ "WHERE H.id=T.transaction_id AND T.account_id=" + id
+ " AND H.date BETWEEN " + startDate + " AND " + endDate);
List<String[]> results = valueResult.getResults();
}
}catch(Exception es){
es.printStackTrace();
}

但其结果SQLException如下。

07-29 11:38:10.653: I/Database(627): sqlite returned: error code = 1, msg = near "Jul": syntax error
07-29 11:38:10.653: W/System.err(627): java.sql.SQLException: Could not perform raw query for select T.amount from TransactionDetails T,TransactionHeaders H where H.id=T.transaction_id AND T.account_id==109 AND H.date BETWEEN Wed Jul 01 00:00:00 GMT+05:30 2015 AND Fri Jul 31 00:00:00 GMT+05:30 2015
07-29 11:38:10.673: W/System.err(627): at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
07-29 11:38:10.673: W/System.err(627): at com.j256.ormlite.dao.BaseDaoImpl.queryRaw(BaseDaoImpl.java:542)
07-29 11:38:10.683: W/System.err(627): at com.example.fort.db.SaveMoneyController.getMonthTotal(SaveMoneyController.java:232)
07-29 11:38:10.683: W/System.err(627): at com.example.fort.db.SaveMoneyController.getListData(SaveMoneyController.java:199)
07-29 11:38:10.703: W/System.err(627): at com.example.fort.view.MainActivity.loadExpense(MainActivity.java:398)
07-29 11:38:10.703: W/System.err(627): at com.example.fort.view.MainActivity.listClicked(MainActivity.java:316)
07-29 11:38:10.713: W/System.err(627): at com.example.fort.view.MainActivity.access$10(MainActivity.java:259)
07-29 11:38:10.713: W/System.err(627): at com.example.fort.view.MainActivity$8.onItemClick(MainActivity.java:227)
07-29 11:38:10.733: W/System.err(627): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-29 11:38:10.733: W/System.err(627): at android.widget.ListView.performItemClick(ListView.java:3513)
07-29 11:38:10.743: W/System.err(627): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
07-29 11:38:10.753: W/System.err(627): at android.os.Handler.handleCallback(Handler.java:587)
07-29 11:38:10.753: W/System.err(627): at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 11:38:10.763: W/System.err(627): at android.os.Looper.loop(Looper.java:123)
07-29 11:38:10.783: W/System.err(627): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-29 11:38:10.783: W/System.err(627): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 11:38:10.783: W/System.err(627): at java.lang.reflect.Method.invoke(Method.java:507)
07-29 11:38:10.783: W/System.err(627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-29 11:38:10.783: W/System.err(627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-29 11:38:10.803: W/System.err(627): at dalvik.system.NativeStart.main(Native Method)
07-29 11:38:10.803: W/System.err(627): Caused by: java.sql.SQLException: Problems executing Android query: select T.amount from TransactionDetails T,TransactionHeaders H where H.id=T.transaction_id AND T.account_id==109 AND H.date BETWEEN Wed Jul 01 00:00:00 GMT+05:30 2015 AND Fri Jul 31 00:00:00 GMT+05:30 2015
07-29 11:38:10.833: W/System.err(627): at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
07-29 11:38:10.833: W/System.err(627): at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:184)
07-29 11:38:10.853: W/System.err(627): at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:65)
07-29 11:38:10.853: W/System.err(627): at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:55)
07-29 11:38:10.872: W/System.err(627): at com.j256.ormlite.stmt.RawResultsImpl.<init>(RawResultsImpl.java:28)
07-29 11:38:10.872: W/System.err(627): at com.j256.ormlite.stmt.StatementExecutor.queryRaw(StatementExecutor.java:280)
07-29 11:38:10.885: W/System.err(627): at com.j256.ormlite.dao.BaseDaoImpl.queryRaw(BaseDaoImpl.java:540)
07-29 11:38:10.885: W/System.err(627): ... 18 more
07-29 11:38:10.885: W/System.err(627): Caused by: android.database.sqlite.SQLiteException: near "Jul": syntax error: , while compiling: select T.amount from TransactionDetails T,TransactionHeaders H where H.id=T.transaction_id AND T.account_id==109 AND H.date BETWEEN Wed Jul 01 00:00:00 GMT+05:30 2015 AND Fri Jul 31 00:00:00 GMT+05:30 2015
07-29 11:38:10.913: W/System.err(627): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-29 11:38:10.913: W/System.err(627): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
07-29 11:38:10.933: W/System.err(627): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
07-29 11:38:10.933: W/System.err(627): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
07-29 11:38:10.985: W/System.err(627): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
07-29 11:38:10.985: W/System.err(627): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
07-29 11:38:11.013: W/System.err(627): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
07-29 11:38:11.013: W/System.err(627): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
07-29 11:38:11.023: W/System.err(627): at com.j256.ormlite.android.compat.BasicApiCompatibility.rawQuery(BasicApiCompatibility.java:15)
07-29 11:38:11.034: W/System.err(627): at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:180)
07-29 11:38:11.053: W/System.err(627): ... 23 more

下面是 TransactionHeaders 类。

TransactionHeaders.java:

public class TransactionHeaders implements Serializable {

@DatabaseField(generatedId = true, columnName = "id")
private Integer id;

@DatabaseField(columnName = "user_id")
private Integer userID;

@DatabaseField(columnName = "description")
private String description;

@DatabaseField(columnName = "date",dataType=DataType.DATE)
private Date date;

public TransactionHeaders() {
super();
// TODO Auto-generated constructor stub
}

public TransactionHeaders(Integer id, Integer userID, String description,
Date date) {
super();
this.id = id;
this.userID = userID;
this.description = description;
this.date = date;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getUserID() {
return userID;
}

public void setUserID(Integer userID) {
this.userID = userID;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}

是否是因为任何字符串或日期类型不匹配?请有人帮我离开这里。

最佳答案

Caused by: SQLiteException: near "Jul": syntax error: , while compiling: select T.amount from TransactionDetails T,TransactionHeaders H where H.id=T.transaction_id AND T.account_id==109 AND H.date BETWEEN Wed Jul 01 00:00:00 GMT+05:30 2015 AND Fri Jul 31 00:00:00 GMT+05:30 2015

此异常试图向您展示问题所在。 SQL 查询无法构建为字符串,因为它将日期对象扩展为其字符串形式。用单引号将它们引起来可以解决 SQL 问题,但我认为您不会因为这个答案而得到结果:

SQL Select between dates

您确实需要使用日期格式化程序尝试不同的日期格式。请参阅Sqlite date formats 。如果您将日期格式更改为受支持的格式之一,则可能会起作用,例如:

YYYY-MM-DD HH:MM:SS

要传递日期字符串,我将使用 SQL ? 参数,并将格式化的日期作为查询后的参数传递给 queryRaw(...)字符串。

关于java - 在 ORMLite queryRaw 方法中获取两个日期字符串之间的日期时出现 SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31693278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com