android studio数据库增删查改源码
时间: 2023-09-29 20:06:15 浏览: 92
android studio 数据库的增删查改
4星 · 用户满意度95%
以下是一个简单的Android Studio数据库增删查改的示例代码:
1. 首先,在你的项目中创建一个DatabaseHelper类,继承自SQLiteOpenHelper。
```java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
//构造方法
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS student(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name VARCHAR, " +
"age INTEGER, " +
"gender VARCHAR)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS student";
db.execSQL(sql);
onCreate(db);
}
}
```
2. 创建一个数据访问类StudentDAO,封装增删查改操作。
```java
public class StudentDAO {
private static final String TABLE_NAME = "student";
//插入数据
public static long insert(Context context, ContentValues values) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();
long id = db.insert(TABLE_NAME, null, values);
db.close();
return id;
}
//删除数据
public static int delete(Context context, String whereClause, String[] whereArgs) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();
int count = db.delete(TABLE_NAME, whereClause, whereArgs);
db.close();
return count;
}
//更新数据
public static int update(Context context, ContentValues values, String whereClause, String[] whereArgs) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();
int count = db.update(TABLE_NAME, values, whereClause, whereArgs);
db.close();
return count;
}
//查询数据
public static List<Student> query(Context context, String selection, String[] selectionArgs) {
List<Student> list = new ArrayList<>();
DatabaseHelper dbHelper = new DatabaseHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, selection, selectionArgs, 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"));
String gender = cursor.getString(cursor.getColumnIndex("gender"));
Student student = new Student(id, name, age, gender);
list.add(student);
}
cursor.close();
db.close();
return list;
}
}
```
3. 在你的Activity中使用StudentDAO类进行增删查改操作。
```java
//插入数据
ContentValues values = new ContentValues();
values.put("name", "张三");
values.put("age", 18);
values.put("gender", "男");
long id = StudentDAO.insert(this, values);
//删除数据
String whereClause = "name=?";
String[] whereArgs = {"张三"};
int count = StudentDAO.delete(this, whereClause, whereArgs);
//更新数据
ContentValues values = new ContentValues();
values.put("name", "李四");
String whereClause = "name=?";
String[] whereArgs = {"张三"};
int count = StudentDAO.update(this, values, whereClause, whereArgs);
//查询数据
String selection = "age=?";
String[] selectionArgs = {"18"};
List<Student> list = StudentDAO.query(this, selection, selectionArgs);
```
阅读全文