在Android开发中,我们经常需要进行数据库操作来存储和管理数据。以下是一个简单的Android数据库操作类示例:
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 DatabaseHelper {
private static final String DATABASE_NAME = "mydatabase";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "mytable";
private static final String KEY_ID = "_id";
private static final String KEY_NAME = "name";
private static final String KEY_AGE = "age";
private static final String DATABASE_CREATE = "create table " + TABLE_NAME +
" (" + KEY_ID + " integer primary key autoincrement, " +
KEY_NAME + " text not null, " +
KEY_AGE + " integer);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DatabaseHelper(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public DatabaseHelper open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public long insertData(String name, int age){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_AGE, age);
return db.insert(TABLE_NAME, null, initialValues);
}
public boolean deleteData(long rowId){
return db.delete(TABLE_NAME, KEY_ID + "=" + rowId, null) > 0;
}
public Cursor getAllData(){
return db.query(TABLE_NAME, new String[] {KEY_ID, KEY_NAME, KEY_AGE},
null, null, null, null, null);
}
public Cursor getData(long rowId) throws SQLException {
Cursor mCursor = db.query(true, TABLE_NAME, new String[] {KEY_ID, KEY_NAME, KEY_AGE},
KEY_ID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateData(long rowId, String name, int age){
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_AGE, age);
return db.update(TABLE_NAME, args, KEY_ID + "=" + rowId, null) > 0;
}
}
这个数据库操作类包括了创建数据库、插入数据、删除数据、查询数据和更新数据等基本操作。您可以根据需要扩展和定制这个类以满足您的业务需求。希望对您有所帮助!
在Android开发中,我们经常需要对数据库进行操作来存储和查询数据。一个常用的方法是使用SQLite数据库来进行数据存储操作。下面是一个简单的Android数据库操作类示例,用于进行数据库的创建、插入、查询、更新和删除操作:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper 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";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY,"
+ COLUMN_NAME + " TEXT"
+ ")";
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 Cursor getAllData() {
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
return cursor;
}
public void updateData(int id, String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
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();
}
}
这个类中包含了数据库的创建、插入、查询、更新和删除操作的方法。在进行操作之前,需要先实例化这个类,并且调用相应的方法来进行操作。例如:
DBHelper dbHelper = new DBHelper(context);
dbHelper.insertData("John");
Cursor cursor = dbHelper.getAllData();
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex("name"));
// Do something with the data
} while (cursor.moveToNext());
}
dbHelper.updateData(1, "Peter");
dbHelper.deleteData(2);
这是一个简单的示例,你可以根据自己的需求来扩展和修改这个类,实现更复杂的数据库操作。希望对你有所帮助。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/149517.html