0

我创建了一个 prepop SQLite 数据库,其中存储了三列 _id、name 和 lname。所以我无法从数据库中检索数据。

PrepopSqliteDbActivity.java

public class PrepopSqliteDbActivity extends ListActivity {
private static final String DB_NAME = "test";
private static final String TABLE_NAME = "friends";
private static final String FRIEND_ID = "_id";
private static final String FRIEND_NAME = "name";
private static final String FRIEND_LNAME = "lname";

private SQLiteDatabase database;
private ListView listView;
private ArrayList<String> friends;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this,
            DB_NAME);
    database = dbOpenHelper.openDataBase();
    fillFreinds();
    setUpList();
}

private void setUpList() {
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, friends));
    listView = getListView();

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(getApplicationContext(),
                    ((TextView) view).getText().toString(),
                    Toast.LENGTH_SHORT).show();
        }
    });
}

private void fillFreinds() {
    friends = new ArrayList<String>();
    Cursor friendCursor = database.query(TABLE_NAME, new String[] {
            FRIEND_ID, FRIEND_NAME, FRIEND_LNAME }, null, null, null, null,
            FRIEND_NAME, FRIEND_LNAME);

    friendCursor.moveToFirst();
    if (!friendCursor.isAfterLast()) {
        do {
            String name = friendCursor.getString(1);
            String lname = friendCursor.getString(2);
            friends.add(name);
            friends.add(lname);
        } while (friendCursor.moveToNext());
    }
    friendCursor.close();
}

}

错误是 = 非法参数异常:无效的 LIMIT 子句:lname

4

1 回答 1

0

最后一列的值应该是整数。请将其从 FRIEND_LNAME 更改为所需的整数值。

Cursor friendCursor = database.query(TABLE_NAME, new String[] {
        FRIEND_ID, FRIEND_NAME, FRIEND_LNAME }, null, null, null, null,
        FRIEND_NAME, "5");
于 2013-08-29T06:41:47.440 回答