这是我的代码
前 3 个函数在数据库类中
public void onCreate(SQLiteDatabase db){
String creation_query3= "CREATE TABLE "
+ TABLE_Doctor + "(" + Doctor_ID
+ " INTEGER PRIMARY KEY," + Doctor_NAME
+ " TEXT, " + Doctor_PHONE + "TEXT )";
db.execSQL(creation_query3);
}
public void adddoctor(String pName, String ph){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(Doctor_NAME,pName);
cv.put(Doctor_PHONE,ph);
db.insert(TABLE_Doctor, null, cv);
db.close();
}
public String getAllDoctors(){
Cursor cursor = getReadableDatabase().rawQuery("SELECT "+Doctor_NAME+" FROM "+ TABLE_Doctor, null);
cursor.moveToFirst();
String s=cursor.getString(cursor.getColumnIndexOrThrow(Doctor_NAME));
cursor.close();
return s;
}
这是在主要课程中
{
String test=db.getAllDoctors();
textView.setText(test.toString());
}
这是错误:
01-17 14:30:02.490 27929-27929/com.example.ghumdan.finalprojectkg
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.ghumdan.finalprojectkg/com.example.ghumdan.finalprojectkg.AddPatient}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.ghumdan.finalprojectkg.Database.getAllDoctors(Database.java:107)
at com.example.ghumdan.finalprojectkg.AddPatient.onCreate(AddPatient.java:35)
at android.app.Activity.performCreate(Activity.java:5250)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)