一、实验目的
掌握数据库的创建。
掌握数据库增删改查。
二、实验内容
任务要求:自行设计个人记账本主界面,界面中设计3个输入框,分别为消费种类,如买铅笔,开销花费金额(如:100元),消费时间(当前系统时间),点击记账按钮将账目数据写入数据库表。点击查询按钮将表中账目数据显示在页面中。点击删除按钮可以根据消费种类删除某条数据,点击修改按钮可以根据消费种类修改开销花费金额。
实验效果要求:
实现数据库的增删改查即可。截图演示一条数据的新增、查询、更新、删除。
三、实验指导
- 创建类继承SQLiteOpenHelper,作为数据库使用帮助类。
- 初始化数据库帮助类,并使用数据库帮助类。SQLiteDatabase db = dbHelper.getWritableDatabase()。
- 使用sql语句对数据库进行增删改查。
- 将模拟器运行结果截图到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>





