数据库
首页 > 数据库> > android-如何查询SQLite数据库

android-如何查询SQLite数据库

作者:互联网

这是我的桌子:

private static final String CREATE_TABLE_EMPLOYEES = "CREATE TABLE "+ TABLENAME + "(" +
            COLUMNS[0] + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , " +
            COLUMNS[1] + " TEXT NOT NULL , " +
            COLUMNS[2] + " TEXT NOT NULL , " +
            COLUMNS[3] + " TEXT NOT NULL , " +
            COLUMNS[4] + " TEXT NOT NULL , " +
            COLUMNS[5] + " TEXT NOT NULL  " +
            ");";

并查询数据库中的所有数据:

public List<Employee> getEmployees() {
        List<Employee> employees = new ArrayList<Employee>();
        Cursor cur = db.query(dbHelper.TABLENAME, columns, null, null, null, null, null);
        cur.moveToFirst(); // need to start the cursor first...!
        while(!cur.isAfterLast()) { // while not end of data stored in table...
            Employee emp = new Employee();
            emp.setId(cur.getInt(0));
            emp.setName(cur.getString(1));
            emp.setCharge(cur.getString(2));
            emp.setDepartament(cur.getString(3));
            emp.setPhone(cur.getString(4));
            emp.setEmail(cur.getString(5));
            employees.add(emp);
            cur.moveToNext(); // next loop
        }
        cur.close(); // !important
        return employees;
    }

如果员工姓名==“ ali”,我想查询所有数据

请帮我.

解决方法:

I want to query all data if employee name ==”ali”.

查询方法中的第3和第4参数可用于在查询中添加WHERE子句.

这样做:

Cursor cur = db.query(dbHelper.TABLENAME, columns, 
                      "name=?", 
                      new String[] { "ali" }, 
                      null, null, null);

标签:sqlite,android-sqlite,android
来源: https://codeday.me/bug/20191119/2035927.html