gpt4 book ai didi

android - 在 SQLite 数据库上检索数据时出错

转载 作者:行者123 更新时间:2023-11-29 17:23:47 26 4
gpt4 key购买 nike

我在检索数据时遇到问题,它说确保在从中访问数据之前正确初始化游标我无法弄清楚我的游标是否正确初始化的问题,

下面是我的 logcat。

      02-16 20:34:49.678 22333-22333/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.computer.mathkiddofinal:second, PID: 22333
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.computer.mathkiddofinal/com.example.computer.mathkiddofinal.Database.Record_DB}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetLong(Native Method)
at android.database.CursorWindow.getLong(CursorWindow.java:507)
at android.database.CursorWindow.getInt(CursorWindow.java:574)
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:80)
at com.example.computer.mathkiddofinal.Database.Record_DB.onCreate(Record_DB.java:55)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5292) 
at java.lang.reflect.Method.invokeNative(Native Method) 

Record_DB.java,这一行出错 int postwh = res.getInt(res.getColumnIndex("POSTWH"));

public class Record_DB extends Activity {
Final_Result_Grade_4 asd;
TextView txtDB;
DatabaseHelper myDb;
ArrayList<String> results = new ArrayList<String>();
ArrayList<Integer> lpostwh = new ArrayList<Integer>();
ArrayList<Integer> lpostmul = new ArrayList<Integer>();
ArrayList<Integer> lpostdiv = new ArrayList<Integer>();
ArrayList<Integer> lprewh = new ArrayList<Integer>();
ArrayList<Integer> lpremul = new ArrayList<Integer>();
ArrayList<Integer> lprediv = new ArrayList<Integer>();
ListView errorListview;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record__db);
errorListview = (ListView) findViewById(R.id.listtt);
//TextView txtDB = (TextView) findViewById(R.id.txtDB);
// txtDB.setMovementMethod(new ScrollingMovementMethod());
myDb = new DatabaseHelper(this);
Cursor res = myDb.getAllData();
int cols = res.getColumnCount();
if (res.getCount() == 0) {
Toast.makeText(Record_DB.this, "no data", Toast.LENGTH_SHORT).show();
return;
}

while (res.moveToNext()) {
int id = res.getInt(res.getColumnIndex("ID"));
String name = res.getString(res.getColumnIndex("NAME"));
int pre = res.getInt(res.getColumnIndex("PRETEST"));
int post = res.getInt(res.getColumnIndex("POSTTEST"));
int postwh = res.getInt(res.getColumnIndex("POSTWH"));
int postmul = res.getInt(res.getColumnIndex("POSTMUL"));
int postdiv = res.getInt(res.getColumnIndex("POSTDIV"));
int prewh = res.getInt(res.getColumnIndex("PREWH"));
int premul = res.getInt(res.getColumnIndex("PREMUL"));
int prediv = res.getInt(res.getColumnIndex("PREDIV"));
results.add(""+postwh + postmul + postdiv + prewh + premul + prediv );


}

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results);
errorListview.setAdapter(adapter);

我真的不知道如何解决这个问题,任何人都可以提供帮助。非常感谢谢谢

数据库助手.java

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "gradefour.db";
public static final String TABLE_NAME = "grade_four_table";
public static final String COL_1= "ID";
public static final String COL_2= "NAME";
public static final String COL_3= "PRETEST";
public static final String COL_4= "POSTTEST";
public static final String COL_5= "POSTWH";
public static final String COL_6= "POSTMUL";
public static final String COL_7= "POSTDIV";
public static final String COL_8= "PREWH";
public static final String COL_9= "PREMUL";
public static final String COL_10= "PREDIV";

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);

}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PRETEST INTEGER,POSTTEST INTEGER,POSTWH INTEGER,POSTMUL INTEGER,POSTDIV INTEGER,PREWH INTEGER,PREMUL INTEGER,PREDIV INTEGER) ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
public boolean inserData(String name, Integer pretest, Integer posttest, Integer postwh, Integer postmul, Integer postdiv,
Integer prewh, Integer premul, Integer prediv){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,pretest);
contentValues.put(COL_4,posttest);
contentValues.put(COL_5,postwh);
contentValues.put(COL_6,postmul);
contentValues.put(COL_7,postdiv);
contentValues.put(COL_8,prewh);
contentValues.put(COL_9,premul);
contentValues.put(COL_10,prediv);
long result = db.insert(TABLE_NAME,null,contentValues);
if(result==-1){
return false;
}
else
{
return true;
}
}
public Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);

return res;
}
}

最佳答案

根据您的评论..!!

在安装您的应用后添加了大约 6 列。所以你必须需要重新安装应用程序,然后在创建新表后并且将在你的表中添加 6 列。

只有当您的应用程序已安装并且您的表已创建时,您的 OnCreate() 方法才会被调用,因此当您需要进行一些更改时,您有两个选择

  1. 更改 OnUpgrade() 方法并更新您的应用。 (为实时应用程序做)

  2. 卸载应用程序并重新安装。 (做测试)

关于android - 在 SQLite 数据库上检索数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35432988/

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