android?studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪查改
實(shí)驗(yàn)?zāi)康模?/strong>
分別使用sqlite3工具和Android代碼的方式建立SQLite數(shù)據(jù)庫(kù)。在完成建立數(shù)據(jù)庫(kù)的工作后,編程實(shí)現(xiàn)基本的數(shù)據(jù)庫(kù)操作功能,包括數(shù)據(jù)的添加、刪除和更新。
實(shí)驗(yàn)要求:
- 1.創(chuàng)建一個(gè)學(xué)生管理的應(yīng)用,基本信息包含學(xué)生姓名,班級(jí),學(xué)號(hào)。采用數(shù)據(jù)庫(kù)存儲(chǔ)這些信息。
- 2.應(yīng)用應(yīng)該至少包含信息錄入和刪除功能。
- 3.數(shù)據(jù)顯示考慮采用ListView。
實(shí)驗(yàn)效果:

工程結(jié)構(gòu):

源代碼:
DBAdapter.java
package com.example.shiyan6_sqlite;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBAdapter {
private static final String DB_NAME = "student.db";
private static final String DB_TABLE = "peopleinfo";
private static final int DB_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_BANJI = "banji";
public static final String KEY_XUEHAO = "xuehao";
private SQLiteDatabase db;
private final Context context;
private DBOpenHelper dbOpenHelper;
public DBAdapter(Context _context) {
context = _context;
}
public void close() {
if(db !=null)
{
db.close();
db=null;
}
}
public void open() throws SQLiteException {
dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
try {
db = dbOpenHelper.getWritableDatabase();
}
catch (SQLiteException ex) {
db = dbOpenHelper.getReadableDatabase();
}
}
public long insert(People people) {
ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, people.Name);
newValues.put(KEY_BANJI, people.Banji);
newValues.put(KEY_XUEHAO, people.Xuehao);
return db.insert(DB_TABLE, null, newValues);
}
public People[] queryAllData() {
Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_BANJI, KEY_XUEHAO},
null, null, null, null, null);
return ConvertToPeople(results);
}
public People[] queryOneData(long id) {
Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_BANJI, KEY_XUEHAO},
KEY_ID + "=" + id, null, null, null, null);
return ConvertToPeople(results);
}
@SuppressLint("Range")
private People[] ConvertToPeople(Cursor cursor){
int resultCounts = cursor.getCount();
if (resultCounts == 0 || !cursor.moveToFirst()){
return null;
}
People[] peoples = new People[resultCounts];
for (int i = 0 ; i<resultCounts; i++){
peoples[i] = new People();
peoples[i].ID = cursor.getInt(0);
peoples[i].Name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
peoples[i].Banji = cursor.getString(cursor.getColumnIndex(KEY_BANJI));
peoples[i].Xuehao = cursor.getString(cursor.getColumnIndex(KEY_XUEHAO));
cursor.moveToNext();
}
return peoples;
}
public long deleteAllData() {
return db.delete(DB_TABLE, null, null);
}
public long deleteOneData(long id) {
return db.delete(DB_TABLE, KEY_ID + "=" + id, null);
}
public long updateOneData(long id , People people){
ContentValues updateValues = new ContentValues();
updateValues.put(KEY_NAME, people.Name);
updateValues.put(KEY_BANJI, people.Banji);
updateValues.put(KEY_XUEHAO, people.Xuehao);
return db.update(DB_TABLE, updateValues, KEY_ID + "=" + id, null);
}
private static class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
private static final String DB_CREATE = "create table " +
DB_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " +
KEY_NAME+ " text not null, " + KEY_BANJI+ " text not null," + KEY_XUEHAO + " text not null);";
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
_db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
onCreate(_db);
}
}
}
People.java
package com.example.shiyan6_sqlite;
public class People {
public int ID = -1;
public String Name;
public String Banji;
public String Xuehao;
@Override
public String toString(){
String result = "";
result += "ID:" + this.ID + ",";
result += "姓名:" + this.Name + ",";
result += "班級(jí):" + this.Banji + ", ";
result += "學(xué)號(hào):" + this.Xuehao;
return result;
}
}
MainActivity.java
package com.example.shiyan6_sqlite;
import androidx.appcompat.app.AppCompatActivity;
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;
public class MainActivity extends AppCompatActivity {
EditText e_xm,e_nl,e_sg,e_id;
TextView t_1;
Button b_add,b_allsee,b_clearsee,b_alldel,b_delid,b_seeid,b_updid;
DBAdapter dbAdapter;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e_xm=findViewById(R.id.e_xm);
e_nl=findViewById(R.id.e_nl);
e_sg=findViewById(R.id.e_sg);
b_add=findViewById(R.id.b_add);
b_allsee=findViewById(R.id.b_allsee);
b_clearsee=findViewById(R.id.b_clearall);
b_alldel=findViewById(R.id.b_delall);
b_delid=findViewById(R.id.b_delid);
b_seeid=findViewById(R.id.b_seeid);
b_updid=findViewById(R.id.b_updid);
e_id=findViewById(R.id.e_id);
t_1=findViewById(R.id.t_1);
dbAdapter=new DBAdapter(this);
dbAdapter.open();
b_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
People t=new People();
t.Name=e_xm.getText().toString();
t.Banji=e_nl.getText().toString();
t.Xuehao=e_sg.getText().toString();
long colunm=dbAdapter.insert(t);
if (colunm == -1 ){
t_1.setText("添加過(guò)程錯(cuò)誤!");
} else {
t_1.setText("成功添加數(shù)據(jù),ID:"+String.valueOf(colunm));
}
}
});
b_allsee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
People [] peoples =dbAdapter.queryAllData();
if (peoples == null){
t_1.setText("數(shù)據(jù)庫(kù)中沒(méi)有數(shù)據(jù)");
return;
}
String t="數(shù)據(jù)庫(kù):\n";
for(int i=0;i<peoples.length;++i){
t+=peoples[i].toString()+"\n";
}
t_1.setText(t);
}
});
b_clearsee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
t_1.setText("");
}
});
b_alldel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dbAdapter.deleteAllData();
t_1.setText("已刪除所有數(shù)據(jù)!");
}
});
b_delid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id=Integer.parseInt(e_id.getText().toString());
long result=dbAdapter.deleteOneData(id);
String msg = "刪除ID為"+e_id.getText().toString()+"的數(shù)據(jù)" + (result>0?"成功":"失敗");
t_1.setText(msg);
}
});
b_seeid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id=Integer.parseInt(e_id.getText().toString());
People people[]=dbAdapter.queryOneData(id);
if(people==null){
t_1.setText("Id為"+id+"的記錄不存在!");
}
else{
t_1.setText("查詢成功:\n"+people[0].toString());
}
}
});
b_updid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id=Integer.parseInt(e_id.getText().toString());
People t=new People();
t.Name=e_xm.getText().toString();
t.Banji=e_nl.getText().toString();
t.Xuehao=e_sg.getText().toString();
long n=dbAdapter.updateOneData(id,t);
if (n<0){
t_1.setText("更新過(guò)程錯(cuò)誤!");
} else {
t_1.setText("成功更新數(shù)據(jù),"+String.valueOf(n)+"條");
}
}
});
}
@Override
protected void onStop() {
super.onStop();
dbAdapter.close();
}
}
到此這篇關(guān)于vandroid studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪查改的文章就介紹到這了,更多相關(guān)數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪查改內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android 通過(guò)SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)管理
- Android四種數(shù)據(jù)存儲(chǔ)的應(yīng)用方式
- Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)
- Android SharedPreferences實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)功能
- android使用SharedPreferences進(jìn)行數(shù)據(jù)存儲(chǔ)
- Android開發(fā)教程之ContentProvider數(shù)據(jù)存儲(chǔ)
- 詳解Android的網(wǎng)絡(luò)數(shù)據(jù)存儲(chǔ)
- 5種Android數(shù)據(jù)存儲(chǔ)方式匯總
- 詳解Android數(shù)據(jù)存儲(chǔ)之SQLCipher數(shù)據(jù)庫(kù)加密
- Android 單例模式實(shí)現(xiàn)可復(fù)用數(shù)據(jù)存儲(chǔ)的詳細(xì)過(guò)程
相關(guān)文章
深入理解與運(yùn)行Android Jetpack組件之ViewModel
ViewModel是Android Jetpack組件之一,是一種用于管理UI相關(guān)數(shù)據(jù)的架構(gòu)組件,它能夠幫助開發(fā)者實(shí)現(xiàn)優(yōu)雅的數(shù)據(jù)驅(qū)動(dòng)和生命周期管理,本文將深入淺出地介紹ViewModel的使用和原理,帶你一步步掌握這個(gè)強(qiáng)大的組件2023-08-08
Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動(dòng)庫(kù)
這篇文章主要介紹了Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動(dòng)庫(kù)的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
一文理解Android系統(tǒng)中強(qiáng)指針的實(shí)現(xiàn)
因?yàn)锳ndroid中很多地方代碼是用C++編寫,為了能夠保證C++中指針能夠被正確的釋放,于是Android引入了其實(shí)在C++中已經(jīng)有的智能指針技術(shù)2021-10-10
Android 程序申請(qǐng)權(quán)限注意事項(xiàng)
本主要介紹Android 程序申請(qǐng)權(quán)限注意事項(xiàng),這里整理了相關(guān)資料,并詳細(xì)說(shuō)明如何避免開發(fā)的程序支持設(shè)備減少,有需要的小伙伴可以參考下2016-09-09
Android 坐標(biāo)系與視圖坐標(biāo)系圖解分析
下面小編就為大家?guī)?lái)一篇Android 坐標(biāo)系與視圖坐標(biāo)系圖解分析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
ViewPager滑動(dòng)靈敏度調(diào)整的方法實(shí)力
這篇文章主要介紹了ViewPager滑動(dòng)靈敏度調(diào)整的方法實(shí)力,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Android DrawableTextView圖片文字居中顯示實(shí)例
在我們開發(fā)中,TextView設(shè)置Android:drawableLeft一定使用的非常多,但Drawable和Text同時(shí)居中顯示可能不好控制,小編想到通過(guò)自定義TextView實(shí)現(xiàn),具體詳情大家參考下本文2017-03-03
Android ApiDemo示例工程的創(chuàng)建
本文主要介紹Android ApiDemo示例工程的創(chuàng)建,這里SDK中的示例工程做了大致介紹,并說(shuō)明如何創(chuàng)建ApiDemo 示例工程,有需要看自帶代碼的朋友可以參考下2016-09-09

