public class SQLiteHelperActivity extends AppCompatActivity implements View.OnClickListener {
private TextView et_name;
private EditText et_age;
private EditText et_height;
private EditText et_weight;
private CheckBox ck_married;
private UserDBHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite_helper);
et_name = findViewById(R.id.et_name);
et_age = findViewById(R.id.et_age);
et_height = findViewById(R.id.et_height);
et_weight = findViewById(R.id.et_weight);
ck_married = findViewById(R.id.ck_married);
findViewById(R.id.bth_save).setOnClickListener(this);
findViewById(R.id.bth_delete).setOnClickListener(this);
findViewById(R.id.bth_updata).setOnClickListener(this);
findViewById(R.id.bth_query).setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
//获得数据库帮助器的实例
mHelper = UserDBHelper.getInstance(this);
//打开数据库帮助器的写连接
mHelper.openWriteLink();
//打开数据库的读连接
mHelper.openReadLink();
}
@Override
protected void onStop() {
super.onStop();
//关闭数据库连接
mHelper.closeLink();
}
@Override
public void onClick(View v) {
String name=et_name.getText().toString();
String age=et_age.getText().toString();
String height=et_height.getText().toString();
String weight=et_weight.getText().toString();
User user=null;
switch (v.getId()){
case R.id.bth_save:
//以下声明一个用户信息对象,并填写它的各字段值
user=new User(name,
Integer.parseInt(age),
Long.parseLong(height),
Long.parseLong(weight),
ck_married.isChecked());
mHelper.insert(user);
if(mHelper.insert(user)>0){
ToastUtil.show(this,"添加成功");
}
break;
}
}
}
public class UserDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME="user.db";
private static final String TABLE_NAME="user_info";
private static final int DB_VERISON=1;
private SQLiteDatabase mRDB=null;
private SQLiteDatabase mWDB=null;
private static UserDBHelper mHelper=null;
private UserDBHelper(Context context){
super(context,DB_NAME,null,DB_VERISON);
}
//利用单例模式获取数据库帮助器的唯一实例
public static UserDBHelper getInstance(Context context){
if(mHelper==null){
mHelper=new UserDBHelper(context);
}
return mHelper;
}
//打开数据库的读连接
public SQLiteDatabase openReadLink(){
if(mRDB==null || !mRDB.isOpen()){
mRDB=mHelper.getReadableDatabase();
}
return mRDB;
}
//打开数据库的写连接
public SQLiteDatabase openWriteLink(){
if(mWDB==null || !mWDB.isOpen()){
mWDB=mHelper.getWritableDatabase();
}
return mWDB;
}
//关闭数据的连接
public void closeLink(){
if(mRDB !=null && mRDB.isOpen()){
mRDB.close();
mRDB=null;
}else if(mWDB !=null && mWDB.isOpen()){
mWDB.close();
}
}
//创建数据库,执行建表语句
@Override
public void onCreate(SQLiteDatabase db) {
String sql="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"("+
"_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+
"name VARCHAR NOT NULL, "+
"age INTEGER NOT NULL, "+
"height LONG NOT NULL, "+
"weight Float NOT NULL, "+
"married INTEGER NOT NULL);";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insert(User user){
ContentValues values=new ContentValues();
values.put("name",user.name);
values.put("age",user.age);
values.put("height",user.height);
values.put("weight",user.weight);
values.put("married",user.married);
return mWDB.insert(TABLE_NAME,null,values);
}
}