如何从Android Studio中的数据库中获取特定数据?
当我单击“查看数据按钮”时,唯一应该出现的数据应该是员工的个人数据。员工ID。
这是我的代码:
public void viewAll() {
btnview.setOnClickListener(
new View.OnClickListener() {
@Override`enter code here`
public void onClick(View v) {
Cursor res = EmployeeData.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Nothing found");
return;}
如何从现有数据库中获取特定数据?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用选择 s s 子句的方法替换
getalldata
(从单击项目中提取员工后)。然后,您需要通过从中提取数据来处理光标(如果存在)。
因此,您可以在类中具有以下内容,以扩展SqliteOpenHelper: -
明显
“ the_table”
和“雇员”
应由实际名称代替。
参见Query为了解释该方法的参数。
查询
(具有4个签名的方法)是一种便利方法,它返回a光标>光标
对象。- 它代表您的SQL生成SQL,例如以上将生成SQL
从the_table中select * select * employeeid =?
- 在哪里?被绑定(防止SQL注入)通过SQLITE绑定到传递的ID值。
从
光标
中提取数据时,您可以依靠光标
如果无法移动到第一行(即没有第一行),则会返回false。因此提取数据可能沿着: -You need to replace the
getAllData
with a method that SELECTs the appropriate data using a WHERE clause which is invoked with the employeeId being passed to it (after extracting the employeeId from the clicked item ).You then need to process the Cursor by extracting the data from it, if any exists.
So you could have something like the following in your class that extends SQLiteOpenHelper :-
obviously
"the_table"
and"employeeId"
should be replaced by the actual names.see Query for an explanation of the method's parameters.
the
Query
(method which has 4 signatures) is a convenience method that returns aCursor
object.-It generates the SQL on your behalf e.g. the above would generate the SQL
SELECT * FROM the_table WHERE employeeId=?
- where the ? is bound (prevents SQL Injection) to the passed id value by SQLite.
When extracting the data from the
Cursor
, rather than checking the count, you can rely upon the fact that theCursor
's will return false if it cannot move to the first row (i.e. there is no first row). So extracting the data could be along the lines of:-