如何从prepopSQLite DB中检索数据

时间:2013-08-29 06:17:28

标签: android

我创建了一个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();
}

}

错误是= illegalArgumentException:无效的LIMIT子句:lname

1 个答案:

答案 0 :(得分:0)

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

Cursor friendCursor = database.query(TABLE_NAME, new String[] {
        FRIEND_ID, FRIEND_NAME, FRIEND_LNAME }, null, null, null, null,
        FRIEND_NAME, "5");