很抱歉,我无法提供具体的课本案例内容。但是,以下是一个简单的Android数据库增删改查的示例代码,您可以根据自己的需求进行修改和使用:
- 创建数据库帮助类 DBHelper.java:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableQuery = "CREATE TABLE IF NOT EXISTS students (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER);";
db.execSQL(createTableQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTableQuery = "DROP TABLE IF EXISTS students;";
db.execSQL(dropTableQuery);
onCreate(db);
}
}
- 创建数据访问类 StudentDAO.java:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
private Context context;
private SQLiteDatabase database;
public StudentDAO(Context context) {
this.context = context;
DBHelper dbHelper = new DBHelper(context);
database = dbHelper.getWritableDatabase();
}
public void addStudent(String name, int age) {
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("age", age);
database.insert("students", null, contentValues);
}
public List<Student> getAllStudents() {
List<Student> studentList = new ArrayList<>();
Cursor cursor = database.query("students", null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
studentList.add(new Student(id, name, age));
}
cursor.close();
return studentList;
}
public void updateStudent(int id, String newName, int newAge) {
ContentValues contentValues = new ContentValues();
contentValues.put("name", newName);
contentValues.put("age", newAge);
database.update("students", contentValues, "_id=?", new String[]{String.valueOf(id)});
}
public void deleteStudent(int id) {
database.delete("students", "_id=?", new String[]{String.valueOf(id)});
}
}
- 创建学生类 Student.java:
public class Student {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
在你的活动或碎片中,你可以使用以下代码来进行数据库增删改查操作:
// 在活动或碎片中使用示例
StudentDAO studentDAO = new StudentDAO(this);
// 插入一条学生记录
studentDAO.addStudent("Tom", 20);
// 获取所有学生记录
List<Student> studentList = studentDAO.getAllStudents();
// 更新学生记录
studentDAO.updateStudent(1, "John", 22);
// 删除学生记录
studentDAO.deleteStudent(1);
请根据自己的需求进行适当的修改和扩展。
很抱歉,我无法提供具体的课本案例,因为我是一款人工智能助手,无法直接获取和展示外部资源。建议您在互联网上搜索相关的案例或教程,以了解更多关于安卓数据库增删改查的内容。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/147604.html