Android上机实验-7 数据库与存储技术

简介: Android上机实验-7 数据库与存储技术

一、实验目的

掌握数据库的创建。

掌握数据库增删改查

二、实验内容

任务要求:自行设计个人记账本主界面,界面中设计3个输入框,分别为消费种类,如买铅笔,开销花费金额(如:100元),消费时间(当前系统时间),点击记账按钮将账目数据写入数据库表。点击查询按钮将表中账目数据显示在页面中。点击删除按钮可以根据消费种类删除某条数据,点击修改按钮可以根据消费种类修改开销花费金额。

实验效果要求:

实现数据库的增删改查即可。截图演示一条数据的新增、查询、更新、删除。

三、实验指导

  1. 创建类继承SQLiteOpenHelper,作为数据库使用帮助类。
  2. 初始化数据库帮助类,并使用数据库帮助类。SQLiteDatabase db = dbHelper.getWritableDatabase()
  3. 使用sql语句对数据库进行增删改查。
  4. 将模拟器运行结果截图到word文档中,并将核心代码复制到word文档中。

四、模拟器效果截图

五、实验源代码

AccountsMain.java

1. package com.example.myapplication02;
2. 
3. import androidx.appcompat.app.AppCompatActivity;
4. 
5. import android.content.ContentValues;
6. import android.database.Cursor;
7. import android.database.sqlite.SQLiteDatabase;
8. import android.os.Bundle;
9. import android.view.View;
10. import android.widget.ArrayAdapter;
11. import android.widget.Button;
12. import android.widget.EditText;
13. import android.widget.ListView;
14. import android.widget.Toast;
15. 
16. import java.io.File;
17. import java.text.SimpleDateFormat;
18. import java.util.ArrayList;
19. import java.util.Date;
20. import java.util.List;
21. 
22. public class AccountsMain extends AppCompatActivity {
23.     AccountsOpen accountsOpen;
24.     SQLiteDatabase db;//数据库的实例对象
25.     EditText category,price;
26.     Button btn_add,btn_select,btn_update,btn_delete;
27.     ListView lv;
28. @Override
29. protected void onCreate(Bundle savedInstanceState) {
30. super.onCreate(savedInstanceState);
31.         setContentView(R.layout.accounts);
32.         initView();
33.         accountsOpen=new AccountsOpen(this);
34. //创建可读可写的数据库对象
35.         db=accountsOpen.getWritableDatabase();
36.         setonclick();
37.     }
38. private void initView() {
39.         category=findViewById(R.id.category);
40.         price=findViewById(R.id.price);
41.         btn_add=findViewById(R.id.btn_add);
42.         btn_select=findViewById(R.id.btn_select);
43.         btn_update=findViewById(R.id.btn_update);
44.         btn_delete=findViewById(R.id.btn_delete);
45.         lv=findViewById(R.id.lv);
46.     }
47. private void setonclick() {
48.         btn_add.setOnClickListener(new View.OnClickListener() {
49. @Override
50. public void onClick(View view) {
51. //新增时判断一下有木有这个水果,如果有,提示用户有了,只能更新不能新增
52.                 Cursor cursor= db.rawQuery("select * from account",null);
53.                 List<String> namelist=new ArrayList<>();
54. while(cursor.moveToNext()){
55.                     String categoryname=cursor.getString(1);
56.                     namelist.add(categoryname);
57.                 }
58. //获取用户输入的名字
59.                 String exited=category.getText().toString();
60. if(namelist.contains(exited)){
61.                     Toast.makeText(AccountsMain.this,"数据表中已经有了,只能更新不能新增",Toast.LENGTH_LONG).show();
62. return;
63.                 }
64.                 ContentValues values=new ContentValues();
65. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
66. Date date = new Date(System.currentTimeMillis());
67. String atime = simpleDateFormat.format(date);
68.                 values.put("category",category.getText().toString());
69.                 values.put("price",price.getText().toString());
70.                 values.put("time",atime);
71. long i= db.insert("account",null,values);
72. if(i>-1){
73.                     Toast.makeText(AccountsMain.this,"记账内容添加成功!",Toast.LENGTH_LONG).show();
74.                 }
75.             }
76.         });
77.         btn_select.setOnClickListener(new View.OnClickListener() {
78. @Override
79. public void onClick(View view) {
80.                 Cursor cursor= db.rawQuery("select * from account",null);
81.                 List<AccountsKeep> accountslist=new ArrayList<>();
82. if(cursor.getCount()!=0){
83. while(cursor.moveToNext()){
84.                         AccountsKeep accountsKeep=new AccountsKeep();
85.                         accountsKeep.setId(cursor.getInt(0));
86.                         accountsKeep.setCategory(cursor.getString(1));
87.                         accountsKeep.setPrice(cursor.getString(2));
88.                         accountsKeep.setTime(cursor.getString(3));
89.                         accountslist.add(accountsKeep);
90.                         accountsKeep=null;
91.                     }
92.                     cursor.close();
93. //数据已经放在集合中了,使用适配器绑定listview
94.                     ArrayAdapter<AccountsKeep> arrayAdapter=new ArrayAdapter<>(AccountsMain.this,
95.                             android.R.layout.simple_list_item_1,accountslist);
96.                     lv.setAdapter(arrayAdapter);
97.                 }
98.             }
99.         });
100.         btn_update.setOnClickListener(new View.OnClickListener() {
101. @Override
102. public void onClick(View view) {
103.                 ContentValues values=new ContentValues();
104.                 values.put("price",price.getText().toString());
105. int i= db.update("account",values,"category=?",new String[]{category.getText().toString()});
106. if(i>0){
107.                     Toast.makeText(AccountsMain.this,"记账内容更新成功!",Toast.LENGTH_LONG).show();
108.                 }
109.             }
110.         });
111.         btn_delete.setOnClickListener(new View.OnClickListener() {
112. @Override
113. public void onClick(View view) {
114. int i= db.delete("account","category=?",new String[]{category.getText().toString()});
115. if(i>0){
116.                     Toast.makeText(AccountsMain.this,"记账内容删除成功!",Toast.LENGTH_LONG).show();
117.                 }
118.             }
119.         });
120.     }
121. }

