andriod studio数据库增删查改的完整代码
时间: 2024-01-10 10:04:23 浏览: 129
androidstudio数据库增删改
这是一个简单的 Android Studio 数据库增删查改的示例代码,使用的是 SQLite 数据库:
1. 首先,在 `build.gradle` 文件中添加以下依赖项:
```
dependencies {
// ...
implementation 'androidx.sqlite:sqlite:2.1.0'
}
```
2. 然后,在你的 `MainActivity` 类中定义以下变量:
```java
private SQLiteDatabase mDatabase;
private EditText mNameEditText, mAgeEditText, mIdEditText;
private Button mAddButton, mUpdateButton, mDeleteButton, mRetrieveButton;
```
3. 接着,在 `onCreate` 方法中初始化这些变量:
```java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化数据库
mDatabase = openOrCreateDatabase("mydatabase.db", MODE_PRIVATE, null);
mDatabase.execSQL("CREATE TABLE IF NOT EXISTS persons (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");
// 初始化 UI 组件
mNameEditText = findViewById(R.id.name_edit_text);
mAgeEditText = findViewById(R.id.age_edit_text);
mIdEditText = findViewById(R.id.id_edit_text);
mAddButton = findViewById(R.id.add_button);
mUpdateButton = findViewById(R.id.update_button);
mDeleteButton = findViewById(R.id.delete_button);
mRetrieveButton = findViewById(R.id.retrieve_button);
// 设置按钮点击事件
mAddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addPerson();
}
});
mUpdateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updatePerson();
}
});
mDeleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deletePerson();
}
});
mRetrieveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
retrievePerson();
}
});
}
```
4. 然后,实现以下四个方法,分别对应增删查改:
```java
private void addPerson() {
String name = mNameEditText.getText().toString();
int age = Integer.parseInt(mAgeEditText.getText().toString());
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
mDatabase.insert("persons", null, values);
Toast.makeText(this, "添加成功!", Toast.LENGTH_SHORT).show();
}
private void updatePerson() {
int id = Integer.parseInt(mIdEditText.getText().toString());
String name = mNameEditText.getText().toString();
int age = Integer.parseInt(mAgeEditText.getText().toString());
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
mDatabase.update("persons", values, "id=?", new String[]{String.valueOf(id)});
Toast.makeText(this, "更新成功!", Toast.LENGTH_SHORT).show();
}
private void deletePerson() {
int id = Integer.parseInt(mIdEditText.getText().toString());
mDatabase.delete("persons", "id=?", new String[]{String.valueOf(id)});
Toast.makeText(this, "删除成功!", Toast.LENGTH_SHORT).show();
}
private void retrievePerson() {
Cursor cursor = mDatabase.query("persons", null, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
buffer.append("id: ").append(id).append(", name: ").append(name).append(", age: ").append(age).append("\n");
}
cursor.close();
Toast.makeText(this, buffer.toString(), Toast.LENGTH_LONG).show();
}
```
5. 最后,在布局文件中添加以下组件:
```xml
<EditText
android:id="@+id/name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"/>
<EditText
android:id="@+id/age_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="年龄"/>
<EditText
android:id="@+id/id_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ID"/>
<Button
android:id="@+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加"/>
<Button
android:id="@+id/update_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更新"/>
<Button
android:id="@+id/delete_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"/>
<Button
android:id="@+id/retrieve_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查找"/>
```
这样,你就可以在 Android Studio 中使用 SQLite 数据库进行增删查改了。
阅读全文