SQLite Android 数据库主从关系实例
在本文中,我们将介绍如何在Android应用中使用SQLite数据库实现主从关系的功能。我们会从数据库的创建开始,使用SQL语句查询和插入数据,并将数据显示在主从结构的布局中。
阅读更多:SQLite 教程
SQLite 数据库介绍
SQLite是一种轻量级的嵌入式数据库引擎,它在移动应用开发中广泛使用。它是Android系统自带的数据库引擎,并提供了许多有用的API来操作数据库。
创建数据库
在Android应用中,我们可以使用SQLiteOpenHelper类来创建和管理SQLite数据库。首先,我们需要创建一个继承自SQLiteOpenHelper的子类,并实现onCreate和onUpgrade方法。onCreate方法在数据库第一次被创建时调用,通常用于创建数据库表和初始化数据。onUpgrade方法在数据库升级时调用,通常用于更新数据库结构和数据迁移。
示例代码如下:
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableQuery = "CREATE TABLE IF NOT EXISTS employees (name TEXT, department TEXT)";
db.execSQL(createTableQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTableQuery = "DROP TABLE IF EXISTS employees";
db.execSQL(dropTableQuery);
onCreate(db);
}
}
查询和插入数据
一旦数据库被创建,我们可以使用SQLiteDatabase类的方法来执行SQL语句并操作数据库。查询数据可以使用query方法,插入数据可以使用insert方法。
示例代码如下:
DBHelper dbHelper = new DBHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
// 查询数据
Cursor cursor = db.query("employees", new String[]{"name", "department"}, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex("name"));
String department = cursor.getString(cursor.getColumnIndex("department"));
Log.i("Employee", "Name: " + name + ", Department: " + department);
} while (cursor.moveToNext());
}
cursor.close();
// 插入数据
ContentValues values = new ContentValues();
values.put("name", "John");
values.put("department", "Sales");
long rowId = db.insert("employees", null, values);
实现主从布局
现在我们已经了解了如何创建和操作SQLite数据库,接下来我们将讲述如何将数据显示在支持主从关系的布局中。在Android中,常见的主从布局有ListView和RecyclerView。
使用ListView展示数据
ListView是一个常用的列表控件,可以显示数据库中的多项数据。我们可以创建一个适配器类继承自BaseAdapter,并实现getView方法来设置每一项的布局和数据。
示例代码如下:
public class EmployeeAdapter extends BaseAdapter {
private Context context;
private List<Employee> employeeList;
public EmployeeAdapter(Context context, List<Employee> employeeList) {
this.context = context;
this.employeeList = employeeList;
}
@Override
public int getCount() {
return employeeList.size();
}
@Override
public Object getItem(int position) {
return employeeList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_employee, parent, false);
}
TextView nameTextView = convertView.findViewById(R.id.nameTextView);
TextView departmentTextView = convertView.findViewById(R.id.departmentTextView);
Employee employee = employeeList.get(position);
nameTextView.setText(employee.getName());
departmentTextView.setText(employee.getDepartment());
return convertView;
}
}
然后我们可以将适配器和ListView关联起来,并在Activity中显示数据。
示例代码如下:
List<Employee> employeeList = new ArrayList<>();
// 查询数据
Cursor cursor = db.query("employees", new String[]{"name", "department"}, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex("name"));
String department = cursor.getString(cursor.getColumnIndex("department"));
employeeList.add(new Employee(name, department));
} while (cursor.moveToNext());
}
cursor.close();
// ListView和适配器关联
ListView listView = findViewById(R.id.listView);
EmployeeAdapter adapter = new EmployeeAdapter(this, employeeList);
listView.setAdapter(adapter);
使用RecyclerView展示数据
RecyclerView是一个更为灵活和高效的列表控件,可以用来显示大量的数据和实现复杂的布局。我们需要创建一个适配器类继承自RecyclerView.Adapter和一个ViewHolder类来保存每一项的布局和数据。
示例代码如下:
public class EmployeeAdapter extends RecyclerView.Adapter<EmployeeAdapter.ViewHolder> {
private Context context;
private List<Employee> employeeList;
public EmployeeAdapter(Context context, List<Employee> employeeList) {
this.context = context;
this.employeeList = employeeList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_employee, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Employee employee = employeeList.get(position);
holder.nameTextView.setText(employee.getName());
holder.departmentTextView.setText(employee.getDepartment());
}
@Override
public int getItemCount() {
return employeeList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView nameTextView;
TextView departmentTextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.nameTextView);
departmentTextView = itemView.findViewById(R.id.departmentTextView);
}
}
}
然后我们可以将适配器和RecyclerView关联起来,并在Activity中显示数据。
示例代码如下:
List<Employee> employeeList = new ArrayList<>();
// 查询数据
Cursor cursor = db.query("employees", new String[]{"name", "department"}, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex("name"));
String department = cursor.getString(cursor.getColumnIndex("department"));
employeeList.add(new Employee(name, department));
} while (cursor.moveToNext());
}
cursor.close();
// RecyclerView和适配器关联
RecyclerView recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
EmployeeAdapter adapter = new EmployeeAdapter(this, employeeList);
recyclerView.setAdapter(adapter);
总结
本文介绍了如何在SQLite Android应用中实现主从关系的数据库操作。我们首先创建了一个数据库并使用SQLiteDatabase进行数据查询和插入。然后,我们使用ListView和RecyclerView展示数据库中的数据,并通过自定义适配器和布局实现了主从关系的界面设计。以上就是SQLite Android主从关系的数据库实例的全部内容。希望本文能够帮助您在Android应用开发中更好地使用SQLite数据库。
极客笔记