从现有数据库和表中检索文本

时间:2013-04-09 11:38:00

标签: android sql sqlite

我制作了一个数据库和一个工作正常的表。现在我希望检索表中输入的数据并将其显示在TextView中。但我无法做到。我正在分享代码和日志猫。请帮忙

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.

1 个答案:

答案 0 :(得分: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);
}