SQLiteDataBase的增刪改查的兩種方式:
阿新 • • 發佈:2018-12-31
在android中內建了一個輕量級的資料庫SQLite,今天主要講講資料庫的增刪改查的兩種方式。
方式一:
前提了解:在SQL語句中資料庫增刪改查的語句:
增:"insert into info(name,phone) values(?;?)",new Object[]{infobean.name,infobean.phone} 刪:"delete from info where name =?;",new Object[]{name} 改:"update info set phone =? where name =?;",new Object[]{infobean.phone,infobean.name} 查:"select _id,name,phone from info where name =?;",new String[]{name}
建立四個類:
Infobean(提供相應的引數)
MainActivity(執行主執行緒)
MySQLiteDataBase extends SQLiteOpenHelper(建立資料庫)
InfoDao(封裝方法)
程式碼如下:
public class Infobean {//定義基本引數
public String name;
public String phone;
}
public class InfoDao {//方法封裝 private MySQLiteDataBase mySQLiteDataBase; public InfoDao(Context context){ mySQLiteDataBase = new MySQLiteDataBase(context); } public void add(Infobean infobean) { SQLiteDatabase db = mySQLiteDataBase.getWritableDatabase(); db.execSQL("insert into info(name,phone) values(?;?)",new Object[]{infobean.name,infobean.phone}); db.close(); } public void del(String name) { SQLiteDatabase db = mySQLiteDataBase.getReadableDatabase(); db.execSQL("delete from info where name =?;",new Object[]{name}); db.close(); } public void update(Infobean infobean) { SQLiteDatabase db = mySQLiteDataBase.getReadableDatabase(); db.execSQL("update info set phone =? where name =?;",new Object[]{infobean.phone,infobean.name}); // db.execSQL("update info set phone=? where name=?;", new Object[]{infobean.phone,infobean.name}); db.close(); } public void query(String name) { SQLiteDatabase db = mySQLiteDataBase.getReadableDatabase(); Cursor cursor = db.rawQuery("select _id,name,phone from info where name =?;",new String[]{name}); if(cursor != null && cursor.getCount()>0){ while (cursor.moveToNext()){ int id = cursor.getInt(0); String mName = cursor.getString(1); String mPhone = cursor.getString(2); Log.d("msg",id + " "+mName + " " + mPhone); } cursor.close(); } db.close(); } }
public class MySQLiteDataBase extends SQLiteOpenHelper {//建立資料庫 public MySQLiteDataBase(Context context) { super(context,"info.db",null,1); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("create table info(_id integer primary key autoincrement," + "name varchar(20),phone varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
public class MainActivity extends AppCompatActivity implements View.OnClickListener {//需要執行的主函式
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MySQLiteDataBase mySQLiteDataBase = new MySQLiteDataBase(this);
SQLiteDatabase db = mySQLiteDataBase.getReadableDatabase();
findViewById(R.id.bt_add).setOnClickListener(this);
findViewById(R.id.bt_del).setOnClickListener(this);;
findViewById(R.id.bt_update).setOnClickListener(this);;
findViewById(R.id.bt_query).setOnClickListener(this);;
}
@Override
public void onClick(View view) {
InfoDao infoDao = new InfoDao(this);
switch (view.getId()){
case R.id.bt_add:
Infobean infobean = new Infobean();
infobean.name = "張三";
infobean.phone = "119";
infoDao.add(infobean);
Infobean infobean1 = new Infobean();
infobean1.name = "李三";
infobean1.phone = "110";
infoDao.add(infobean1);
break;
case R.id.bt_del:
infoDao.del("李三");
break;
case R.id.bt_update:
Infobean bean = new Infobean();
bean.name = "張三";
bean.phone = "120";
infoDao.update(bean);
break;
case R.id.bt_query:
infoDao.query("張三");
infoDao.query("李三");
break;
}
}
}
方式二:
方式一中相應操作的SQL語句都需要自己去寫,比較麻煩,也比較容易出錯,對此,android提供了相應封裝好的類來執行相應的操作。
public boolean add(Infobean infobean) {//增加的方法
SQLiteDatabase db = mySQLiteDataBase.getWritableDatabase();
// 方法二(有返回值):
ContentValues contentValues = new ContentValues();
contentValues.put("name",infobean.name);
contentValues.put("phone",infobean.phone);
long i = db.insert("info",null,contentValues);
db.close();
if(i == -1){
return false;
}
return true;
}
public void del(String name, Context context) {//刪除的方法
SQLiteDatabase db = mySQLiteDataBase.getReadableDatabase();
int i = db.delete("info","name = ? ",new String[]{name});
Toast.makeText(context,"刪除了"+ i+ "行",Toast.LENGTH_SHORT).show();
db.close();
}
public void update(Infobean infobean, Context context) {//修改的方法
SQLiteDatabase db = mySQLiteDataBase.getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("phone",infobean.phone);
int resule = db.update("info",contentValues,"name = ?",new String[]{infobean.name});
Toast.makeText(context,"修改了"+ resule + "行",Toast.LENGTH_SHORT).show();
db.close();
}
public void query(String name) {
SQLiteDatabase db = mySQLiteDataBase.getReadableDatabase();
Cursor cursor = db.query("info",new String[]{"_id","name","phone"},"name = ?",new String[]{name},null,null,"_id desc");
if(cursor != null && cursor.getCount()>0){
while (cursor.moveToNext()){
int id = cursor.getInt(0);
String mName = cursor.getString(1);
String mPhone = cursor.getString(2);
Log.d("msg",id + " "+mName + " " + mPhone);
}
cursor.close();
}
db.close();
}
在第二種方式中,最明顯的區別是第一種方式無返回值,而第二種方式有返回值。在應用中,增刪改建議用第二種方式,查建議用第一種,因為第二種方式一次只能查一個表,沒有辦法查多個表