采用android studio自带的数据库实现stu数据库和stu表的创建,增删改查和关闭

这是项目的大致结构

主界面

子界面

布局源码

  1. <!-- Main -->
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. >
  9. <Button
  10. android:id="@+id/b0"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:textSize="10pt"
  14. android:text="创建学生信息表"
  15. android:layout_margin="10dp"/>
  16. <Button
  17. android:id="@+id/b1"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:textSize="10pt"
  21. android:text="增加数据"
  22. android:layout_margin="10dp"
  23. />
  24. <Button
  25. android:id="@+id/b2"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:textSize="10pt"
  29. android:text="删除数据"
  30. android:layout_margin="10dp"
  31. />
  32. <Button
  33. android:id="@+id/b3"
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:textSize="10pt"
  37. android:text="修改数据"
  38. android:layout_margin="10dp"
  39. />
  40. <Button
  41. android:id="@+id/b4"
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content"
  44. android:textSize="10pt"
  45. android:text="查询数据"
  46. android:layout_margin="10dp"
  47. />
  48. <Button
  49. android:id="@+id/b5"
  50. android:layout_width="match_parent"
  51. android:layout_height="wrap_content"
  52. android:textSize="10pt"
  53. android:text="浏览数据"
  54. android:layout_margin="10dp"
  55. />
  56. <Button
  57. android:id="@+id/b6"
  58. android:layout_width="match_parent"
  59. android:layout_height="wrap_content"
  60. android:textSize="10pt"
  61. android:text="清空表格"
  62. android:layout_margin="10dp"
  63. />
  64. <Button
  65. android:id="@+id/b7"
  66. android:layout_width="match_parent"
  67. android:layout_height="wrap_content"
  68. android:textSize="10pt"
  69. android:text="关闭数据库"
  70. android:layout_margin="10dp"
  71. />
  72. </LinearLayout>
  73. <!-- Add -->
  74. <?xml version="1.0" encoding="utf-8"?>
  75. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  76. android:layout_width="match_parent"
  77. android:layout_height="match_parent"
  78. android:orientation="vertical"
  79. >
  80. <TextView
  81. android:layout_width="match_parent"
  82. android:layout_height="wrap_content"
  83. android:textSize="10pt"
  84. android:layout_margin="10dp"
  85. android:text="请输入学号"
  86. android:textColor="@android:color/holo_blue_bright"
  87. />
  88. <EditText
  89. android:layout_width="match_parent"
  90. android:layout_height="wrap_content"
  91. android:textSize="10pt"
  92. android:inputType="text"
  93. android:layout_margin="10dp"
  94. android:hint="请输入学号"
  95. android:id="@+id/id"
  96. />
  97. <TextView
  98. android:textColor="@android:color/holo_blue_bright"
  99. android:layout_width="match_parent"
  100. android:layout_height="wrap_content"
  101. android:textSize="10pt"
  102. android:layout_margin="10dp"
  103. android:text="请输入姓名"
  104. />
  105. <EditText
  106. android:layout_width="match_parent"
  107. android:layout_height="wrap_content"
  108. android:textSize="10pt"
  109. android:inputType="text"
  110. android:layout_margin="10dp"
  111. android:hint="请输入姓名"
  112. android:id="@+id/name"
  113. />
  114. <TextView
  115. android:textColor="@android:color/holo_blue_bright"
  116. android:layout_width="match_parent"
  117. android:layout_height="wrap_content"
  118. android:textSize="10pt"
  119. android:layout_margin="10dp"
  120. android:text="请输入年龄"
  121. />
  122. <EditText
  123. android:layout_width="match_parent"
  124. android:layout_height="wrap_content"
  125. android:textSize="10pt"
  126. android:layout_margin="10dp"
  127. android:inputType="text"
  128. android:hint="请输入年龄"
  129. android:id="@+id/age"
  130. />
  131. <TextView
  132. android:textColor="@android:color/holo_blue_bright"
  133. android:layout_width="match_parent"
  134. android:layout_height="wrap_content"
  135. android:textSize="10pt"
  136. android:text="请选择性别"
  137. />
  138. <RadioGroup
  139. android:layout_width="match_parent"
  140. android:layout_height="wrap_content"
  141. android:layout_margin="10dp"
  142. android:id="@+id/sex"
  143. android:orientation="horizontal"
  144. >
  145. <RadioButton
  146. android:id="@+id/man"
  147. android:layout_width="wrap_content"
  148. android:layout_height="wrap_content"
  149. android:textSize="10pt"
  150. android:layout_margin="10dp"
  151. android:text="男"
  152. />
  153. <RadioButton
  154. android:id="@+id/woman"
  155. android:layout_width="wrap_content"
  156. android:layout_height="wrap_content"
  157. android:textSize="10pt"
  158. android:layout_margin="10dp"
  159. android:text="女"
  160. />
  161. </RadioGroup>
  162. <Button
  163. android:layout_width="match_parent"
  164. android:layout_height="wrap_content"
  165. android:textSize="10pt"
  166. android:layout_margin="10dp"
  167. android:text="添加"
  168. android:id="@+id/add"
  169. />
  170. </LinearLayout>
  171. <!-- Delete -->
  172. <?xml version="1.0" encoding="utf-8"?>
  173. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  174. android:layout_width="match_parent"
  175. android:layout_height="match_parent"
  176. android:orientation="vertical"
  177. >
  178. <TextView
  179. android:textColor="@android:color/holo_blue_bright"
  180. android:layout_width="match_parent"
  181. android:layout_height="wrap_content"
  182. android:textSize="10pt"
  183. android:layout_margin="10dp"
  184. android:text="通过学号删除"
  185. />
  186. <EditText
  187. android:layout_width="match_parent"
  188. android:layout_height="wrap_content"
  189. android:textSize="10pt"
  190. android:layout_margin="10dp"
  191. android:inputType="text"
  192. android:hint="请输入学号"
  193. android:id="@+id/id"
  194. />
  195. <Button
  196. android:textColor="@android:color/holo_red_dark"
  197. android:layout_width="match_parent"
  198. android:layout_height="wrap_content"
  199. android:textSize="10pt"
  200. android:layout_margin="10dp"
  201. android:text="删除"
  202. android:id="@+id/delete"
  203. />
  204. </LinearLayout>
  205. <!-- Update -->
  206. <?xml version="1.0" encoding="utf-8"?>
  207. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  208. android:layout_width="match_parent"
  209. android:layout_height="match_parent"
  210. android:orientation="vertical"
  211. >
  212. <TextView
  213. android:layout_width="match_parent"
  214. android:layout_height="wrap_content"
  215. android:textSize="10pt"
  216. android:layout_margin="5dp"
  217. android:text="请输入旧学号"
  218. android:textColor="@android:color/holo_blue_bright"
  219. />
  220. <EditText
  221. android:layout_width="match_parent"
  222. android:layout_height="wrap_content"
  223. android:textSize="10pt"
  224. android:inputType="text"
  225. android:layout_margin="5dp"
  226. android:hint="请输入旧学号"
  227. android:id="@+id/oldid"
  228. />
  229. <TextView
  230. android:layout_width="match_parent"
  231. android:layout_height="wrap_content"
  232. android:textSize="10pt"
  233. android:layout_margin="5dp"
  234. android:text="请输入学号"
  235. android:textColor="@android:color/holo_blue_bright"
  236. />
  237. <EditText
  238. android:layout_width="match_parent"
  239. android:layout_height="wrap_content"
  240. android:textSize="10pt"
  241. android:inputType="text"
  242. android:layout_margin="5dp"
  243. android:hint="请输入学号"
  244. android:id="@+id/id"
  245. />
  246. <TextView
  247. android:textColor="@android:color/holo_blue_bright"
  248. android:layout_width="match_parent"
  249. android:layout_height="wrap_content"
  250. android:textSize="10pt"
  251. android:layout_margin="5dp"
  252. android:text="请输入姓名"
  253. />
  254. <EditText
  255. android:layout_width="match_parent"
  256. android:layout_height="wrap_content"
  257. android:textSize="10pt"
  258. android:inputType="text"
  259. android:layout_margin="5dp"
  260. android:hint="请输入姓名"
  261. android:id="@+id/name"
  262. />
  263. <TextView
  264. android:textColor="@android:color/holo_blue_bright"
  265. android:layout_width="match_parent"
  266. android:layout_height="wrap_content"
  267. android:textSize="10pt"
  268. android:layout_margin="5dp"
  269. android:text="请输入年龄"
  270. />
  271. <EditText
  272. android:layout_width="match_parent"
  273. android:layout_height="wrap_content"
  274. android:textSize="10pt"
  275. android:layout_margin="5dp"
  276. android:inputType="text"
  277. android:hint="请输入年龄"
  278. android:id="@+id/age"
  279. />
  280. <TextView
  281. android:textColor="@android:color/holo_blue_bright"
  282. android:layout_width="match_parent"
  283. android:layout_height="wrap_content"
  284. android:textSize="10pt"
  285. android:text="请选择性别"
  286. />
  287. <RadioGroup
  288. android:layout_width="match_parent"
  289. android:layout_height="wrap_content"
  290. android:layout_margin="5dp"
  291. android:id="@+id/sex"
  292. android:orientation="horizontal"
  293. >
  294. <RadioButton
  295. android:layout_width="wrap_content"
  296. android:layout_height="wrap_content"
  297. android:textSize="10pt"
  298. android:layout_margin="5dp"
  299. android:text="男"
  300. />
  301. <RadioButton
  302. android:layout_width="wrap_content"
  303. android:layout_height="wrap_content"
  304. android:textSize="10pt"
  305. android:layout_margin="5dp"
  306. android:text="女"
  307. />
  308. </RadioGroup>
  309. <Button
  310. android:layout_width="match_parent"
  311. android:layout_height="wrap_content"
  312. android:textSize="10pt"
  313. android:layout_margin="5dp"
  314. android:text="修改"
  315. android:id="@+id/update"
  316. />
  317. </LinearLayout>
  318. <!-- Select -->
  319. <?xml version="1.0" encoding="utf-8"?>
  320. <LinearLayout
  321. xmlns:android="http://schemas.android.com/apk/res/android"
  322. android:layout_width="match_parent"
  323. android:layout_height="match_parent"
  324. android:orientation="vertical"
  325. >
  326. <TextView
  327. android:textColor="@android:color/holo_blue_bright"
  328. android:layout_width="match_parent"
  329. android:layout_height="wrap_content"
  330. android:textSize="10pt"
  331. android:layout_margin="10dp"
  332. android:text="通过学号查询"
  333. />
  334. <EditText
  335. android:layout_width="match_parent"
  336. android:layout_height="wrap_content"
  337. android:textSize="10pt"
  338. android:layout_margin="10dp"
  339. android:inputType="text"
  340. android:hint="请输入学号"
  341. android:id="@+id/id"
  342. />
  343. <Button
  344. android:textColor="@android:color/holo_red_dark"
  345. android:layout_width="match_parent"
  346. android:layout_height="wrap_content"
  347. android:textSize="10pt"
  348. android:layout_margin="10dp"
  349. android:text="查询"
  350. android:id="@+id/select"
  351. />
  352. <ListView
  353. android:layout_width="match_parent"
  354. android:layout_height="wrap_content"
  355. android:id="@+id/list"
  356. android:divider="@color/black"
  357. />
  358. </LinearLayout>
  359. <!-- Browse -->
  360. <?xml version="1.0" encoding="utf-8"?>
  361. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  362. android:layout_height="match_parent"
  363. android:layout_width="match_parent"
  364. android:orientation="vertical"
  365. >
  366. <ListView
  367. android:layout_width="match_parent"
  368. android:layout_height="wrap_content"
  369. android:id="@+id/list"
  370. android:divider="@color/black"
  371. />
  372. </LinearLayout>
  373. <!-- StuAdapter -->
  374. <?xml version="1.0" encoding="utf-8"?>
  375. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  376. android:layout_width="match_parent"
  377. android:layout_height="match_parent"
  378. android:orientation="horizontal"
  379. android:background="@color/black"
  380. >
  381. <TextView
  382. android:id="@+id/t1"
  383. android:layout_width="0dp"
  384. android:layout_height="match_parent"
  385. android:layout_weight="1"
  386. android:layout_margin="2dp"
  387. android:background="@color/white"
  388. />
  389. <TextView
  390. android:id="@+id/t2"
  391. android:layout_width="0dp"
  392. android:layout_height="match_parent"
  393. android:layout_weight="1"
  394. android:layout_margin="2dp"
  395. android:background="@color/white"
  396. />
  397. <TextView
  398. android:id="@+id/t3"
  399. android:layout_width="0dp"
  400. android:layout_height="match_parent"
  401. android:layout_weight="1"
  402. android:layout_margin="2dp"
  403. android:background="@color/white"
  404. />
  405. <TextView
  406. android:id="@+id/t4"
  407. android:layout_width="0dp"
  408. android:layout_height="match_parent"
  409. android:layout_weight="1"
  410. android:layout_margin="2dp"
  411. android:background="@color/white"
  412. />
  413. </LinearLayout>

