gpt4 book ai didi

java - Android SQLiteException。为什么会发生这种情况?

转载 作者:行者123 更新时间:2023-12-02 12:21:07 27 4
gpt4 key购买 nike

这是从SQLiteDatebase获取表RECORD的代码。在 try block 中出现问题并且总是抛出 SQLiteException。我在这里想做的就是将表“RECORD”的每一行转换为字符串并将其添加到 ArrayList 中,然后将数组连接到 ArrayAdapter,最后使用ListView来显示数组。

package com.example.tommy.stop_watch_real;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import java.util.ArrayList;

public class RecordList extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record_list);
ListView temp = (ListView) findViewById(R.id.recordList);
ArrayList<String> arrayList = new ArrayList<String>();
ArrayAdapter<String> adapter;

**try {
SQLiteOpenHelper dataBaseHelper = new DataBaseHelper(this);
SQLiteDatabase db = dataBaseHelper.getReadableDatabase();
Cursor cursor = db.query("RECORD", new String[] {"_id, DATE, MINUTES"}, null, null,null,null,null);
while (cursor.moveToFirst()){
String id = Integer.toString(cursor.getInt(0));
String date = cursor.getString(1);
String minutes = Integer.toString(cursor.getInt(2));
String finalValue = id + "|" + date + "|" + minutes;
arrayList.add(finalValue);
}
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
arrayList);
temp.setAdapter(adapter);
cursor.close();
db.close();}**
catch (SQLiteException e) {
Toast.makeText(RecordList.this, "Database not available", Toast.LENGTH_LONG).show();
}

}
}

这是我的 datebaseHelper 的代码:

class DataBaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "Record"; // name of the database
private static final int DB_VERSION = 1; //Version

DataBaseHelper (Context context){
super(context, DB_NAME, null, DB_VERSION);
}

public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE RECORD ("
+ "_id INTEGER PRIMARY KEY AUTOINCERMENT, "
+ "DATE STRING," //careful
+ "MINUTES INTEGER" + ");"
);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
//no need to worry about this yet.
}
}

toast 总是出现,这意味着 try block 中出现了错误。我一周前刚刚开始android开发,我不知道我的代码出了什么问题。如果您能给我一些指导,那就太好了。

提前谢谢您。

最佳答案

这部分对我来说似乎是错误的。

 while (cursor.moveToFirst()){
String id = Integer.toString(cursor.getInt(0));
String date = cursor.getString(1);
String minutes = Integer.toString(cursor.getInt(2));
String finalValue = id + "|" + date + "|" + minutes;
arrayList.add(finalValue);
}

我不确定您想要实现什么目标,但从数据库获取数据的正确方法是这样的:

 cursor.moveToFirst()
while (cursor.isAfterLast()){
String id = Integer.toString(cursor.getInt(0));
String date = cursor.getString(1);
String minutes = Integer.toString(cursor.getInt(2));
String finalValue = id + "|" + date + "|" + minutes;
arrayList.add(finalValue);
}

关于java - Android SQLiteException。为什么会发生这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45773110/

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