0

我制作了一个数据库和一个工作正常的表。现在我希望检索表中输入的数据并将其显示在 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.
4

1 回答 1

0

您的查询无效。您需要将其更正为:

String query = "select " + DBHElper.COLUMN_COMMENT + " from "
               + DBHElper.TABLE_COMMENTS;
db.rawQuery(query, null);

在您的来源查询中,您写道:

"SELECT COLUMN_COMMENT " + TABLE_COMMENTS 

你有几个错误:

  • 您缺少FROM强制性条款
  • 您将列命名为评论并分配 了 COLUMN_COMMENT。这应该是正确的,但是您需要将其分配给查询作为变量(保存列名值)而不是文字。

第二个大错误:

你忘了创建数据库。所以你必须在你的onCreate()方法中创建数据库:

@Override
public void onCreate(SQLiteDatabase db) {
   String createQuery = "create table " + DBHElper.TABLE_COMMENTS + "("
   + DBHElper.COLUMN_ID + " integer primary key autoincrement, "
   + DBHElper.COLUMN_COMMENT + " text" + ")";
   db.execSQL(qreateQuery);
}

我还建议您也实施 onUpgrade() 方法:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   String upgradeQuery = "drop table if exists " + DBHElper.TABLE_COMMENTS;
   db.execSQL(upgradeQuery);
   onCreate(db);
}
于 2013-04-09T11:44:25.213 回答