0

我正在尝试使用 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
4

3 回答 3

0
android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, persons_name, persons_number FROM peopleTable

您的表没有名为 的列_id。在那里添加它并重新创建数据库。或者从您的选择列中删除它。

似乎存在以下 SQL 语法问题onCreate()

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + "INTEGER PRIMARY KEY, " +
KEY_NAME + "TEXT NOT NULL, " +
KEY_NUMBER + "TEXT NOT NULL);"

列名和类型之间应该有空格,例如

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_NUMBER + " TEXT NOT NULL);"

这是一个棘手的问题,因为 eg_idINTEGER PRIMARY KEY是一个有效的列规范,它只是没有指定一个名为_id.

像这样更改架构时,请记住提高数据库版本号。

于 2013-10-22T13:20:35.010 回答
0

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

c.close(); // add this line to your code
return result;
于 2013-10-22T11:46:06.020 回答
0

初始化一个ArrayList<String> result = new ArrayList<String>();代替String result

将循环更改为:

while(c.moveToNext())
{
result.add(c.getString(iRow) + "   " + c.getString(iName) + "    " + c.getString(iNumber));
Log.i("data", c.getString(iRow) + "   " + c.getString(iName) + "    " + c.getString(iNumber));
}

c.close();

return result;

将您的返回类型更改getData()ArrayList

最后,您可以使用类ArrayList在控制台日志中进行迭代和打印。Log检查是否获取日志...

于 2013-10-22T12:12:51.373 回答