Android系统集成了一个轻量级的关系数据库---SQLite(占用资源少,运行效率高,安全可靠,可移植性强)
sqliteDemon效果图实现了增删改查的功能:
本案例参考了Android数据库SQLite增改查删操作演示 以及Android:Sqlite删除数据 和Android:SQLiteOpenHelper类(SQLlite数据库操作)详细解析
首先创建一个sqlite3数据库:
package com.example.sqlitedemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Administrator.
*/
public class DBOpenHelperextends SQLiteOpenHelper {
//定义创建数据表Lord的SQL语句
final StringCREATE_TABLE_SQL =
"create table Lord(_id integer primary " +
"key autoincrement , name)";
public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,int version) {
super(context, name,null, version);//重写构造方法并设置工厂为null
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_SQL);//创建单词信息表
}
@Override
// 重写基类的onUpgrade()方法,以便数据库版本更新
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
//提示版本更新并输出旧版本信息与新版本信息
System.out.println("---版本更新-----" + oldVersion +"--->" + newVersion);
}
}
创建android的布局文件:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SQLite增删改查删除操作演示"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_gravity="center"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/edit_insert"
android:hint="请输入要插入的数据">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/insert"
android:text="插入">
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/edit_before"
android:hint="请输入更新前的内容">
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/edit_after"
android:hint="请输入更新后的内容">
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/update"
android:text="修改">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/edit_delete"
android:hint="请输入要删除的数据">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/delete"
android:text="删除">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_query"
android:text="查询所有">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/clear"
android:text="清除所有">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:hint="查询结果在此处">
MainActivity的代码:
package com.example.sqlitedemo;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivityextends AppCompatActivityimplements View.OnClickListener {
private EditTextmEditInsert;
private ButtonmInsert;
private EditTextmEditBefore;
private EditTextmEditAfter;
private ButtonmUpdate;
private EditTextmEditDelete;
private ButtonmDelete;
private ButtonmEditQuery;
private ButtonmClear;
private DBOpenHelperdbOpenHelper;
private TextViewtextview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
dbOpenHelper =new DBOpenHelper(this,"Lord.db",null,1);
}
private void initView() {
mEditInsert = (EditText) findViewById(R.id.edit_insert);
mInsert = (Button) findViewById(R.id.insert);
mEditBefore = (EditText) findViewById(R.id.edit_before);
mEditAfter = (EditText) findViewById(R.id.edit_after);
mUpdate = (Button) findViewById(R.id.update);
mEditDelete = (EditText) findViewById(R.id.edit_delete);
mDelete = (Button) findViewById(R.id.delete);
mEditQuery = (Button) findViewById(R.id.edit_query);
mClear = (Button) findViewById(R.id.clear);
mInsert.setOnClickListener(this);
mUpdate.setOnClickListener(this);
mDelete.setOnClickListener(this);
mEditQuery.setOnClickListener(this);
mClear.setOnClickListener(this);
textview = (TextView) findViewById(R.id.textview);
textview.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.insert:
insert();
break;
case R.id.update:
update();
break;
case R.id.delete:
delete();
break;
case R.id.edit_query:
query();
break;
case R.id.clear:
clean();
break;
}
}
//数据库删除记录
private void delete() {
String content_delete=mEditDelete.getText().toString();
SQLiteDatabase db =dbOpenHelper.getWritableDatabase();
//判断用户未添加要删除的信息
if (content_delete.isEmpty()){
Toast.makeText(MainActivity.this,"你所要删除的数据内容为空",Toast.LENGTH_SHORT).show();
}else {
db.delete("Lord","name=?",new String[]{content_delete});
Toast.makeText(MainActivity.this,"你所要删除的数据删除成功", Toast.LENGTH_SHORT).show();
}
}
//数据库查询记录
//设置查询的方法
private void query() {
//建立游标对象
Cursor cursor=dbOpenHelper.getReadableDatabase().query("Lord",new String [] {"name"},null,null,null,null,null);
String text_data="";
//利用游标遍历所有数据对象
while (cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex("name"));
text_data=text_data+"\n"+name;
}
//为了显示全部,把所有对象连接起来,放到TextView中
textview.setText(text_data);
cursor.close();
Toast.makeText(MainActivity.this,"查询结果如下",Toast.LENGTH_SHORT).show();
}
//清除所有的记录【清除所有的按钮】
private void clean() {
mEditInsert.setText("");
mEditAfter.setText("");
mEditBefore.setText("");
mEditDelete.setText("");
textview.setText("");
Toast.makeText(MainActivity.this,"清除所有数据成功",Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dbOpenHelper!=null){
//关闭游标,释放资源
dbOpenHelper.close();
}
}
//数据库修改记录
private void update() {
String edit_before =mEditBefore.getText().toString();
String edit_after =mEditAfter.getText().toString();
if (edit_before.isEmpty() && edit_after.isEmpty()) {
Toast.makeText(MainActivity.this,"要修改的数据不能为空", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"修改数据成功", Toast.LENGTH_SHORT).show();
updateData(dbOpenHelper.getReadableDatabase(), edit_after);
}
}
private void updateData(SQLiteDatabase sqLiteDatabase, String name) {
String edit_before =mEditBefore.getText().toString();
String edit_after =mEditAfter.getText().toString();
ContentValues values2 =new ContentValues();
values2.put("name", edit_after);
sqLiteDatabase.update("Lord", values2,"name=?",new String[]{edit_before});
}
private void insert() {
String name =mEditInsert.getText().toString();
if (name.isEmpty()) {
Toast.makeText(MainActivity.this,"插入数据不能为空", Toast.LENGTH_SHORT).show();
}else {
insertData(dbOpenHelper.getReadableDatabase(), name);
Toast.makeText(MainActivity.this,"插入数据成功", Toast.LENGTH_SHORT).show();
}
}
//创建存放数据的ContentValues对象
private void insertData(SQLiteDatabase sqLiteDatabase, String name) {
ContentValues values1 =new ContentValues();
values1.put("name", name);
//数据库执行插入命令
sqLiteDatabase.insert("Lord",null, values1);
}
}