Android系統(tǒng)集成了一個輕量級的關(guān)系數(shù)據(jù)庫---SQLite(占用資源少女轿,運(yùn)行效率高闲昭,安全可靠蜂莉,可移植性強(qiáng))
sqliteDemon效果圖實(shí)現(xiàn)了增刪改查的功能:
本案例參考了Android數(shù)據(jù)庫SQLite增改查刪操作演示?以及Android:Sqlite刪除數(shù)據(jù)?和Android:SQLiteOpenHelper類(SQLlite數(shù)據(jù)庫操作)詳細(xì)解析
首先創(chuàng)建一個sqlite3數(shù)據(jù)庫:
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 {
//定義創(chuàng)建數(shù)據(jù)表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);//重寫構(gòu)造方法并設(shè)置工廠為null
? ? }
@Override
? ? public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_SQL);//創(chuàng)建單詞信息表
? ? }
@Override
? ? // 重寫基類的onUpgrade()方法贰逾,以便數(shù)據(jù)庫版本更新
? ? public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
//提示版本更新并輸出舊版本信息與新版本信息
? ? ? ? System.out.println("---版本更新-----" + oldVersion +"--->" + newVersion);
}
}
創(chuàng)建android的布局文件:
<?xml version="1.0" encoding="utf-8"?>
? ? 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="請輸入要插入的數(shù)據(jù)">
? ? ? ? ? ? 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="請輸入更新前的內(nèi)容">
? ? ? ? ? ? android:layout_width="150dp"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:id="@+id/edit_after"
? ? ? ? ? ? android:hint="請輸入更新后的內(nèi)容">
? ? ? ? ? ? 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="請輸入要刪除的數(shù)據(jù)">
? ? ? ? ? ? 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="查詢結(jié)果在此處">
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;
}
}
//數(shù)據(jù)庫刪除記錄
? ? private void delete() {
String content_delete=mEditDelete.getText().toString();
SQLiteDatabase db =dbOpenHelper.getWritableDatabase();
//判斷用戶未添加要刪除的信息
? ? ? ? if (content_delete.isEmpty()){
Toast.makeText(MainActivity.this,"你所要刪除的數(shù)據(jù)內(nèi)容為空",Toast.LENGTH_SHORT).show();
}else {
db.delete("Lord","name=?",new String[]{content_delete});
Toast.makeText(MainActivity.this,"你所要刪除的數(shù)據(jù)刪除成功", Toast.LENGTH_SHORT).show();
}
}
//數(shù)據(jù)庫查詢記錄
//設(shè)置查詢的方法
? ? private void query() {
//建立游標(biāo)對象
? ? ? ? Cursor cursor=dbOpenHelper.getReadableDatabase().query("Lord",new String [] {"name"},null,null,null,null,null);
String text_data="";
//利用游標(biāo)遍歷所有數(shù)據(jù)對象
? ? ? ? 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,"查詢結(jié)果如下",Toast.LENGTH_SHORT).show();
}
//清除所有的記錄【清除所有的按鈕】
? ? private void clean() {
mEditInsert.setText("");
mEditAfter.setText("");
mEditBefore.setText("");
mEditDelete.setText("");
textview.setText("");
Toast.makeText(MainActivity.this,"清除所有數(shù)據(jù)成功",Toast.LENGTH_SHORT).show();
}
@Override
? ? protected void onDestroy() {
super.onDestroy();
if (dbOpenHelper!=null){
//關(guān)閉游標(biāo)赊瞬,釋放資源
? ? ? ? ? ? dbOpenHelper.close();
}
}
//數(shù)據(jù)庫修改記錄
? ? 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,"要修改的數(shù)據(jù)不能為空", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"修改數(shù)據(jù)成功", 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,"插入數(shù)據(jù)不能為空", Toast.LENGTH_SHORT).show();
}else {
insertData(dbOpenHelper.getReadableDatabase(), name);
Toast.makeText(MainActivity.this,"插入數(shù)據(jù)成功", Toast.LENGTH_SHORT).show();
}
}
//創(chuàng)建存放數(shù)據(jù)的ContentValues對象
? ? private void insertData(SQLiteDatabase sqLiteDatabase, String name) {
ContentValues values1 =new ContentValues();
values1.put("name", name);
//數(shù)據(jù)庫執(zhí)行插入命令
? ? ? ? sqLiteDatabase.insert("Lord",null, values1);
}
}