AccountsKeep.java

1. package com.example.myapplication02;
2. 
3. public class AccountsKeep {
4. int id;
5.     String category;
6.     String price;
7.     String time;
8. 
9. public int getId() {
10. return id;
11.     }
12. 
13. public void setId(int id) {
14. this.id = id;
15.     }
16. 
17. public String getCategory() {
18. return category;
19.     }
20. 
21. public void setCategory(String category) {
22. this.category = category;
23.     }
24. 
25. public String getPrice() {
26. return price;
27.     }
28. 
29. public void setPrice(String price) {
30. this.price = price;
31.     }
32. 
33. public String getTime() {
34. return time;
35.     }
36. 
37. public void setTime(String time) {
38. this.time = time;
39.     }
40. 
41. @Override
42. public String toString() {
43. return "AccountsKeep{" +
44. "id=" + id +
45. ", category='" + category + '\'' +
46. ", price='" + price + '\'' +
47. ", time='" + time + '\'' +
48. '}';
49.     }
50. }

AccountsOpen.java

1. package com.example.myapplication02;
2. 
3. import android.content.Context;
4. import android.database.sqlite.SQLiteDatabase;
5. import android.database.sqlite.SQLiteOpenHelper;
6. 
7. import androidx.annotation.Nullable;
8. 
9. public class AccountsOpen extends SQLiteOpenHelper {
10. 
11. public AccountsOpen(@Nullable Context context) {
12. //第二个参数:name  数据库的名称 account.db
13. //第三个参数:游标工厂,默认的游标工厂null
14. //第四个参数:version版本号  从1开始
15. super(context, "account.db", null, 1);
16.     }
17. 
18. @Override
19. public void onCreate(SQLiteDatabase sqLiteDatabase) {
20. //数据库第一次被创建的时候调用,适合做一些数据表结构的初始化
21.         sqLiteDatabase.execSQL("create table account (id INTEGER primary key autoincrement," +
22. "category varchar(30)," +
23. "price varchar(30)," +
24. "time varchar(30))");
25.     }
26. 
27. @Override
28. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
29. 
30.     }
31. }

accounts.xml

