我试图通过使用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);
}
}
请帮助找出错误....日志cat显示此错误
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
答案 0 :(得分:0)
使用它后,您没有关闭光标。在return result;
之前添加以下行:
c.close(); // add this line to your code
return result;
答案 1 :(得分: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
类在控制台日志中打印。
检查是否获得日志......
答案 2 :(得分:0)
android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, persons_name, persons_number FROM peopleTable
您的表没有名为_id
的列。将其添加到那里并重新创建数据库。或者从选择列中删除它。
似乎onCreate()
中存在SQL语法问题:
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);"
这是一个棘手的问题,例如_idINTEGER PRIMARY KEY
是有效的列规范,它不会指定名为_id
的列。
更改此类架构时,请记住提高数据库版本号。