在StudentDao类文件中代码如下:
public class StudentDao {
Context context;
final String tableName = "student";
public StudentDao(Context mContext) {
context = mContext;
}
// 添加
public void addStudent(Student student) {
DbUtil mDbUtil = new DbUtil(context);
SQLiteDatabase mSQLiteDatabase = mDbUtil.getWritableDatabase();
// 执行insert语句的方式
ContentValues values = new ContentValues();
values.put("id", student.getId());
values.put("age", student.getAge());
values.put("name", student.getName());
mSQLiteDatabase.insert(tableName, null, values);
// 关闭
mSQLiteDatabase.close();
mDbUtil.close();
}
}
在DbUtil类文件中代码如下:
public class DbUtil extends SQLiteOpenHelper {
//创建库文件
public DbUtil(Context context) {
super(context, "test.db", null, 2);
}
//创建表
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table student(id int,name varchar(20))");
db.execSQL("create table teacher(id int,name varchar(20))");
Log.e("DbUtil", "onCreate");
}
}
问题1:在StudentDao类中,StudentDao构造函数有什么作用(可以说详细一点吗)?
问题2:在StudentDao类中,这一句代码DbUtil mDbUtil = new DbUtil(context)这一句代码是创建一个数据库吗?