开发一个安卓app时,如果需要使用数据存储功能,常用的方式就是使用数据库。Android 系统提供了 SQLite 数据库,这是一款轻型的嵌入式关系型数据库,非常适合在手机等移动设备上使用。
下面是开发一个使用数据库的安卓app的大致步骤:
1. 引入 SQLite 组件
首先需要在项目的 build.gradle 文件中添加以下依赖:
```
dependencies {
implementation 'androidx.sqlite:sqlite:2.1.0'
implementation 'androidx.sqlite:sqlite-framework:2.1.0'
implementation 'androidx.room:room-runtime:2.2.5'
kapt 'androidx.room:room-compiler:2.2.5'
}
```
2. 创建数据库
创建一个 DatabaseHelper 类继承自 SQLiteOpenHelper,并实现以下两个方法:
```
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MyDatabase";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS `Person` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT NOT NULL, `age` INTEGER NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS `Person`");
onCreate(sqLiteDatabase);
}
}
```
在 onCreate 方法中,可以创建需要的表格。
3. 进行 CRUD(增删改查)操作
在项目中需要操作数据时,可以创建一个 DataHelper 类,封装增删改查等操作。
```
public class DataHelper {
private SQLiteDatabase mSQLiteDatabase;
public DataHelper(Context context) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
mSQLiteDatabase = dbHelper.getWritableDatabase();
}
public void insert(String name, int age) {
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("age", age);
mSQLiteDatabase.insert("Person", null, cv);
}
public void delete(int id) {
mSQLiteDatabase.delete("Person", "id=?", new String[]{String.valueOf(id)});
}
public void update(int id, String name, int age) {
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("age", age);
mSQLiteDatabase.update("Person", cv, "id=?", new String[]{String.valueOf(id)});
}
public Cursor queryAll() {
return mSQLiteDatabase.query("Person", null, null, null, null, null, null);
}
}
```
其中,insert、delete、update、queryAll 等方法分别对应着数据库操作的增、删、改、查。
4. 使用数据
最后,在页面需要使用数据时,可以在相应的方法中调用 DataHelper 中定义好的方法。
```
public class MainActivity extends AppCompatActivity {
private DataHelper mDataHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDataHelper = new DataHelper(this);
Button insertBtn = findViewById(R.id.insert_btn);
insertBtn.setOnClickListener(view -> mDataHelper.insert("张三", 18));
Button deleteBtn = findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(view -> mDataHelper.delete(1));
Button updateBtn = findViewById(R.id.update_btn);
updateBtn.setOnClickListener(view -> mDataHelper.update(2, "李四", 20));
Button queryBtn = findViewById(R.id.query_btn);
queryBtn.setOnClickListener(view -> {
Cursor cursor = mDataHelper.queryAll();
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.d("MainActivity", "name: " + name);
Log.d("MainActivity", "age: " + age);
} while (cursor.moveToNext());
}
cursor.close();
});
}
}
```
以上就是开发一个使用数据库的安卓app的大致步骤。需要注意的是,在使用完数据库之后,需要调用相应的 close() 方法关闭数据库,以免造成资源浪费。