经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » SQLite » 查看文章
android studio数据存储建立SQLite数据库实现增删查改
来源:jb51  时间:2021/12/20 19:46:12  对本文有异议

实验目的:

分别使用sqlite3工具和Android代码的方式建立SQLite数据库。在完成建立数据库的工作后,编程实现基本的数据库操作功能,包括数据的添加、删除和更新。

实验要求:

  • 1.创建一个学生管理的应用,基本信息包含学生姓名,班级,学号。采用数据库存储这些信息。
  • 2.应用应该至少包含信息录入和删除功能。
  • 3.数据显示考虑采用ListView。

实验效果:

在这里插入图片描述

工程结构:

在这里插入图片描述

源代码:

DBAdapter.java

  1. package com.example.shiyan6_sqlite;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteException;
  9. import android.database.sqlite.SQLiteOpenHelper;
  10. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  11.  
  12. public class DBAdapter {
  13.  
  14. private static final String DB_NAME = "student.db";
  15. private static final String DB_TABLE = "peopleinfo";
  16. private static final int DB_VERSION = 1;
  17.  
  18. public static final String KEY_ID = "_id";
  19. public static final String KEY_NAME = "name";
  20. public static final String KEY_BANJI = "banji";
  21. public static final String KEY_XUEHAO = "xuehao";
  22.  
  23. private SQLiteDatabase db;
  24. private final Context context;
  25. private DBOpenHelper dbOpenHelper;
  26.  
  27. public DBAdapter(Context _context) {
  28. context = _context;
  29. }
  30.  
  31. public void close() {
  32. if(db !=null)
  33. {
  34. db.close();
  35. db=null;
  36. }
  37. }
  38.  
  39. public void open() throws SQLiteException {
  40. dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
  41. try {
  42. db = dbOpenHelper.getWritableDatabase();
  43. }
  44. catch (SQLiteException ex) {
  45. db = dbOpenHelper.getReadableDatabase();
  46. }
  47. }
  48.  
  49.  
  50. public long insert(People people) {
  51. ContentValues newValues = new ContentValues();
  52. newValues.put(KEY_NAME, people.Name);
  53. newValues.put(KEY_BANJI, people.Banji);
  54. newValues.put(KEY_XUEHAO, people.Xuehao);
  55.  
  56. return db.insert(DB_TABLE, null, newValues);
  57. }
  58.  
  59.  
  60. public People[] queryAllData() {
  61. Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_BANJI, KEY_XUEHAO},
  62. null, null, null, null, null);
  63. return ConvertToPeople(results);
  64. }
  65.  
  66. public People[] queryOneData(long id) {
  67. Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_BANJI, KEY_XUEHAO},
  68. KEY_ID + "=" + id, null, null, null, null);
  69. return ConvertToPeople(results);
  70. }
  71.  
  72. @SuppressLint("Range")
  73. private People[] ConvertToPeople(Cursor cursor){
  74. int resultCounts = cursor.getCount();
  75. if (resultCounts == 0 || !cursor.moveToFirst()){
  76. return null;
  77. }
  78. People[] peoples = new People[resultCounts];
  79. for (int i = 0 ; i<resultCounts; i++){
  80. peoples[i] = new People();
  81. peoples[i].ID = cursor.getInt(0);
  82. peoples[i].Name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
  83. peoples[i].Banji = cursor.getString(cursor.getColumnIndex(KEY_BANJI));
  84. peoples[i].Xuehao = cursor.getString(cursor.getColumnIndex(KEY_XUEHAO));
  85. cursor.moveToNext();
  86. }
  87. return peoples;
  88. }
  89.  
  90. public long deleteAllData() {
  91. return db.delete(DB_TABLE, null, null);
  92. }
  93.  
  94. public long deleteOneData(long id) {
  95. return db.delete(DB_TABLE, KEY_ID + "=" + id, null);
  96. }
  97.  
  98. public long updateOneData(long id , People people){
  99. ContentValues updateValues = new ContentValues();
  100. updateValues.put(KEY_NAME, people.Name);
  101. updateValues.put(KEY_BANJI, people.Banji);
  102. updateValues.put(KEY_XUEHAO, people.Xuehao);
  103.  
  104. return db.update(DB_TABLE, updateValues, KEY_ID + "=" + id, null);
  105. }
  106.  
  107. private static class DBOpenHelper extends SQLiteOpenHelper {
  108.  
  109. public DBOpenHelper(Context context, String name, CursorFactory factory, int version) {
  110. super(context, name, factory, version);
  111. }
  112.  
  113. private static final String DB_CREATE = "create table " +
  114. DB_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " +
  115. KEY_NAME+ " text not null, " + KEY_BANJI+ " text not null," + KEY_XUEHAO + " text not null);";
  116.  
  117. @Override
  118. public void onCreate(SQLiteDatabase _db) {
  119. _db.execSQL(DB_CREATE);
  120. }
  121.  
  122. @Override
  123. public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
  124. _db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
  125. onCreate(_db);
  126. }
  127. }
  128. }
  129.  

