在Android中,可以使用SQLite数据库来创建和管理本地数据库。下面使用Android自带的数据库API创建表,并进行增删改查操作:
1. **创建数据库帮助类**:
首先,创建一个继承自`SQLiteOpenHelper`的类,用于管理数据库的创建和版本控制。
```java
// 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 = "my_database";
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 my_table ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "name TEXT, "
+ "age INTEGER)";
db.execSQL(createTableQuery);
}
// 更新数据库
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 当数据库版本更新时执行的操作
// 一般情况下是备份数据,删除旧表,创建新表,然后将备份数据重新插入
}
}
```
2. **进行增删改查操作**:
在您的活动或其他Java类中,实例化`DbHelper`类并使用`SQLiteDatabase`执行增删改查操作。
```java
// MainActivity.java
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private DbHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DbHelper(this);
// 插入数据
insertData("John", 25);
// 查询数据
readData();
// 更新数据
updateData(1, "Jack");
// 删除数据
deleteData(1);
}
private void insertData(String name, int age) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
long newRowId = db.insert("my_table", null, values);
Log.d("MainActivity", "New row inserted with ID: " + newRowId);
}
private void readData() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] projection = {"id", "name", "age"};
Cursor cursor = db.query("my_table", projection, 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"));
Log.d("MainActivity", "ID: " + id + ", Name: " + name + ", Age: " + age);
}
cursor.close();
}
private void updateData(int id, String newName) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", newName);
String selection = "id = ?";
String[] selectionArgs = {String.valueOf(id)};
int count = db.update("my_table", values, selection, selectionArgs);
Log.d("MainActivity", count + " rows updated");
}
private void deleteData(int id) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
String selection = "id = ?";
String[] selectionArgs = {String.valueOf(id)};
int count = db.delete("my_table", selection, selectionArgs);
Log.d("MainActivity", count + " rows deleted");
}
}
```