Android數(shù)據(jù)庫SQLite增改查刪操作演示

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);

}

}


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末先煎,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子巧涧,更是在濱河造成了極大的恐慌薯蝎,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谤绳,死亡現(xiàn)場離奇詭異占锯,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)缩筛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進(jìn)店門消略,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人瞎抛,你說我怎么就攤上這事艺演。” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵胎撤,是天一觀的道長晓殊。 經(jīng)常有香客問我,道長伤提,這世上最難降的妖魔是什么巫俺? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮肿男,結(jié)果婚禮上识藤,老公的妹妹穿的比我還像新娘。我一直安慰自己次伶,他們只是感情好痴昧,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著冠王,像睡著了一般赶撰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上柱彻,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天豪娜,我揣著相機(jī)與錄音,去河邊找鬼哟楷。 笑死瘤载,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的卖擅。 我是一名探鬼主播鸣奔,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼惩阶!你這毒婦竟也來了挎狸?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤断楷,失蹤者是張志新(化名)和其女友劉穎锨匆,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體冬筒,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡恐锣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了舞痰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片土榴。...
    茶點(diǎn)故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖匀奏,靈堂內(nèi)的尸體忽然破棺而出鞭衩,到底是詐尸還是另有隱情,我是刑警寧澤娃善,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布论衍,位于F島的核電站,受9級特大地震影響聚磺,放射性物質(zhì)發(fā)生泄漏坯台。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一瘫寝、第九天 我趴在偏房一處隱蔽的房頂上張望蜒蕾。 院中可真熱鬧,春花似錦焕阿、人聲如沸咪啡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽撤摸。三九已至,卻和暖如春褒纲,著一層夾襖步出監(jiān)牢的瞬間准夷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工莺掠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留衫嵌,地道東北人。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓彻秆,卻偏偏與公主長得像楔绞,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子唇兑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容