今天把之前寫的讀取數據的頁面改裝一下
把讀取到的數據丟進database
首先 先寫一個能夠調用資料庫的class
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_VERSION = 1;
private static UserDBHelper mHelper = null;
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 sqLiteDatabase) {
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);";
sqLiteDatabase.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);
}
}
再寫一個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 +
'}';
}
}
主程式的部分
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 cb_marry;
private UserDBHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_sqlite_helper);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
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);
cb_marry = findViewById(R.id.cb_marry);
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 weight = et_weight.getText().toString();
String height = et_height.getText().toString();
User user = null;
if(view.getId()==R.id.btn_save){
user = new User(name,Integer.parseInt(age),Long.parseLong(height),Long.parseLong(weight),cb_marry.isChecked());
if(mHelper.insert(user)>0){
ToastUtil.show(this,"添加成功");
mHelper.insert(user);
}
}
}
正式步入資料庫的部分 越來越難了:(