0

如何从外部数据库中检索行并在文本视图中显示它们?我尝试了一些代码,但应用程序停止了。

在我的数据库助手中,我添加了该代码:

public String[] getData(String l) {
    // TODO Auto-generated method stub
    String[] columns = new String[] { DB_ID, DB_name, DB_desc };
    // FILTERS DEPENDING ON THE CATEGORY TYPE PASSED IN THE PARAMETER
    Cursor c = mDataBase.rawQuery("SELECT _id,name,description FROM facilities WHERE name='" + l + "'", null);
    String[] result = new String[] { null, null, null, null, null };

    int iId = c.getColumnIndex(DB_ID);
    int iName = c.getColumnIndex(DB_name);
    int iDesc = c.getColumnIndex(DB_desc);

    c.moveToFirst();

    result[0] = c.getString(iId);
    result[1] = c.getString(iName);
    result[2] = c.getString(iDesc);
    return result;
}

我希望在 textview 中显示字符串的活动中的这段代码:

     for(int i=0;i<4;i++){
        // dbhelper.open();
        String[] data =dbhelper.getData(namee);
        // dbhelper.close();
        txt.append("\n"+data[3]+"\n");}
     }

这是我尝试过的另一个代码,来自这个 url: Return all data from a SQLite DB into two columns in Android

4

2 回答 2

0

I used this in one of my app.... This may help.

I declared the following in the main activity itself

DatabaseHelper myDbHelper = new DatabaseHelper(this);
.
.
try {
myDbHelper.createDataBase();
}
.
.
.
db = myDbHelper.getWritableDatabase();// or db = myDbHelper.getReadableDatabase();
.
.
.
//In another function
Cursor c = db.rawQuery("Whatever you want to query");
String[] s=new String[]{"The fields(columns) you want queried and used in textbox"};
于 2013-11-06T19:23:35.093 回答
0

您可以发布 LogCat 条目吗?这条线有什么作用?

String[] result=new String[]{null,null,null,null,null};

尝试

String[] result = new String[5];

反而;

于 2013-11-06T19:04:26.790 回答