gpt4 book ai didi

android - 查看sqlite数据库的代码错误

转载 作者:行者123 更新时间:2023-12-03 17:36:16 26 4
gpt4 key购买 nike

我正在尝试使用Cursor在TextView中查看数据库的内容,但是当我尝试运行该应用程序时崩溃了...我无法识别该错误

package aditya.testapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SqlData {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_NUMBER = "persons_number";

private static final String DATABASE_NAME = "DataDb";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 2 ;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

private static class DbHelper extends SQLiteOpenHelper{

public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}


// onCreate method only used once when a database is created....other times onUpgrade methode is used
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + "INTEGER PRIMARY KEY, " +
KEY_NAME + "TEXT NOT NULL, " +
KEY_NUMBER + "TEXT NOT NULL);"
);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}}

// initializing context ourContext
public SqlData (Context c){
ourContext = c;
}
//XX
public SqlData open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}

public long createEntry(String name, String number) {
// TODO Auto-generated method stub
// in table ContentValues is used instead of bundle
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_NUMBER, number);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public String getData() {

// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID , KEY_NAME , KEY_NUMBER};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";

int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iNumber = c.getColumnIndex(KEY_NUMBER);

for( c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iNumber) + "/n";
}
c.close();
return result;


}
}

并且此代码是用于查看数据库的类的
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SqlView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView lblSqlInfo = (TextView) findViewById(R.id.lblSqlInfo);
SqlData info = new SqlData(this);
info.open();
String data = info.getData(); // getData is a method in SqlData class
info.close();
lblSqlInfo.setText(data);
}

}

请帮助找出错误...。日志猫显示此错误
10-22 12:51:56.483: E/AndroidRuntime(1815): FATAL EXCEPTION: main
10-22 12:51:56.483: E/AndroidRuntime(1815): java.lang.RuntimeException: Unable to start activity ComponentInfo{aditya.testapp/aditya.testapp.SqlView}: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, persons_name, persons_number FROM peopleTable
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.os.Looper.loop(Looper.java:137)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-22 12:51:56.483: E/AndroidRuntime(1815): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 12:51:56.483: E/AndroidRuntime(1815): at java.lang.reflect.Method.invoke(Method.java:511)
10-22 12:51:56.483: E/AndroidRuntime(1815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-22 12:51:56.483: E/AndroidRuntime(1815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-22 12:51:56.483: E/AndroidRuntime(1815): at dalvik.system.NativeStart.main(Native Method)
10-22 12:51:56.483: E/AndroidRuntime(1815): Caused by: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, persons_name, persons_number FROM peopleTable
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
10-22 12:51:56.483: E/AndroidRuntime(1815): at aditya.testapp.SqlData.getData(SqlData.java:79)
10-22 12:51:56.483: E/AndroidRuntime(1815): at aditya.testapp.SqlView.onCreate(SqlView.java:16)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.app.Activity.performCreate(Activity.java:5104)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-22 12:51:56.483: E/AndroidRuntime(1815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-22 12:51:56.483: E/AndroidRuntime(1815): ... 11 more

最佳答案

使用完游标后,您没有关闭游标。在return result;之前添加以下行:

c.close(); // add this line to your code
return result;

关于android - 查看sqlite数据库的代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19516707/

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