Android studio中的SQL数据库的增删查改
时间: 2024-05-13 11:15:12 浏览: 93
android studio 数据库的增删查改
4星 · 用户满意度95%
在 Android Studio 中,可以使用 SQLite 数据库进行数据存储和管理。下面是使用 SQL 数据库进行增删查改的示例代码:
## 建表
```java
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "my_database";
private static final int DB_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";
public MyDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableSql = "CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_NAME + " TEXT,"
+ COLUMN_AGE + " INTEGER)";
db.execSQL(createTableSql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTableSql = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(dropTableSql);
onCreate(db);
}
}
```
## 插入数据
```java
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MyDatabaseHelper.COLUMN_NAME, "张三");
values.put(MyDatabaseHelper.COLUMN_AGE, 18);
db.insert(MyDatabaseHelper.TABLE_NAME, null, values);
db.close();
}
}
```
## 查询数据
```java
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(MyDatabaseHelper.TABLE_NAME, null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(MyDatabaseHelper.COLUMN_ID));
String name = cursor.getString(cursor.getColumnIndex(MyDatabaseHelper.COLUMN_NAME));
int age = cursor.getInt(cursor.getColumnIndex(MyDatabaseHelper.COLUMN_AGE));
Log.d("MainActivity", "id=" + id + ", name=" + name + ", age=" + age);
}
cursor.close();
db.close();
}
}
```
## 更新数据
```java
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MyDatabaseHelper.COLUMN_AGE, 20);
db.update(MyDatabaseHelper.TABLE_NAME, values, MyDatabaseHelper.COLUMN_NAME + "=?", new String[]{"张三"});
db.close();
}
}
```
## 删除数据
```java
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(MyDatabaseHelper.TABLE_NAME, MyDatabaseHelper.COLUMN_NAME + "=?", new String[]{"张三"});
db.close();
}
}
```
阅读全文