Android(安卓)SQLite 使用 query 查询特定行数据
16lz
2021-01-26
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();
更多相关文章
- Android(安卓)中部分文字高亮显示方法
- Android(安卓)RoboGuice 使用指南(9):Untargetted Bindings
- android源码分析流程-init.c
- Android(安卓)封装的数据库管理操作类
- SQLite轻量级数据库的注意事项
- 在Android中为啥建议你用Message.obtain()方法获取Message对象,而
- android之SQLite数据库应用(二)
- mars Android视频第14讲中代码出现的错误分析——Handler中的rem
- Android(安卓)Studio 模板用法与自定义模板