后台JAVA代码

  1. //Stu
  2. package com.example.stuapp.pojo;
  3. public class Stu {
  4. private String id,name,age,sex;
  5. public Stu(String id, String name, String age, String sex) {
  6. this.id = id;
  7. this.name = name;
  8. this.age = age;
  9. this.sex = sex;
  10. }
  11. public Stu() {
  12. }
  13. @Override
  14. public String toString() {
  15. return "Stu{" +
  16. "id='" + id + '\'' +
  17. ", name='" + name + '\'' +
  18. ", age='" + age + '\'' +
  19. ", sex='" + sex + '\'' +
  20. '}';
  21. }
  22. public String getId() {
  23. return id;
  24. }
  25. public void setId(String id) {
  26. this.id = id;
  27. }
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public String getAge() {
  35. return age;
  36. }
  37. public void setAge(String age) {
  38. this.age = age;
  39. }
  40. public String getSex() {
  41. return sex;
  42. }
  43. public void setSex(String sex) {
  44. this.sex = sex;
  45. }
  46. }
  47. //StuHelp
  48. package com.example.stuapp.utils;
  49. import android.content.ContentValues;
  50. import android.content.Context;
  51. import android.database.Cursor;
  52. import android.database.sqlite.SQLiteDatabase;
  53. import android.database.sqlite.SQLiteOpenHelper;
  54. import androidx.annotation.Nullable;
  55. import androidx.appcompat.app.AlertDialog;
  56. import com.example.stuapp.MainActivity;
  57. import com.example.stuapp.pojo.Stu;
  58. import java.util.ArrayList;
  59. import java.util.List;
  60. public class StuHelp extends SQLiteOpenHelper {
  61. private static StuHelp stuHelp;
  62. private static final String Name="stu";
  63. public static StuHelp getStuHelp(Context context) {
  64. if(stuHelp==null) stuHelp=new StuHelp(context);
  65. return stuHelp;
  66. }
  67. public long add(SQLiteDatabase sqLiteDatabase, Stu stu){
  68. System.out.println(stu.toString());
  69. ContentValues values=new ContentValues();
  70. values.put("id",stu.getId());
  71. values.put("name",stu.getName());
  72. values.put("age",stu.getAge());
  73. values.put("sex",stu.getSex());
  74. return sqLiteDatabase.insert(Name,null,values);
  75. }
  76. public long delete(SQLiteDatabase sqLiteDatabase,String id){
  77. return sqLiteDatabase.delete(Name,"id=?",new String[]{id});
  78. }
  79. public long update(SQLiteDatabase sqLiteDatabase,Stu stu,String id){
  80. ContentValues values=new ContentValues();
  81. if(stu.getId()!=null&&!stu.getId().equals(""))
  82. values.put("id",stu.getId());
  83. if(stu.getName()!=null&&!stu.getName().equals(""))
  84. values.put("name",stu.getName());
  85. if(stu.getAge()!=null&&!stu.getAge().equals(""))
  86. values.put("age",stu.getAge());
  87. if(stu.getSex()!=null&&!stu.getSex().equals(""))
  88. values.put("sex",stu.getSex());
  89. return sqLiteDatabase.update(Name,values,"id=?",new String[]{id});
  90. }
  91. public Stu select(SQLiteDatabase sqLiteDatabase,String id){
  92. Cursor cursor=sqLiteDatabase.rawQuery("select * from stu where id=?",new String[]{id});
  93. Stu stu=null;
  94. if(cursor.getCount()>0){ cursor.moveToNext();stu=new Stu(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3));}
  95. return stu;
  96. }
  97. public List<Stu> browse(SQLiteDatabase sqLiteDatabase){
  98. List<Stu> stus=new ArrayList<>();
  99. Cursor cursor =sqLiteDatabase.query(Name,null,null,null,null,null,null);
  100. while (cursor.moveToNext()){
  101. Stu stu=new Stu();
  102. stu.setId(cursor.getString(0));
  103. stu.setName(cursor.getString(1));
  104. stu.setAge(cursor.getString(2));
  105. stu.setSex(cursor.getString(3));
  106. stus.add(stu);
  107. }
  108. return stus;
  109. }
  110. private StuHelp(@Nullable Context context) {
  111. super(context,Name ,null, 1);
  112. }
  113. @Override
  114. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  115. String sql="create table if not exists stu(" +
  116. "id varchar(10) not null primary key," +
  117. "`name` varchar(10) not null," +
  118. "age varchar(10) not null," +
  119. "sex varchar(10) not null)";
  120. sqLiteDatabase.execSQL(sql);
  121. }
  122. @Override
  123. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  124. }
  125. }
  126. //StuAdapter
  127. package com.example.stuapp.utils;
  128. import android.app.Application;
  129. import android.content.Context;
  130. import android.view.LayoutInflater;
  131. import android.view.View;
  132. import android.view.ViewGroup;
  133. import android.widget.BaseAdapter;
  134. import android.widget.TextView;
  135. import com.example.stuapp.R;
  136. import com.example.stuapp.pojo.Stu;
  137. import java.util.List;
  138. public class StuAdapter extends BaseAdapter {
  139. private Context context;
  140. private List<Stu> stus;
  141. public StuAdapter(Context context, List<Stu> stus) {
  142. super();
  143. this.context=context;
  144. this.stus=stus;
  145. }
  146. @Override
  147. public int getCount() {
  148. return stus.size();
  149. }
  150. @Override
  151. public Object getItem(int i) {
  152. return stus.get(i);
  153. }
  154. @Override
  155. public long getItemId(int i) {
  156. return i;
  157. }
  158. @Override
  159. public View getView(int i, View view, ViewGroup viewGroup) {
  160. View stuview = LayoutInflater.from(context).inflate(R.layout.stu_item,null);
  161. TextView[] textView=new TextView[9];
  162. Stu stu=stus.get(i);
  163. textView[0]=stuview.findViewById(R.id.t1);
  164. textView[0].setText(stu.getId());
  165. textView[1]=stuview.findViewById(R.id.t2);
  166. textView[1].setText(stu.getName());
  167. textView[2]=stuview.findViewById(R.id.t3);
  168. textView[2].setText(stu.getAge());
  169. textView[3]=stuview.findViewById(R.id.t4);
  170. textView[3].setText(stu.getSex());
  171. return stuview;
  172. }
  173. }
  174. //Main
  175. package com.example.stuapp;
  176. import androidx.appcompat.app.AlertDialog;
  177. import androidx.appcompat.app.AppCompatActivity;
  178. import android.annotation.SuppressLint;
  179. import android.content.Context;
  180. import android.content.Intent;
  181. import android.database.sqlite.SQLiteDatabase;
  182. import android.os.Bundle;
  183. import android.view.View;
  184. import android.widget.Button;
  185. import android.widget.TextView;
  186. import com.example.stuapp.utils.StuHelp;
  187. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  188. private String SqlPath;
  189. private SQLiteDatabase sqLiteDatabase;
  190. private StuHelp stuHelp;
  191. @SuppressLint("MissingInflatedId")
  192. @Override
  193. protected void onCreate(Bundle savedInstanceState) {
  194. super.onCreate(savedInstanceState);
  195. setContentView(R.layout.activity_main);
  196. Button[] buttons=new Button[10];
  197. SqlPath=getFilesDir()+"/stu.db";
  198. buttons[0]=findViewById(R.id.b0);
  199. buttons[0].setOnClickListener(this);
  200. buttons[1]=findViewById(R.id.b1);
  201. buttons[1].setOnClickListener(this);
  202. buttons[2]=findViewById(R.id.b2);
  203. buttons[2].setOnClickListener(this);
  204. buttons[3]=findViewById(R.id.b3);
  205. buttons[3].setOnClickListener(this);
  206. buttons[4]=findViewById(R.id.b4);
  207. buttons[4].setOnClickListener(this);
  208. buttons[5]=findViewById(R.id.b5);
  209. buttons[5].setOnClickListener(this);
  210. buttons[6]=findViewById(R.id.b6);
  211. buttons[6].setOnClickListener(this);
  212. buttons[7]=findViewById(R.id.b7);
  213. buttons[7].setOnClickListener(this);
  214. }
  215. @Override
  216. public void onClick(View view) {
  217. sqLiteDatabase=openOrCreateDatabase(SqlPath, Context.MODE_PRIVATE,null);
  218. AlertDialog.Builder builder=new AlertDialog.Builder(this);
  219. Intent intent=new Intent();
  220. switch (view.getId()){
  221. case R.id.b0:
  222. stuHelp=StuHelp.getStuHelp(getApplicationContext());
  223. stuHelp.onCreate(sqLiteDatabase);
  224. builder.setTitle("创建结果")
  225. .setMessage("信息表路径为"+sqLiteDatabase.getPath().toString())
  226. .setPositiveButton("确认",null)
  227. .show();
  228. break;
  229. case R.id.b1:
  230. intent.setClass(MainActivity.this,Add.class);
  231. startActivity(intent);
  232. break;
  233. case R.id.b2:
  234. intent.setClass(MainActivity.this,Delete.class);
  235. startActivity(intent);
  236. break;
  237. case R.id.b3:
  238. intent.setClass(MainActivity.this,Update.class);
  239. startActivity(intent);
  240. break;
  241. case R.id.b4:
  242. intent.setClass(MainActivity.this,Select.class);
  243. startActivity(intent);
  244. break;
  245. case R.id.b5:
  246. intent.setClass(MainActivity.this,Browse.class);
  247. startActivity(intent);
  248. break;
  249. case R.id.b6:
  250. builder.setTitle("清空表格")
  251. .setMessage("表格已经清空")
  252. .setPositiveButton("确认",null)
  253. .show();
  254. sqLiteDatabase.execSQL("delete from stu where id=id");
  255. break;
  256. case R.id.b7:
  257. builder.setTitle("关闭结果")
  258. .setMessage("数据库已经关闭")
  259. .setPositiveButton("确认",null)
  260. .show();
  261. sqLiteDatabase.close();
  262. break;
  263. }
  264. }
  265. }
  266. //Add
  267. package com.example.stuapp;
  268. import android.content.Context;
  269. import android.content.DialogInterface;
  270. import android.content.Intent;
  271. import android.database.sqlite.SQLiteDatabase;
  272. import android.os.Bundle;
  273. import android.os.PersistableBundle;
  274. import android.view.View;
  275. import android.widget.Button;
  276. import android.widget.EditText;
  277. import android.widget.RadioGroup;
  278. import androidx.annotation.Nullable;
  279. import androidx.appcompat.app.AlertDialog;
  280. import androidx.appcompat.app.AppCompatActivity;
  281. import com.example.stuapp.pojo.Stu;
  282. import com.example.stuapp.utils.StuHelp;
  283. public class Add extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
  284. private Stu stu;
  285. private String SqlPath;
  286. private SQLiteDatabase sqLiteDatabase;
  287. private StuHelp stuHelp;
  288. @Override
  289. public void onCreate(@Nullable Bundle savedInstanceState) {
  290. super.onCreate(savedInstanceState);
  291. setContentView(R.layout.add);
  292. Button add=findViewById(R.id.add);
  293. stu=new Stu();
  294. add.setOnClickListener(this);
  295. SqlPath=getFilesDir()+"/stu.db";
  296. RadioGroup sex=findViewById(R.id.sex);
  297. sex.setOnCheckedChangeListener(this);
  298. }
  299. @Override
  300. public void onClick(View view) {
  301. AlertDialog.Builder builder=new AlertDialog.Builder(this);
  302. builder.setTitle("添加结果");
  303. EditText id=findViewById(R.id.id);
  304. stu.setId(id.getText().toString());
  305. EditText name=findViewById(R.id.name);
  306. stu.setName(name.getText().toString());
  307. EditText age=findViewById(R.id.age);
  308. stu.setAge(age.getText().toString());
  309. sqLiteDatabase=openOrCreateDatabase(SqlPath, Context.MODE_PRIVATE,null);
  310. stuHelp=StuHelp.getStuHelp(getApplicationContext());
  311. long row=stuHelp.add(sqLiteDatabase,stu);
  312. if(row>0){
  313. builder.setMessage("添加成功"+" "+stu.toString());
  314. }
  315. else{
  316. builder.setMessage("添加失败"+" "+stu.toString());
  317. }
  318. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  319. @Override
  320. public void onClick(DialogInterface dialogInterface, int i) {
  321. Intent intent=new Intent();
  322. intent.setClass(Add.this,MainActivity.class);
  323. startActivity(intent);
  324. }
  325. });
  326. builder.show();
  327. }
  328. @Override
  329. public void onCheckedChanged(RadioGroup radioGroup, int i) {
  330. switch (i) {
  331. case R.id.man:
  332. stu.setSex("男");
  333. break;
  334. case R.id.woman:
  335. stu.setSex("女");
  336. break;
  337. }
  338. }
  339. }
  340. //Delete
  341. package com.example.stuapp;
  342. import android.content.Context;
  343. import android.content.DialogInterface;
  344. import android.content.Intent;
  345. import android.database.sqlite.SQLiteDatabase;
  346. import android.os.Bundle;
  347. import android.view.View;
  348. import android.widget.Button;
  349. import android.widget.EditText;
  350. import androidx.annotation.Nullable;
  351. import androidx.appcompat.app.AlertDialog;
  352. import androidx.appcompat.app.AppCompatActivity;
  353. import com.example.stuapp.pojo.Stu;
  354. import com.example.stuapp.utils.StuHelp;
  355. public class Delete extends AppCompatActivity implements View.OnClickListener {
  356. private String SqlPath;
  357. private SQLiteDatabase sqLiteDatabase;
  358. private StuHelp stuHelp;
  359. @Override
  360. public void onCreate(@Nullable Bundle savedInstanceState) {
  361. super.onCreate(savedInstanceState);
  362. setContentView(R.layout.delete);
  363. Button delete=findViewById(R.id.delete);
  364. delete.setOnClickListener(this);
  365. SqlPath=getFilesDir()+"/stu.db";
  366. }
  367. @Override
  368. public void onClick(View view) {
  369. EditText id=findViewById(R.id.id);
  370. sqLiteDatabase=openOrCreateDatabase(SqlPath, Context.MODE_PRIVATE,null);
  371. stuHelp=StuHelp.getStuHelp(getApplicationContext());
  372. long row=stuHelp.delete(sqLiteDatabase,id.getText().toString());
  373. String res;
  374. if(row>0) res="删除成功";
  375. else res="删除失败";
  376. AlertDialog.Builder builder=new AlertDialog.Builder(this);
  377. builder.setTitle("删除结果")
  378. .setMessage(res)
  379. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  380. @Override
  381. public void onClick(DialogInterface dialogInterface, int i) {
  382. Intent intent=new Intent();
  383. intent.setClass(Delete.this,MainActivity.class);
  384. startActivity(intent);
  385. }
  386. })
  387. .show();
  388. }
  389. }
  390. //Update
  391. package com.example.stuapp;
  392. import android.content.Context;
  393. import android.content.DialogInterface;
  394. import android.content.Intent;
  395. import android.database.sqlite.SQLiteDatabase;
  396. import android.os.Bundle;
  397. import android.view.View;
  398. import android.widget.Button;
  399. import android.widget.EditText;
  400. import android.widget.RadioGroup;
  401. import androidx.annotation.Nullable;
  402. import androidx.appcompat.app.AlertDialog;
  403. import androidx.appcompat.app.AppCompatActivity;
  404. import com.example.stuapp.pojo.Stu;
  405. import com.example.stuapp.utils.StuHelp;
  406. public class Update extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
  407. private Stu stu;
  408. private String SqlPath;
  409. private SQLiteDatabase sqLiteDatabase;
  410. private StuHelp stuHelp;
  411. @Override
  412. public void onCreate(@Nullable Bundle savedInstanceState) {
  413. super.onCreate(savedInstanceState);
  414. setContentView(R.layout.update);
  415. Button update=findViewById(R.id.update);
  416. update.setOnClickListener(this);
  417. stu=new Stu();
  418. SqlPath=getFilesDir()+"/stu.db";
  419. RadioGroup sex=findViewById(R.id.sex);
  420. sex.setOnCheckedChangeListener(this);
  421. }
  422. @Override
  423. public void onClick(View view) {
  424. AlertDialog.Builder builder=new AlertDialog.Builder(this);
  425. sqLiteDatabase=openOrCreateDatabase(SqlPath, Context.MODE_PRIVATE,null);
  426. stuHelp=StuHelp.getStuHelp(getApplicationContext());
  427. EditText id=findViewById(R.id.id);
  428. stu.setId(id.getText().toString());
  429. EditText name=findViewById(R.id.name);
  430. stu.setName(name.getText().toString());
  431. EditText age=findViewById(R.id.age);
  432. stu.setAge(age.getText().toString());
  433. EditText oldid=findViewById(R.id.oldid);
  434. long row=stuHelp.update(sqLiteDatabase,stu,oldid.getText().toString());
  435. String res;
  436. if(row>0) res="修改成功";
  437. else res="修改失败";
  438. builder.setTitle("删除结果")
  439. .setMessage(res)
  440. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  441. @Override
  442. public void onClick(DialogInterface dialogInterface, int i) {
  443. Intent intent=new Intent();
  444. intent.setClass(Update.this,MainActivity.class);
  445. startActivity(intent);
  446. }
  447. })
  448. .show();
  449. }
  450. @Override
  451. public void onCheckedChanged(RadioGroup radioGroup, int i) {
  452. switch (i) {
  453. case R.id.man:
  454. stu.setSex("男");
  455. break;
  456. case R.id.woman:
  457. stu.setSex("女");
  458. break;
  459. }
  460. }
  461. }
  462. //Select
  463. package com.example.stuapp;
  464. import android.content.Context;
  465. import android.database.sqlite.SQLiteDatabase;
  466. import android.os.Bundle;
  467. import android.view.View;
  468. import android.widget.Button;
  469. import android.widget.EditText;
  470. import android.widget.ListView;
  471. import androidx.annotation.Nullable;
  472. import androidx.appcompat.app.AlertDialog;
  473. import androidx.appcompat.app.AppCompatActivity;
  474. import com.example.stuapp.pojo.Stu;
  475. import com.example.stuapp.utils.StuAdapter;
  476. import com.example.stuapp.utils.StuHelp;
  477. import java.util.ArrayList;
  478. import java.util.List;
  479. public class Select extends AppCompatActivity implements View.OnClickListener {
  480. private String SqlPath;
  481. private SQLiteDatabase sqLiteDatabase;
  482. private StuHelp stuHelp;
  483. public List<Stu> stus;
  484. @Override
  485. public void onCreate(@Nullable Bundle savedInstanceState) {
  486. super.onCreate(savedInstanceState);
  487. setContentView(R.layout.select);
  488. Button select=findViewById(R.id.select);
  489. select.setOnClickListener(this);
  490. SqlPath=getFilesDir()+"/stu.db";
  491. }
  492. @Override
  493. public void onClick(View view) {
  494. EditText id=findViewById(R.id.id);
  495. sqLiteDatabase=openOrCreateDatabase(SqlPath,Context.MODE_PRIVATE,null);
  496. stuHelp=StuHelp.getStuHelp(this);
  497. stus=new ArrayList<>();
  498. stus.add(new Stu("学号","姓名","年龄","性别"));
  499. Stu stu=stuHelp.select(sqLiteDatabase,id.getText().toString());
  500. if(stu!=null)
  501. stus.add(stu);
  502. else{
  503. AlertDialog.Builder builder=new AlertDialog.Builder(this);
  504. builder.setTitle("查询失败")
  505. .setMessage("未找到相关信息")
  506. .setPositiveButton("确认",null)
  507. .show();
  508. }
  509. StuAdapter stuAdapter=new StuAdapter(this,stus);
  510. ListView listView=findViewById(R.id.list);
  511. listView.setAdapter(stuAdapter);
  512. }
  513. }
  514. //Browse
  515. package com.example.stuapp;
  516. import android.content.Context;
  517. import android.database.Cursor;
  518. import android.database.sqlite.SQLiteDatabase;
  519. import android.os.Bundle;
  520. import android.os.PersistableBundle;
  521. import android.widget.ListView;
  522. import android.widget.Spinner;
  523. import android.widget.TextView;
  524. import androidx.annotation.Nullable;
  525. import androidx.appcompat.app.AppCompatActivity;
  526. import com.example.stuapp.pojo.Stu;
  527. import com.example.stuapp.utils.StuAdapter;
  528. import com.example.stuapp.utils.StuHelp;
  529. import java.util.List;
  530. public class Browse extends AppCompatActivity {
  531. private List<Stu> stus;
  532. private SQLiteDatabase sqLiteDatabase;
  533. private StuHelp stuHelp;
  534. @Override
  535. public void onCreate(@Nullable Bundle savedInstanceState) {
  536. super.onCreate(savedInstanceState);
  537. setContentView(R.layout.browse);
  538. ListView listView=findViewById(R.id.list);
  539. sqLiteDatabase=openOrCreateDatabase(getFilesDir()+"/stu.db", Context.MODE_PRIVATE,null);
  540. stuHelp=StuHelp.getStuHelp(this);
  541. stus=stuHelp.browse(sqLiteDatabase);
  542. stus.add(0,new Stu("学号","姓名","年龄","性别"));
  543. System.out.println(stus.toString());
  544. StuAdapter stuAdapter=new StuAdapter(this,stus);
  545. listView.setAdapter(stuAdapter);
  546. System.out.println(stus.toString());
  547. }
  548. }

