在 Android 开发中,我们经常需要对数据库进行管理操作,包括创建数据库、创建表、插入数据、更新数据、删除数据等操作。以下是一个简单的数据库管理操作类示例:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "my_database";
private static final int DATABASE_VERSION = 1;
// 表名
private static final String TABLE_NAME = "my_table";
// 列名
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
// 创建表的 SQL 语句
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_NAME + " TEXT)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void insertData(String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
db.insert(TABLE_NAME, null, values);
db.close();
}
public void updateData(int id, String newName) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, newName);
db.update(TABLE_NAME, values, COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
db.close();
}
public void deleteData(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
db.close();
}
public List<String> getAllData() {
List<String> dataList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
dataList.add(name);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return dataList;
}
}
在这个类中,我们定义了一个名为 DatabaseHelper
的类,继承自 SQLiteOpenHelper
,用于管理数据库操作。在 onCreate()
方法中,我们创建了一个表,定义了两个列。在插入数据、更新数据、删除数据和获取所有数据的方法中,我们使用 SQLiteDatabase
对象来执行相应的数据库操作。
在实际使用中,我们可以通过创建 DatabaseHelper
对象来进行数据库管理操作,例如:
DatabaseHelper dbHelper = new DatabaseHelper(context);
dbHelper.insertData("Alice");
dbHelper.insertData("Bob");
dbHelper.updateData(1, "David");
dbHelper.deleteData(2);
List<String> dataList = dbHelper.getAllData();
通过这个数据库管理操作类,我们可以方便地对数据库进行增删改查操作,实现数据的持久化存储。
在Android平台上进行数据库管理操作,通常会使用SQLite数据库。下面是一个示例的数据库管理操作类:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseManager {
private static final String DATABASE_NAME = "my_database";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "my_table";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_AGE = "age";
private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_AGE + " INTEGER);";
private Context context;
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
public DatabaseManager(Context context) {
this.context = context;
}
public DatabaseManager open() throws SQLException {
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
//添加数据
public long insert(String name, int age) {
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_AGE, age);
return database.insert(TABLE_NAME, null, values);
}
//查询数据
public Cursor query() {
return database.query(TABLE_NAME, new String[]{COLUMN_ID, COLUMN_NAME, COLUMN_AGE},
null, null, null, null, null);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
使用这个数据库管理操作类,可以进行数据库的创建、打开、插入数据和查询数据等操作。记得在使用完毨,调用 close() 方法关闭数据库连接。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/150609.html