網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
android?studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪查改_Android
作者:wow_awsl_qwq ? 更新時(shí)間: 2022-03-09 編程語(yǔ)言實(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(); } }
原文鏈接:https://blog.csdn.net/qq_42641977/article/details/121970828
相關(guān)推薦
- 2023-01-26 python獲取redis?memory使用情況場(chǎng)景分析_python
- 2022-04-19 一起來(lái)了解c語(yǔ)言的str函數(shù)_C 語(yǔ)言
- 2022-12-21 Oracle聯(lián)機(jī)日志文件與歸檔文件詳細(xì)介紹_oracle
- 2022-06-04 解決Go語(yǔ)言time包數(shù)字與時(shí)間相乘的問(wèn)題_Golang
- 2022-11-06 Android如何通過(guò)組合的方式自定義View_Android
- 2023-01-31 Android位圖(圖片)加載引入的內(nèi)存溢出問(wèn)題詳細(xì)解析_Android
- 2022-11-07 ASP.NET?MVC通過(guò)勾選checkbox更改select的內(nèi)容_實(shí)用技巧
- 2023-03-20 C#?BeginInvoke實(shí)現(xiàn)異步編程方式_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支