我正在尝试从原始查询返回到我的SQLITE数据库的字符串。
问题是我正在桥接两个表,所以我不确定如何调用包含颜色的列(需要的值)。
如何从SQL查询返回字符串值到结果variable
?
public String studentColor(){
SQLiteDatabase db = getWritableDatabase();
String queryStudent = "SELECT students.color FROM students, comments WHERE comments.author = students.name;";
Cursor cursor = db.rawQuery(queryStudent, null);
String result = // get students.color from cursor;
return result
}
由于
修改
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.me.echoboard, PID: 21552
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
at android.view.View$PerformClick.run(View.java:23645)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
at android.view.View$PerformClick.run(View.java:23645)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.me.echoboard.DBHandler.studentColor(DBHandler.java:121)
at com.example.me.echoboard.login.onClick(login.java:37)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
at android.view.View$PerformClick.run(View.java:23645)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
答案 0 :(得分:2)
你没有在光标内移动,所以它位于-1(第一行之前)
使用以下内容移动到游标的第一行(如果有的话,如果不是空字符串将返回): -
public String studentColor(){
SQLiteDatabase db = getWritableDatabase();
String result = "";
String queryStudent = "SELECT students.color FROM students, comments WHERE comments.author = students.name;";
Cursor cursor = db.rawQuery(queryStudent, null);
if (cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex("students.color"));
}
cursor.close();
return result
}
答案 1 :(得分:0)
您需要做的是使用此更改查询,然后使用光标
"SELECT students.color as color FROM students, comments WHERE comments.author = students.name;";
光标
String result = cursor.getString(cursor.getColumnIndex("color"));