话不多说,先献上你们最爱的效果图~
这边我用的是一个ListView来展示数据库里的数据
public class OpenHelper extends SQLiteOpenHelper {
public OpenHelper(Context context) {
super(context,"sq.db",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//拓展:主键标识 primary key autoincrement
String sql="create table "+"tbname"+"(id text,name text,age text )";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
OpenHelper openHelper= new OpenHelper(SQActivity.this);
SQLiteDatabase db = openHelper.getWritableDatabase();
这样子可以说准备工作就做完了,可以开始功能的实现了。
首先实例化一下ContentValues,然后用put的方式以key-value(键对值)的形式传入数据,最后SQLiteDatabase的insert方法插入数据库。
private void setSQ(String id, String name, String age) { //增
ContentValues cv=new ContentValues();
cv.put("id", id);
cv.put("name", name);
cv.put("age", age);
//分别插入id name age三个数据,然后调用insert方法进行插入
db.insert("tbname", null, cv);
}
删除就一行代码~SQLiteDatabase的delete的方法实现,这里只演示根据id来删除对应的数据。
private void getDel(String str_del) { //删
//这里仅演示根据ID删除某条数据
db.delete("tbname", "id=?", new String[]{str_del});
}
首先创建一个Cursor 游标,然后遍历所有数据,这里由于我是用的ListView来展示数据,所以在while循环里面直接把数据传入到List中,cursor.getString(0)这个就是获取你每条数据的第一个数据的意思,也就是我传入的id。
private void getSQ() { //查
//创建游标 cursor 并用游标遍历所有数据
Cursor cursor = db.query("tbname", null, null, null, null, null, null);
while (cursor.moveToNext()) {
bean = new SQBean(cursor.getString(0), cursor.getString(1), cursor.getString(2));
list.add(bean);
}
cursor.close();//关闭游标(cursor)释放资源
}
更改数据库中的内容的话,你需要再实例化一个ContentValues,然后添加一个数据到里面,最后利用SQLiteDatabase的update方法实现新旧数据的更新。(我这里是对name来进行更新)
private void getUpData() {//改
ContentValues cv_Updata=new ContentValues();
cv_Updata.put("name",editUpdatastr.getText().toString());
db.update("tbname", cv_Updata, "name=?", new String[]{editUpdataid.getText().toString()});
}
如果还有什么不懂的可以下方评论区留言讨论!
SQActivity.java
public class SQActivity extends AppCompatActivity implements View.OnClickListener {
private EditText e1;
private EditText e2;
private EditText e3;
private Button btn;
private ListView lv;
private EditText editDel;
private Button buttonDel;
private EditText editUpdataid;
private EditText editUpdatastr;
private Button buttonUpdata;
private Button buttonRead;
private OpenHelper openHelper;
private SQLiteDatabase db;
private SQBean bean;
private SQAdapter adapter;
private List<SQBean> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sq);
initView();
openHelper= new OpenHelper(SQActivity.this);
db = openHelper.getWritableDatabase();
adapter = new SQAdapter(SQActivity.this, list);
lv.setAdapter(adapter);
}
private void setSQ(String id, String name, String age) { //增
ContentValues cv=new ContentValues();
cv.put("id", id);
cv.put("name", name);
cv.put("age", age);
//分别插入id name age三歌数据,然后调用insert方法进行插入
db.insert("tbname", null, cv);
}
private void getDel(String str_del) { //删
//这里仅演示根据ID删除某条数据
db.delete("tbname", "id=?", new String[]{str_del});
}
private void getSQ() { //查
//创建游标 cursor 并用游标遍历所有数据
Cursor cursor = db.query("tbname", null, null, null, null, null, null);
while (cursor.moveToNext()) {
bean = new SQBean(cursor.getString(0), cursor.getString(1), cursor.getString(2));
list.add(bean);
}
cursor.close();//关闭游标(cursor)释放资源
}
private void getUpData() {//改
ContentValues cv_Updata=new ContentValues();
cv_Updata.put("name",editUpdatastr.getText().toString());
db.update("tbname", cv_Updata, "name=?", new String[]{editUpdataid.getText().toString()});
}
private void initView() {
e1 = (EditText) findViewById(R.id.e1);
e2 = (EditText) findViewById(R.id.e2);
e3 = (EditText) findViewById(R.id.e3);
btn = (Button) findViewById(R.id.btn);
lv = (ListView) findViewById(R.id.lv);
editDel = (EditText) findViewById(R.id.edit_del);
buttonDel = (Button) findViewById(R.id.button_del);
editUpdataid = (EditText) findViewById(R.id.edit_updataid);
editUpdatastr = (EditText) findViewById(R.id.edit_updatastr);
buttonUpdata = (Button) findViewById(R.id.button_updata);
buttonRead = (Button) findViewById(R.id.button_read);
btn.setOnClickListener(this);
buttonUpdata.setOnClickListener(this);
buttonRead.setOnClickListener(this);
buttonDel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn: //增
setSQ(e1.getText().toString(), e2.getText().toString(), e3.getText().toString());
Toast.makeText(this, "插入数据成功", Toast.LENGTH_SHORT).show();
break;
case R.id.button_del: //删
getDel(editDel.getText().toString());
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
break;
case R.id.button_read: //查
list.clear();//先清空旧数据防止重叠
getSQ();
adapter.notifyDataSetChanged();//更新List列表
Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
break;
case R.id.button_updata: //改
getUpData();
Toast.makeText(this, "更改成功", Toast.LENGTH_SHORT).show();
break;
}
}
}
activity_sq.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:gravity="center"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:singleLine="true"
android:maxLines="1"
android:hint="id"
android:id="@+id/e1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:singleLine="true"
android:maxLines="1"
android:hint="name"
android:layout_marginLeft="10dp"
android:id="@+id/e2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:singleLine="true"
android:maxLines="1"
android:hint="age"
android:layout_marginLeft="10dp"
android:id="@+id/e3"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:text="插入"
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:gravity="center"
android:hint="id"
android:id="@+id/edit_del"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="删除"
android:id="@+id/button_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:gravity="center"
android:hint="old"
android:id="@+id/edit_updataid"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:gravity="center"
android:hint="new"
android:id="@+id/edit_updatastr"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="改"
android:id="@+id/button_updata"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:text="查询数据库"
android:id="@+id/button_read"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>