如何从Android Studio中的数据库中获取特定数据?

发布于 2025-02-11 04:50:40 字数 649 浏览 1 评论 0 原文

当我单击“查看数据按钮”时,唯一应该出现的数据应该是员工的个人数据。员工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;}
                    

如何从现有数据库中获取特定数据?

The only data that should appear when I click the "view data button" should be the employee's personal data by searching its employee id.

This is my code:

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;}
                    

How can I get the specific data from the existing database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2025-02-18 04:50:40

您需要使用选择 s s 子句的方法替换 getalldata (从单击项目中提取员工后)。

然后,您需要通过从中提取数据来处理光标(如果存在)。

因此,您可以在类中具有以下内容,以扩展SqliteOpenHelper: -

public Cursor getEmployeeDataById(long id) {
    return this.getWritableDatabase().query("the_table",null,"employeeId=?",new String[]{String.valueOf(id)},null, null,null);
}
  • 明显 “ the_table” “雇员” 应由实际名称代替。


  • 参见Query为了解释该方法的参数。

  • 查询 (具有4个签名的方法)是一种便利方法,它返回a 光标>光标 对象。
    - 它代表您的SQL生成SQL,例如以上将生成SQL 从the_table中select * select * employeeid =?
    - 在哪里?被绑定(防止SQL注入)通过SQLITE绑定到传递的ID值。

光标 中提取数据时,您可以依靠 光标 如果无法移动到第一行(即没有第一行),则会返回false。因此提取数据可能沿着: -

    Cursor csr = EmployeeData.getEmployeeDataById(extracted_employee_id);
    if (csr.moveToFirst()) {
        .... extract the data from the cursor
    } else {
        showMessage("Error","Nothing found");
    }

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 :-

public Cursor getEmployeeDataById(long id) {
    return this.getWritableDatabase().query("the_table",null,"employeeId=?",new String[]{String.valueOf(id)},null, null,null);
}
  • 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 a Cursor 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 the Cursor'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:-

    Cursor csr = EmployeeData.getEmployeeDataById(extracted_employee_id);
    if (csr.moveToFirst()) {
        .... extract the data from the cursor
    } else {
        showMessage("Error","Nothing found");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文