所以我需要任何人帮我扫描我的代码中的问题。每次我尝试运行它时页面都会崩溃。我认为问题是由于以下代码而发生的。
DBController.java
public ArrayList<ProfileDB> getResult(String profileDOB, String profileSex,
String profileWeight, String profileHeight, String textWorkout,
String textLevel, String textWorkWeight, String textResult) {
ArrayList<ProfileDB> profileList = new ArrayList<ProfileDB>();
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.query(TABLE_NAME, new String[] { KEY_ID,
KEY_NAME, KEY_DOB, KEY_SEX, KEY_WEIGHT, KEY_HEIGHT,
KEY_WORKOUT, KEY_LEVEL, KEY_WORKWEIGHT, KEY_RESULT }, KEY_DOB
+ "='" + profileDOB + KEY_SEX + "='" + profileSex + KEY_WEIGHT
+ "='" + profileWeight + KEY_HEIGHT + "='" + profileHeight
+ KEY_WORKOUT + "='" + textWorkout + KEY_LEVEL + "='"
+ textLevel + KEY_WORKWEIGHT + "='" + textWorkWeight + "' and "
+ KEY_RESULT + "='" + textResult + "'", null, null, null, null);
if (cursor.moveToFirst()) {
do {
ProfileDB p = new ProfileDB(cursor.getInt(0),
cursor.getString(1), cursor.getString(2),
cursor.getString(3), cursor.getString(4),
cursor.getString(5), cursor.getString(6),
cursor.getString(7), cursor.getString(8),
cursor.getString(9));
profileList.add(p);
} while (cursor.moveToNext());
}
return profileList;
}
Continue.java
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
parent.getId();
ArrayList<ProfileDB> profileList = database.getResult(null, null, null,
null, null, null, null, textResult.getText().toString());
String str = "";
for (int i = 0; i < profileList.size(); i++) {
str += profileList.get(i).getTextResult() + "\n";
}
textResult.setText(str);
这是我从LogCat获得的错误。
05-08 00:34:55.072: E/SQLiteLog(32596): (1) near "nullprofileWeight": syntax error
05-08 00:34:55.072: E/AndroidRuntime(32596): FATAL EXCEPTION: main
05-08 00:34:55.072: E/AndroidRuntime(32596): Process: com.etty.theiglove2, PID: 32596
05-08 00:34:55.072: E/AndroidRuntime(32596): android.database.sqlite.SQLiteException: near "nullprofileWeight": syntax error (code 1): , while compiling: SELECT profileId, profileName, profileDOB, profileSex, profileWeight, profileHeight, textWorkout, textLevel, textWorkWeight, textResult FROM profiles WHERE profileDOB='nullprofileSex='nullprofileWeight='nullprofileHeight='nulltextWorkout='nulltextLevel='nulltextWorkWeight='null' and textResult='100
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1121)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:694)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1436)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1283)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1154)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1322)
05-08 00:34:55.072: E/AndroidRuntime(32596): at com.etty.theiglove2.DBController.getResult(DBController.java:177)
05-08 00:34:55.072: E/AndroidRuntime(32596): at com.etty.theiglove2.Continue.onItemSelected(Continue.java:82)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.widget.AdapterView.fireOnSelected(AdapterView.java:956)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.widget.AdapterView.access$200(AdapterView.java:49)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:920)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.os.Handler.handleCallback(Handler.java:733)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.os.Handler.dispatchMessage(Handler.java:95)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.os.Looper.loop(Looper.java:157)
05-08 00:34:55.072: E/AndroidRuntime(32596): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-08 00:34:55.072: E/AndroidRuntime(32596): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 00:34:55.072: E/AndroidRuntime(32596): at java.lang.reflect.Method.invoke(Method.java:515)
05-08 00:34:55.072: E/AndroidRuntime(32596): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-08 00:34:55.072: E/AndroidRuntime(32596): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-08 00:34:55.072: E/AndroidRuntime(32596): at dalvik.system.NativeStart.main(Native Method)
希望有人能帮助我找到问题所在。谢谢!
答案 0 :(得分:0)
selection
调用的query
参数除了被格式化以使其不可读之外,还远不及有效的SQL;个别比较表达式必须与AND
结合使用。
此外,当字符串值中的任何一个包含引号时,将直接将字符串值放入SQL字符串中;改为使用参数:
Cursor cursor = database.query(TABLE_NAME,
new String[] { KEY_ID, KEY_NAME, KEY_DOB, KEY_SEX,
KEY_WEIGHT, KEY_HEIGHT, KEY_WORKOUT,
KEY_LEVEL, KEY_WORKWEIGHT, KEY_RESULT },
KEY_DOB + "= ? AND " +
KEY_SEX + "= ? AND " +
KEY_WEIGHT + "= ? AND " +
KEY_HEIGHT + "= ? AND " +
KEY_WORKOUT + "= ? AND " +
KEY_LEVEL + "= ? AND " +
KEY_WORKWEIGHT + "= ? AND " +
KEY_RESULT + "= ?",
new String[] { profileDOB, profileSex, profileWeight, profileHeight,
textWorkout, textLevel, textWorkWeight, textResult },
null, null, null);
答案 1 :(得分:-1)
您的列名和值之间似乎没有空格。试试这样:
KEY_DOB + "='" + profileDOB + " " + KEY_SEX + "='" + profileSex + " " + ...