差点淹死在学习的海洋里

Android Studio实现数据库的所有操作的更多相关文章

  1. Android Studio学习随笔-模拟耗时操作(sleep)

    在这里我申明一点,因为我是挂着VPN去YOUTOBE看的尚学堂的高明鑫老师讲的Android基础学习视频,有些东西他没有讲,而我也没办法,只能等两个星期后学校请老师来的时候进行询问,当然我也会将一些问 ...

  2. android studio如何查看数据库文件

    android studio查看数据库文件有两种方式: 1.SQLSCOUT 优点:集成在as中,功能强大. 缺点:收费,破解麻烦. 2.Android Device Monitor 中的File E ...

  3. Android Studio 环境部署 (转载)

    Android Studio的安装和使用过程经常需要下载以来文件和Gradle版本,而Google网站在天朝的访问可谓步履维艰,没有稳定的FQ工具是非常痛苦的.何况,作为一个优秀的程序员,不能访问国外 ...

  4. Android Studio配置Git及Git文件状态说明

    Android Studio配置Git还是比较简单的,麻烦的是可能中间出现各种问题.如果你想了解或感兴趣,请往下看. 首先你得下载Git客户端,网址:http://git-scm.com/downlo ...

  5. Android Studio快速添加Gson以及GsonFormat的使用

    目录: 一.Android Studio快速添加Gson 二.Android Studio中GsonFormat的使用 三.在线JSON校验格式化工具 一.Android Studio快速添加Gson ...

  6. studio_ 优化Android Studio 启动、编译和运行速度?

    http://www.admin10000.com/document/6842.html: 作为一名 Android 程序员,选择一个好的 IDE 工具可以使开发变得非常高效,很多程序员喜欢使用 Go ...

  7. 如何优化 Android Studio 启动、编译和运行速度?

    作为一名 Android 程序员,选择一个好的 IDE 工具可以使开发变得非常高效,很多程序员喜欢使用 Google 的 Android Studio来进行开发,但使用起来有时会出现卡顿等问题.本文介 ...

  8. Android studio如何使用SVN进行版本控制?

    通过这两天对Android Studio的研究,终于搞通了Android Studio的基本操作及与SVN的相关关联操作(这样才能在公司的开发工作中使用):Google年底将会停止ADT插件的更新和支 ...

  9. 或许是介绍Android Studio使用Git最详细的文章

    欢迎访问我的个人博客转发请注明出处:http://www.wensibo.top/2017/03/12/GitOnAS/ 前言 本文较长,图片很多很多,流量党慎入 使用Git已经有一段时间了,但是之前 ...

  10. Android Studio | 详细安装教程

    Windows和Mac系统下的安装教程差不多,需要注意的是确保系统中已经安装了JDK,并且JDK版本为1.7或1.7以上版本,如果没有,请自行更新下载安装,地址如下: Java SE Develop ...

