一、前言:数据库学的真的很让人烦躁,一边看视频一边写代码,还有好多代码是什么意思我都不知到。这篇文章是对Android自带的数据库的操作,有数据的增加,删除,修改,查询的操作。
二、废话不多说上代码
首先界面布局xml
对应的Activity:SQLiteHelperActivity.java
public class SQLiteHelperActivity extends AppCompatActivity implements View.OnClickListener {
private EditText 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.btn_save).setOnClickListener(this);
findViewById(R.id.btn_delete).setOnClickListener(this);
findViewById(R.id.btn_update).setOnClickListener(this);
findViewById(R.id.btn_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 view) {
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 (view.getId()) {
case R.id.btn_save:
//以下声明一个用户信息对象,并填写他的各个字段值
user = new User(name,
Integer.parseInt(age),
Long.parseLong(height),
Float.parseFloat(weight),
ck_married.isChecked());
if (mHelper.insert(user)>0){
ToastUtil.show(this,"添加成功");
}
mHelper.insert(user);
break;
case R.id.btn_delete:
if (mHelper.deleteByName(name)>0) {
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_update:
user = new User(name,
Integer.parseInt(age),
Long.parseLong(height),
Long.parseLong(weight),
ck_married.isChecked());
if (mHelper.update(user)>0){
Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_query:
List list = mHelper.queryAll();
for (User u:list){
Log.d("ming", u.toString());
}
break;
}
}
}
新建一个包entity---------在这个包下面新建一个类User
public class User {
public int id;//序号
public String name;//姓名
public int age;
public Long height;//身高
public float weight;//体重
public boolean married;//婚否
public User(){
}
public User(String name,int age, Long height, float weight, boolean married) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
this.married = married;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", height=" + height +
", weight=" + weight +
", married=" + married +
'}';
}
}
新建一个包base----------在这个包下面新建一个类UserDBHelper.java
public class UserDBHelper extends SQLiteOpenHelper {
/**
* 当我们继承SQLiteOpenHelper时会报错我们实现完其中的两个方法的时候还是会报错这是什么原因
* 需要调用父类的构造方法(在子类中去调用父类的构造方法)
* @param sqLiteDatabase
*/
private static final String DB_NAME = "user.db";
private static final String TABLE_NAME = "user_info";
private static final int DB_VERSION = 1;
private static UserDBHelper mHelper;
private SQLiteDatabase mRDB = null;
private SQLiteDatabase mWDB = null;
private UserDBHelper(Context context){
super(context,DB_NAME,null,DB_VERSION);
}
//利用单例模式获取数据库帮助器的唯一实例
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;
}
if (mWDB != null && mWDB.isOpen()){
mWDB.close();
mWDB = null;
}
}
//创建数据库执行建表语句
@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 sqLiteDatabase, int i, int i1) {
}
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);
}
public long deleteByName(String name){
//删除所有
// mWDB.delete(TABLE_NAME,"1=1",null);
return mWDB.delete(TABLE_NAME,"name=?",new String[]{name});
}
public long update(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.update(TABLE_NAME,values,"name=?",new String[]{user.name});//更新影响了多少行
}
public List queryAll(){
List list = new ArrayList<>();
//执行记录查询动作,该语句返回结果集的游标
Cursor cursor = mRDB.query(TABLE_NAME,null,null,null,null,null,null);
//循环取出游标指向的每一条记录
User user = new User();
while (cursor.moveToNext()){
user.id = cursor.getInt(0);
user.name = cursor.getString(1);
user.age = cursor.getInt(2);
user.height = cursor.getLong(3);
user.weight = cursor.getFloat(4);
//SQLite没有布尔类型,用0表示false,用1表示true
user.married = (cursor.getInt(5) == 0) ? false :true;
}
return list;
}
}
三、总结:学习数据库真的很枯燥,他没有之前android控件学的让人开心。写完这篇文章肯定不意味着结束学习,它大概意味着新的领域新的挑战。希望我写的这些东西能帮到更多人。