我制作了一个数据库和一个工作正常的表。现在我希望检索表中输入的数据并将其显示在 TextView 中。但我做不到。我正在共享代码并记录 cat 。请帮忙
Helper类(这里指的是已经存在的DB):package com.example.query;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHElper extends SQLiteOpenHelper{
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "comment";
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
public DBHElper(Context context) {
super(context,DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
MainActivity(这里我查询了表格并在textview上显示)
package com.example.query;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "comment";
TextView tv;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
QueryDb();
}
private void QueryDb() {
// TODO Auto-generated method stub
try
{
DBHElper helper = new DBHElper(this.getApplicationContext());
db= helper.getWritableDatabase();
Cursor c = db.rawQuery("SELECT COLUMN_COMMENT " +
TABLE_COMMENTS
, null);
if(c!=null)
{
if(c.moveToFirst())
{
do
{
String comment=c.getString(1);
tv =(TextView) findViewById(R.id.labels);
tv.setText(comment);
}while(c.moveToNext());
}
}
}catch(SQLiteException se)
{
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
LOGCAT
04-09 16:59:46.460: I/SqliteDatabaseCpp(1662): sqlite returned: error code = 1, msg = no such column: COLUMN_COMMENT, db=/data/data/com.example.query/databases/commments.db
04-09 16:59:46.470: E/MainActivity(1662): Could not create or Open the database
04-09 16:59:46.610: D/gralloc_goldfish(1662): Emulator without GPU emulation detected.