安卓编写1个StudentDao类,能够获取student(id,name,sex,phone)表格的全部数据(源代码说明)
时间: 2023-02-09 09:38:14 浏览: 140
这是一个可以获取 `student` 表格全部数据的 StudentDao 类的示例代码:
```
public class StudentDao {
private SQLiteDatabase db;
public StudentDao(SQLiteDatabase db) {
this.db = db;
}
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
Cursor cursor = db.query(
"student", // 表名
new String[]{"id", "name", "sex", "phone"}, // 列名数组
null, // WHERE
null, // WHERE 参数
null, // GROUP BY
null, // HAVING
null); // ORDER BY
while (cursor.moveToNext()) {
long id = cursor.getLong(0);
String name = cursor.getString(1);
String sex = cursor.getString(2);
String phone = cursor.getString(3);
students.add(new Student(id, name, sex, phone));
}
cursor.close();
return students;
}
}
```
其中,`Student` 类是表示学生信息的类,需要你自己定义。
使用方法是:
```
StudentDao dao = new StudentDao(db);
List<Student> students = dao.getAllStudents();
```
其中,`db` 是一个 `SQLiteDatabase` 对象,表示数据库连接。
希望这能帮到你!
阅读全文
相关推荐
