随机推荐

  1. 关于XAF中ListView慢的总结与改善

    关于XAF中ListView慢的总结与改善: 一.数据访问模式改善:ListView中DataAccessMode中的改变:默认模式是Client,这在大多数情况下,适当的使用Server,Serve ...

  2. JAVA查漏补缺 1

    JAVA查漏补缺 1 目录 JAVA查漏补缺 1 基本数据类型 数组 方法参数传递机制 基本数据类型 数据类型 关键字 取值范围 内存占用(字节数) 整型 byte -128~127 1 整型 sho ...

  3. Java基础-注释、标识符和关键字、数据类型及拓展

    注释 单行注释// 多行注释/* */ 文档注释/** */ 标识符 Java所有的组成部分都需要名字.类名.变量名及方法名都被成为标识符 关键字 数据类型 强类型语言(安全性高,java) 要求变量 ...

  4. unixbench测试CPU性能工具

    UnixBench是一个类unix系(Unix,BSD,Linux)统下的性能测试工具,一个开源工具,被广泛用与测试linux系统主机的性能.Unixbench的主要测试项目有:系统调用.读写.进程. ...

  5. Ubuntu20.04 LTS更新源

    Ubuntu 20.04 LTS Focal Fossa于2020年04月23日发布. 备份sudo cp /etc/apt/sources.list /etc/apt/sources.list.ba ...

  6. [Docker-1自顶向下学习Docker

    本文目录: 什么是DOCKER? 什么是容器? 什么是DOCKER镜像? DOCKER有什么使用场景和优势? 流程图一:从中央仓库拉取镜像并部署 流程图二:上传镜像到中央私库 结语   什么是DOCK ...

  7. 003 jmeter连接数据库及jmeter关联提取器

    1.jmeter连接数据库 测试计划-->线程组-->在线程组上右键-添加-配置元件-JDBC Connection Configuration-->在线程组上右键-添加-取样器-J ...

  8. Ginan-PEA例程下载

    输入以下命令可在Ubuntu系统中进行下载,但受到网络限制并不能有效下载或者下载很慢 python3 scripts/download_examples.py 通过阅读python脚本,可将下载网址拷 ...

  9. Linux上的I2C基础知识

    Linux上的I2C基础知识 什么是I2C I2C(Inter-Integrated Circuit,eye-squared-C),也称为 I2C 或 IIC,是一种同步.多控制器/多目标(主/从). ...

  10. 数据库常用sql

    1.创建表 create table 表名( 字段名 类型 约束, 字段名 类型 约束 ... ) 如:create table students( id int unsigned primary k ...