建立一个android工程,修改main.xml文件并编写程序代码,用SQLite编写一个学生信息管理应用,并实现插入、删除、更新、查找等方法
时间: 2024-03-23 08:43:23 浏览: 42
好的,下面是一个简单的示例代码,仅供参考:
1. 在主Activity中创建SQLiteOpenHelper的子类,例如:
```
public class StudentDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "student.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "student";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_AGE = "age";
public StudentDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_NAME + " TEXT,"
+ COLUMN_AGE + " INTEGER" + ")";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public long insertStudent(String name, int age) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_AGE, age);
long id = db.insert(TABLE_NAME, null, values);
db.close();
return id;
}
public int updateStudent(long id, String name, int age) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_AGE, age);
int rowsAffected = db.update(TABLE_NAME, values, COLUMN_ID + " = ?",
new String[] { String.valueOf(id) });
db.close();
return rowsAffected;
}
public int deleteStudent(long id) {
SQLiteDatabase db = this.getWritableDatabase();
int rowsAffected = db.delete(TABLE_NAME, COLUMN_ID + " = ?",
new String[] { String.valueOf(id) });
db.close();
return rowsAffected;
}
public Cursor getAllStudents() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
return cursor;
}
public Cursor searchStudentByName(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, COLUMN_NAME + " LIKE ?",
new String[] { "%" + name + "%" }, null, null, null);
return cursor;
}
}
```
2. 在主Activity中定义UI界面的组件,例如:
```
public class MainActivity extends AppCompatActivity {
private EditText editTextName;
private EditText editTextAge;
private Button buttonAdd;
private Button buttonUpdate;
private Button buttonDelete;
private Button buttonSearch;
private ListView listView;
private SimpleCursorAdapter adapter;
private StudentDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new StudentDatabaseHelper(this);
editTextName = findViewById(R.id.editTextName);
editTextAge = findViewById(R.id.editTextAge);
buttonAdd = findViewById(R.id.buttonAdd);
buttonUpdate = findViewById(R.id.buttonUpdate);
buttonDelete = findViewById(R.id.buttonDelete);
buttonSearch = findViewById(R.id.buttonSearch);
listView = findViewById(R.id.listView);
String[] fromColumns = { StudentDatabaseHelper.COLUMN_NAME, StudentDatabaseHelper.COLUMN_AGE };
int[] toViews = { android.R.id.text1, android.R.id.text2 };
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, null, fromColumns, toViews, 0);
listView.setAdapter(adapter);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString().trim();
int age = Integer.parseInt(editTextAge.getText().toString().trim());
long id = dbHelper.insertStudent(name, age);
if (id > 0) {
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
refreshListView();
} else {
Toast.makeText(MainActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
}
}
});
buttonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long id = listView.getSelectedItemId();
if (id == AdapterView.INVALID_ROW_ID) {
Toast.makeText(MainActivity.this, "请选中一条记录", Toast.LENGTH_SHORT).show();
return;
}
String name = editTextName.getText().toString().trim();
int age = Integer.parseInt(editTextAge.getText().toString().trim());
int rowsAffected = dbHelper.updateStudent(id, name, age);
if (rowsAffected > 0) {
Toast.makeText(MainActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
refreshListView();
} else {
Toast.makeText(MainActivity.this, "更新失败", Toast.LENGTH_SHORT).show();
}
}
});
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long id = listView.getSelectedItemId();
if (id == AdapterView.INVALID_ROW_ID) {
Toast.makeText(MainActivity.this, "请选中一条记录", Toast.LENGTH_SHORT).show();
return;
}
int rowsAffected = dbHelper.deleteStudent(id);
if (rowsAffected > 0) {
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
refreshListView();
} else {
Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
}
}
});
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString().trim();
Cursor cursor = dbHelper.searchStudentByName(name);
adapter.changeCursor(cursor);
}
});
refreshListView();
}
private void refreshListView() {
Cursor cursor = dbHelper.getAllStudents();
adapter.changeCursor(cursor);
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
}
```
3. 在布局文件(例如activity_main.xml)中定义UI界面的组件,例如:
```
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="年龄" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加" />
<Button
android:id="@+id/buttonUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新" />
<Button
android:id="@+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除" />
<Button
android:id="@+id/buttonSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查找" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
```
需要注意的是,由于SQLite数据库的操作需要在后台线程中进行,因此可以使用AsyncTask等异步任务框架来实现。此外,为了提高查询效率,可以在数据库中创建索引。
阅读全文