1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. android:background="@drawable/p01"
8. tools:context=".AccountsMain"
9. android:orientation="vertical">
10. 
11. <TextView
12. android:layout_width="match_parent"
13. android:layout_height="wrap_content"
14. android:text="记账本"
15. android:textSize="30sp"
16. android:textStyle="bold"
17. android:gravity="center"
18. android:layout_marginTop="50dp" />
19. <EditText
20. android:layout_width="match_parent"
21. android:layout_height="wrap_content"
22. android:layout_marginTop="50dp"
23. android:hint="请输入消费种类"
24. android:id="@+id/category"/>
25. <EditText
26. android:layout_width="match_parent"
27. android:layout_height="wrap_content"
28. android:layout_marginTop="10dp"
29. android:hint="请输入花费金额"
30. android:id="@+id/price"/>
31. <LinearLayout
32. android:layout_width="match_parent"
33. android:layout_height="wrap_content"
34. android:orientation="horizontal">
35. <Button
36. android:layout_width="0dp"
37. android:layout_weight="1"
38. android:layout_height="wrap_content"
39. android:text="记账添加"
40. android:id="@+id/btn_add"/>
41. <Button
42. android:layout_width="0dp"
43. android:layout_weight="1"
44. android:layout_height="wrap_content"
45. android:text="记账查询"
46. android:id="@+id/btn_select"/>
47. <Button
48. android:layout_width="0dp"
49. android:layout_weight="1"
50. android:layout_height="wrap_content"
51. android:text="记账更新"
52. android:id="@+id/btn_update"/>
53. <Button
54. android:layout_width="0dp"
55. android:layout_weight="1"
56. android:layout_height="wrap_content"
57. android:text="记账删除"
58. android:id="@+id/btn_delete"/>
59. </LinearLayout>
60. <ListView
61. android:layout_width="match_parent"
62. android:layout_height="wrap_content"
63. android:id="@+id/lv" />
64. </LinearLayout>
目录
相关文章
|
网络协议 关系型数据库 MySQL
如何实现无公网ip远程访问本地安卓Termux部署的MySQL数据库【内网穿透】
如何实现无公网ip远程访问本地安卓Termux部署的MySQL数据库【内网穿透】
|
8月前
|
XML 数据库 Android开发
Android数据库的使用(增删改查)
本文介绍了一个简单的数据库操作Demo,包含创建数据库、增删改查功能。通过5个按钮分别实现创建数据库、插入数据、删除数据、更新数据和查询数据的操作。代码结构清晰,适合初学者学习Android SQLite数据库基础操作。
265 5
|
8月前
|
数据库 Android开发
Android外部数据库的引用
简介:本文介绍了在Android项目中引用外部数据库的方法。首先,将现成的数据库文件放入项目的`assets`文件夹中(需手动创建)。其次,在APP引导界面通过代码将数据库拷贝至App目录下,确保数据库可用。最后,对数据库进行增删改查等操作。关键步骤包括判断数据库是否存在、使用`AssetManager`读取数据库文件并写入App私有目录,实现外部数据库的顺利集成与使用。
113 2
|
8月前
|
数据库 Android开发 开发者
Android常用的room增删改查语句(外部数据库)
本文分享了将一个原生数据库驱动的单词APP重构为使用Room库的过程及遇到的问题,重点解决了Room中增删改查的常用语句实现。文章通过具体示例(以“forget”表为例),详细展示了如何定义实体类、Dao接口、Database类以及Repository和ViewModel的设计与实现。同时,提供了插入、删除、更新和查询数据的代码示例,包括模糊查询、分页加载等功能。此外,针对外部数据库导入问题,作者建议可通过公众号“计蒙不吃鱼”获取更多支持。此内容适合有一定Room基础的开发者深入学习。
276 0
Android常用的room增删改查语句(外部数据库)
|
关系型数据库 MySQL Java
Django学习二:配置mysql,创建model实例,自动创建数据库表,对mysql数据库表已经创建好的进行直接操作和实验。
这篇文章是关于如何使用Django框架配置MySQL数据库,创建模型实例,并自动或手动创建数据库表,以及对这些表进行操作的详细教程。
605 0
Django学习二:配置mysql,创建model实例,自动创建数据库表,对mysql数据库表已经创建好的进行直接操作和实验。
|
数据库 Android开发 数据安全/隐私保护
在 Android Studio 中结合使用 SQLite 数据库实现简单的注册和登录功能
在 Android Studio 中结合使用 SQLite 数据库实现简单的注册和登录功能
505 2
|
SQL 存储 数据库
48. 【Android教程】数据库:SQLite 的使用
48. 【Android教程】数据库:SQLite 的使用
512 1
|
数据库 Android开发
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
|
存储 Oracle 关系型数据库
实验三 Oracle数据库的创建和管理
实验三 Oracle数据库的创建和管理
254 1
|
SQL Oracle 关系型数据库
实验一 安装和使用Oracle数据库
实验一 安装和使用Oracle数据库
264 1