我是android ORMLite的初学者,我有一个查询,它使用它的模型类从两个表中选择特定数据,基于它的" date"字段值应该在给定的字符串日期之间。这是我的查询。
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;
}
}
是否因为任何字符串或日期类型不匹配?有人请帮我离开这里。
答案 0 :(得分:2)
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
This exception is trying to show you what is wrong. The SQL query cannot be built as a string because it is expanding your date objects into their string forms. Surrounding them with single quotes will fix the SQL issue but I don't think you will get results because of this answer:
You are doing to need to try different formats of the dates using a date formatter. See the Sqlite date formats. It may work if you change the date format to one of the supported forms such as:
YYYY-MM-DD HH:MM:SS
To pass in your date string, I'd use the SQL ?
arguments and pass the formatted dates to queryRaw(...)
as arguments after the query-string.
答案 1 :(得分:0)
Surround the dates with single quotes ('
):
"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 + "'"
I suggest you to use SQL parameters instead of concatenating to avoid this kind of errors.
Anyways, SQLite cannot compare string dates with time zones using BETWEEN
. In Android I suggest you to store the UTC timestamp instead, which will definitely work with BETWEEN
and also solve any time zone problem.