2

我做了一个方法来查询和返回数据库的结果。当此方法在到达光标行时运行时,整个程序将崩溃。

这是它崩溃的行:

光标项 = rDb.query(DATABASE_TABLE, columns, "_id like '1'", null, null, null, null);

这些列是一个字符串,我有一个名为 _id 的列。我看不出语法有什么问题,请帮忙!

数据库类中的变量:

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "restaurants";

//Database Columns
public static final String COLUMN_ROWID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_TELEPHONE = "phone_number";
public static final String COLUMN_EMAIL ="email";
public static final String COLUMN_MENUURL = "menu_address";
public static final String COLUMN_WEBSITE = "website_address";
public static final String COLUMN_MOBILE = "mobile number";

//Database Table Create Statement
private static final String DATABASE_TABLECREATE = "create table "
        + DATABASE_TABLE + " (" + COLUMN_ROWID
        + " integer not null, " 
        + COLUMN_NAME + " text not null, " 
        + COLUMN_TELEPHONE + " integer not null, "
        + COLUMN_EMAIL + " text not null, "
        + COLUMN_MENUURL + " text not null, "
        + COLUMN_WEBSITE + " text not null, "
        + COLUMN_MOBILE + " integer not null) ;";

//the database itself
private SQLiteDatabase rDb;

列:

String[] columns = new String[]{COLUMN_ROWID, COLUMN_NAME, COLUMN_TELEPHONE, COLUMN_EMAIL,     COLUMN_MENUURL, COLUMN_WEBSITE, COLUMN_MOBILE};

11-05 16:15:05.633:D/gralloc_goldfish(882):没有检测到 GPU 仿真的仿真器。11-05 16:15:07.533: D/Button(882): 点击 11-05 16:15:07.553: D/string 声明(882): 就在查询之前 11-05 16:15:07.553: D/AndroidRuntime( 882): 关闭 VM 11-05 16:15:07.553: W/dalvikvm(882): threadid=1: 线程退出未捕获异常 (group=0x41465700) 11-05 16:15:07.564: E/AndroidRuntime(882 ):致命异常:主要 11-05 16:15:07.564:E/AndroidRuntime(882):java.lang.NullPointerException 11-05 16:15:07.564:E/AndroidRuntime(882):在 com.example.testforthingy。 RestaurantDatabase.getData(RestaurantDatabase.java:107) 11-05 16:15:07.564: E/AndroidRuntime(882): at com.example.testforthingy.RestaurantDisplay$1.onClick(RestaurantDisplay.java:26) 11-05 16:15 :07.564: E/AndroidRuntime(882): 在 android.view.View.performClick(View.java:4240) 11-05 16:15:

public String getData(){
    String rName= "";
    String[] columns = new String[]{COLUMN_ROWID, COLUMN_NAME, COLUMN_TELEPHONE, COLUMN_EMAIL, COLUMN_MENUURL, COLUMN_WEBSITE, COLUMN_MOBILE};

    Cursor item = rDb.query(DATABASE_TABLE, columns, "name like 'Terry'", null, null, null, null);

    for(item.moveToFirst();item.moveToNext(); item.isAfterLast()) {
        rName = item.getString(item.getColumnIndex(COLUMN_NAME));
    }

    return rName;
4

1 回答 1

0

_id 是整数,而不是字符串。并且列必须是 String[] 类型,而不是 String,否则您的代码甚至无法编译。所以我建议你试试:

Cursor item = rDb.query(DATABASE_TABLE, columns, "_id=1", null, null, null, null);
于 2013-11-05T21:34:34.233 回答