query()方法:
query( table, columns, selection, selectionArgs, groupBy, having, orderBy, limit );
参数含义:
- table:表名。
- columns:要查询出来的列名。
- selection:查询条件子句。
- selectionArgs:对应于selection语句中占位符的值。
- groupBy:分组。相当于select语句group by关键字后面的部分。
- having:分组后聚合的过滤条件。相当于select语句having关键字后面的部分。
- orderBy:排序。相当于select语句order by关键字后面的部分 ASC或DESC。
- limit:指定偏移量和获取的记录数。
已有表:
Id | Title | Link | UserName | Password |
---|---|---|---|---|
… | … | … | … | … |
10 | testTitle | testLink | testUserName | testPassword |
… | … | … | … | … |
假设要获取 Title 值为 testTitle 的行的数据,并显示到 TextView 中
则可以通过下列代码实现:
String pwdTitle = "testTitle";
TextView tvTitle, tvLink, tvUserName, tvPwd;
SQLiteDatabase db = openOrCreateDatabase("newItem.db", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS password (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"Title NVARCHAR, " +
"Link NVARCHAR, " +
"UserName NVARCHAR, " +
"Password NVARCHAR)");
Cursor cursor = db.query("password", new String[]{"Title, Link, UserName, Password"}, "Title=?", new String[]{pwdTitle}, null, null, null);
cursor.moveToFirst();
tvTitle.setText(cursor.getString(cursor.getColumnIndex("Title")));
tvLink.setText(cursor.getString(cursor.getColumnIndex("Link")));
tvUserName.setText(cursor.getString(cursor.getColumnIndex("UserName")));
tvPwd.setText(cursor.getString(cursor.getColumnIndex("Password")));
cursor.close();