People.java

  1. package com.example.shiyan6_sqlite;
  2.  
  3. public class People {
  4. public int ID = -1;
  5. public String Name;
  6. public String Banji;
  7. public String Xuehao;
  8.  
  9. @Override
  10. public String toString(){
  11. String result = "";
  12. result += "ID:" + this.ID + ",";
  13. result += "姓名:" + this.Name + ",";
  14. result += "班级:" + this.Banji + ", ";
  15. result += "学号:" + this.Xuehao;
  16. return result;
  17. }
  18. }
  19.  
  20.  
  21. MainActivity.java
  22.  
  23. package com.example.shiyan6_sqlite;
  24.  
  25. import androidx.appcompat.app.AppCompatActivity;
  26.  
  27. import android.database.sqlite.SQLiteDatabase;
  28. import android.os.Bundle;
  29. import android.view.View;
  30. import android.widget.Button;
  31. import android.widget.EditText;
  32. import android.widget.TextView;
  33.  
  34. public class MainActivity extends AppCompatActivity {
  35.  
  36. EditText e_xm,e_nl,e_sg,e_id;
  37. TextView t_1;
  38. Button b_add,b_allsee,b_clearsee,b_alldel,b_delid,b_seeid,b_updid;
  39. DBAdapter dbAdapter;
  40. SQLiteDatabase db;
  41.  
  42. @Override
  43. protected void onCreate(Bundle savedInstanceState) {
  44. super.onCreate(savedInstanceState);
  45. setContentView(R.layout.activity_main);
  46.  
  47. e_xm=findViewById(R.id.e_xm);
  48. e_nl=findViewById(R.id.e_nl);
  49. e_sg=findViewById(R.id.e_sg);
  50. b_add=findViewById(R.id.b_add);
  51. b_allsee=findViewById(R.id.b_allsee);
  52. b_clearsee=findViewById(R.id.b_clearall);
  53. b_alldel=findViewById(R.id.b_delall);
  54. b_delid=findViewById(R.id.b_delid);
  55. b_seeid=findViewById(R.id.b_seeid);
  56. b_updid=findViewById(R.id.b_updid);
  57. e_id=findViewById(R.id.e_id);
  58. t_1=findViewById(R.id.t_1);
  59. dbAdapter=new DBAdapter(this);
  60. dbAdapter.open();
  61.  
  62.  
  63. b_add.setOnClickListener(new View.OnClickListener() {
  64. @Override
  65. public void onClick(View view) {
  66. People t=new People();
  67. t.Name=e_xm.getText().toString();
  68. t.Banji=e_nl.getText().toString();
  69. t.Xuehao=e_sg.getText().toString();
  70. long colunm=dbAdapter.insert(t);
  71. if (colunm == -1 ){
  72. t_1.setText("添加过程错误!");
  73. } else {
  74. t_1.setText("成功添加数据,ID:"+String.valueOf(colunm));
  75. }
  76. }
  77. });
  78.  
  79. b_allsee.setOnClickListener(new View.OnClickListener() {
  80. @Override
  81. public void onClick(View view) {
  82. People [] peoples =dbAdapter.queryAllData();
  83. if (peoples == null){
  84. t_1.setText("数据库中没有数据");
  85. return;
  86. }
  87. String t="数据库:\n";
  88. for(int i=0;i<peoples.length;++i){
  89. t+=peoples[i].toString()+"\n";
  90. }
  91. t_1.setText(t);
  92. }
  93. });
  94.  
  95. b_clearsee.setOnClickListener(new View.OnClickListener() {
  96. @Override
  97. public void onClick(View view) {
  98. t_1.setText("");
  99. }
  100. });
  101.  
  102. b_alldel.setOnClickListener(new View.OnClickListener() {
  103. @Override
  104. public void onClick(View view) {
  105. dbAdapter.deleteAllData();
  106. t_1.setText("已删除所有数据!");
  107. }
  108. });
  109.  
  110. b_delid.setOnClickListener(new View.OnClickListener() {
  111. @Override
  112. public void onClick(View view) {
  113. int id=Integer.parseInt(e_id.getText().toString());
  114. long result=dbAdapter.deleteOneData(id);
  115. String msg = "删除ID为"+e_id.getText().toString()+"的数据" + (result>0?"成功":"失败");
  116. t_1.setText(msg);
  117. }
  118. });
  119.  
  120. b_seeid.setOnClickListener(new View.OnClickListener() {
  121. @Override
  122. public void onClick(View view) {
  123. int id=Integer.parseInt(e_id.getText().toString());
  124. People people[]=dbAdapter.queryOneData(id);
  125. if(people==null){
  126. t_1.setText("Id为"+id+"的记录不存在!");
  127. }
  128. else{
  129. t_1.setText("查询成功:\n"+people[0].toString());
  130. }
  131. }
  132. });
  133.  
  134. b_updid.setOnClickListener(new View.OnClickListener() {
  135. @Override
  136. public void onClick(View view) {
  137. int id=Integer.parseInt(e_id.getText().toString());
  138. People t=new People();
  139. t.Name=e_xm.getText().toString();
  140. t.Banji=e_nl.getText().toString();
  141. t.Xuehao=e_sg.getText().toString();
  142. long n=dbAdapter.updateOneData(id,t);
  143. if (n<0){
  144. t_1.setText("更新过程错误!");
  145. } else {
  146. t_1.setText("成功更新数据,"+String.valueOf(n)+"条");
  147. }
  148. }
  149. });
  150. }
  151.  
  152. @Override
  153. protected void onStop() {
  154. super.onStop();
  155. dbAdapter.close();
  156. }
  157. }
  158.  

到此这篇关于vandroid studio数据存储建立SQLite数据库实现增删查改的文章就介绍到这了,更多相关数据存储建立SQLite数据库实现增删查改内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号