使用代码创建SQLite数据库,并实对现数据库的增删查改。要求所创建的数据库表中所包含的属性为:编号(id)、姓名(name)、性别(sex)、年龄(age)、身高(height),完成对数据库中内容的增删查改工作。
时间: 2024-01-19 09:03:17 浏览: 96
好的,下面是使用代码创建SQLite数据库,并实现对现有数据库的增删查改的示例代码。
首先,创建一个名为DatabaseHelper的类,继承自SQLiteOpenHelper,用于创建和管理数据库。代码如下:
```java
public class DatabaseHelper extends SQLiteOpenHelper {
// 数据库名称
private static final String DATABASE_NAME = "mydatabase.db";
// 数据库版本号
private static final int DATABASE_VERSION = 1;
// 表名
private static final String TABLE_NAME = "person";
// 列名
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_SEX = "sex";
private static final String COLUMN_AGE = "age";
private static final String COLUMN_HEIGHT = "height";
// 创建表的SQL语句
private static final String CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_NAME + " TEXT,"
+ COLUMN_SEX + " TEXT,"
+ COLUMN_AGE + " INTEGER,"
+ COLUMN_HEIGHT + " INTEGER)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建表
db.execSQL(CREATE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 删除旧表
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// 创建新表
onCreate(db);
}
}
```
上面的代码中,我们定义了数据库名称、版本号、表名和列名等常量,以及创建表的SQL语句。在DatabaseHelper类中,我们实现了SQLiteOpenHelper的两个抽象方法onCreate()和onUpgrade(),用于创建和升级数据库。在onCreate()方法中,我们执行了创建表的SQL语句,从而创建了一个名为person的表,该表包含编号、姓名、性别、年龄和身高等属性。
接下来,我们可以在Activity中使用DatabaseHelper类来实现对数据库的增删查改操作。以下是一个简单的示例代码:
```java
public class MainActivity extends AppCompatActivity {
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建DatabaseHelper对象
dbHelper = new DatabaseHelper(this);
// 添加数据
addData("Tom", "Male", 20, 175);
addData("Lucy", "Female", 18, 160);
// 查询数据
List<Person> persons = queryData();
for (Person person : persons) {
Log.d("MainActivity", "id: " + person.getId() + ", name: " + person.getName()
+ ", sex: " + person.getSex() + ", age: " + person.getAge()
+ ", height: " + person.getHeight());
}
// 修改数据
updateData(1, "Jerry", "Male", 22, 180);
// 查询数据
persons = queryData();
for (Person person : persons) {
Log.d("MainActivity", "id: " + person.getId() + ", name: " + person.getName()
+ ", sex: " + person.getSex() + ", age: " + person.getAge()
+ ", height: " + person.getHeight());
}
// 删除数据
deleteData(2);
// 查询数据
persons = queryData();
for (Person person : persons) {
Log.d("MainActivity", "id: " + person.getId() + ", name: " + person.getName()
+ ", sex: " + person.getSex() + ", age: " + person.getAge()
+ ", height: " + person.getHeight());
}
}
// 添加数据
private void addData(String name, String sex, int age, int height) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabaseHelper.COLUMN_NAME, name);
values.put(DatabaseHelper.COLUMN_SEX, sex);
values.put(DatabaseHelper.COLUMN_AGE, age);
values.put(DatabaseHelper.COLUMN_HEIGHT, height);
db.insert(DatabaseHelper.TABLE_NAME, null, values);
}
// 查询数据
private List<Person> queryData() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(DatabaseHelper.TABLE_NAME, null, null, null, null, null, null);
List<Person> persons = new ArrayList<>();
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_ID));
String name = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NAME));
String sex = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_SEX));
int age = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_AGE));
int height = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_HEIGHT));
Person person = new Person(id, name, sex, age, height);
persons.add(person);
}
cursor.close();
return persons;
}
// 修改数据
private void updateData(int id, String name, String sex, int age, int height) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabaseHelper.COLUMN_NAME, name);
values.put(DatabaseHelper.COLUMN_SEX, sex);
values.put(DatabaseHelper.COLUMN_AGE, age);
values.put(DatabaseHelper.COLUMN_HEIGHT, height);
db.update(DatabaseHelper.TABLE_NAME, values, DatabaseHelper.COLUMN_ID + "=?", new String[]{String.valueOf(id)});
}
// 删除数据
private void deleteData(int id) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(DatabaseHelper.TABLE_NAME, DatabaseHelper.COLUMN_ID + "=?", new String[]{String.valueOf(id)});
}
@Override
protected void onDestroy() {
super.onDestroy();
// 关闭数据库
dbHelper.close();
}
}
```
上面的代码中,我们首先在onCreate()方法中创建了DatabaseHelper对象,并调用addData()方法向数据库中添加了两条数据。接着,我们调用queryData()方法查询数据库中的数据,并通过Log输出到控制台中。然后,我们调用updateData()方法修改了id为1的数据,并再次调用queryData()方法查询数据。最后,我们调用deleteData()方法删除了id为2的数据,并再次调用queryData()方法查询数据。
注意,在Activity中使用完数据库之后,需要调用DatabaseHelper的close()方法来关闭数据库连接,以释放资源。
阅读全文