首页 > 数据库 >Android-实现增加数据进入数据库

Android-实现增加数据进入数据库

时间:2023-02-25 18:33:33浏览次数:38  
标签:Student 数据库 public KEY student import Android 数据 android

继续写简单增删改查,终于可以实现插入数据进入数据库了

主要代码差不多了,现在主要是需要解决一个显示数据的问题,这个问题之后接着写。

初步效果

 

 然后我们用可视化应用打开curd.db , 可以看见数据插进去了哈

部分代码

Student.java

 1 package com.example.myapplication;
 2 
 3 public class Student {
 4 
 5     public static final String TABLE= "Student1";
 6 
 7     public static final String KEY_KEYS = "keys";
 8     public static final String KEY_NAME="name";
 9     public static final String KEY_ID = "id";
10     public static final String KEY_GRADE = "grade";
11 
12     public int student_keys;
13     public String student_name;
14     public String student_id;
15     public String student_grade;
16 
17 }

MainActivty.java

 1 package com.example.myapplication;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 import androidx.recyclerview.widget.ListAdapter;
 5 
 6 import android.app.ListActivity;
 7 import android.content.Intent;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.widget.AdapterView;
11 import android.widget.Button;
12 import android.widget.ListView;
13 import android.widget.SimpleAdapter;
14 import android.widget.TextView;
15 import android.widget.Toast;
16 
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 
20 public class MainActivity extends ListActivity implements View.OnClickListener {
21 
22     private Button btn_add,btn_get;
23     private TextView student_keys;
24 
25     public void onCreate(Bundle saveInstanceState) {
26         super.onCreate(saveInstanceState);
27         setContentView(R.layout.activity_main);
28 
29         btn_add = (Button) findViewById(R.id.btn_add);
30         btn_add.setOnClickListener(this);
31 
32         btn_get = (Button) findViewById(R.id.btn_get);
33         btn_get.setOnClickListener(this);
34     }
35     @Override
36     public void onClick(View v) {
37         if(v == findViewById(R.id.btn_add)){
38 
39             Intent intent = new Intent(this,StudentDetail.class);
40             intent.putExtra("student_keys",0);
41             startActivity(intent);
42 
43         }
44     }
45 }

DBHelper.java

 1 package com.example.myapplication;
 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 DBHelper extends SQLiteOpenHelper {
10 
11     private static final int DATABASE_VERSION=4;
12 
13     private static final String DATABASE_NAME="crud.db";
14 
15     public DBHelper(Context context) {
16         super(context,DATABASE_NAME,null,DATABASE_VERSION);
17     }
18 
19     @Override
20     public void onCreate(SQLiteDatabase db) {
21         String CREATE_TABLE_STUDENT="CREATE TABLE "+ Student.TABLE+"("
22                 +Student.KEY_KEYS+" INTEGER PRIMARY KEY AUTOINCREMENT ,"
23                 +Student.KEY_NAME+" TEXT ,"
24                 +Student.KEY_ID+" TEXT ,"
25                 +Student.KEY_GRADE+" TEXT)";
26         db.execSQL(CREATE_TABLE_STUDENT);
27 
28     }
29 
30     @Override
31     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
32 
33         db.execSQL("DROP TABLE IF EXISTS "+ Student.TABLE);
34 
35         onCreate(db);
36     }
37 }
StudentDetail.java
 1 package com.example.myapplication;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.EditText;
10 import android.widget.Toast;
11 import android.widget.Toolbar;
12 
13 import androidx.appcompat.app.ActionBar;
14 
15 
16 public class StudentDetail extends Activity implements View.OnClickListener {
17 
18     private Button btn_save,btn_delete;
19     private Button btn_close;
20 
21     private EditText et_name;
22     private EditText et_id;
23     private EditText et_grade;
24     private int _student_keys = 0;
25 
26     protected void onCreate(Bundle saveInstanceState) {
27         super.onCreate(saveInstanceState);
28         setContentView(R.layout.activity_student);
29 
30         btn_save = (Button) findViewById(R.id.btn_save);
31         btn_delete = (Button) findViewById(R.id.btn_delete);
32         btn_close = (Button) findViewById(R.id.btn_close);
33 
34         et_name = (EditText) findViewById(R.id.et_name);
35         et_id = (EditText) findViewById(R.id.et_id);
36         et_grade =(EditText) findViewById(R.id.et_grade);
37 
38         btn_save.setOnClickListener(this);
39         btn_delete.setOnClickListener(this);
40         btn_close.setOnClickListener(this);
41 
42         _student_keys = 0;
43         Intent intent = getIntent();
44         _student_keys = intent.getIntExtra("student_keys",0);
45         StudentRepo repo = new StudentRepo(this);
46         Student student = new Student();
47         student = repo.getStudentByKeys(_student_keys);
48 
49         et_name.setText(student.student_name);
50         et_id.setText(student.student_id);
51         et_grade.setText(student.student_grade);
52 
53     }
54 
55     public void onClick(View v){
56         if(v== findViewById(R.id.btn_save)){
57             StudentRepo repo = new StudentRepo(this);
58             Student student = new Student();
59             student.student_name = et_name.getText().toString();
60             student.student_id = et_name.getText().toString();
61             student.student_grade = et_grade.getText().toString();
62             student.student_keys = _student_keys;
63 
64             if(_student_keys==0){
65                 _student_keys= repo.insert(student);
66                 Toast.makeText(this,"插入成功",Toast.LENGTH_SHORT).show();
67             }70 71         }   else if (v == findViewById(R.id.btn_close)) {
72             finish();
73         }
74     }
75 
76 }
StudentRepo.java
  1 package com.example.myapplication;
  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 
  9 import java.util.ArrayList;
 10 import java.util.HashMap;
 11 
 12 public class StudentRepo {
 13 
 14     private DBHelper dbHelper;
 15 
 16     public StudentRepo(Context context){
 17         dbHelper = new DBHelper(context);
 18     }
 19 
 20     public int insert(Student student){
 21 
 22         SQLiteDatabase db = dbHelper.getWritableDatabase();
 23         ContentValues values = new ContentValues();
 24 
 25         values.put(Student.KEY_NAME,student.student_name);
 26         values.put(Student.KEY_ID,student.student_id);
 27         values.put(Student.KEY_GRADE,student.student_grade);
 28 
 29         long student_keys = db.insert(Student.TABLE,null,values);
 30         db.close();
 31         return (int)student_keys;
 32     }
 33     public void delete(int student_keys){
 34 
 35         SQLiteDatabase db = dbHelper.getWritableDatabase();
 36         db.delete(Student.TABLE,Student.KEY_KEYS+"=?",new String[]{String.valueOf(student_keys)});
 37         db.close();
 38     }
 39     public void update(Student student){
 40         SQLiteDatabase db = dbHelper.getWritableDatabase();
 41         ContentValues values = new ContentValues();
 42 
 43         values.put(Student.KEY_NAME,student.student_name);
 44         values.put(Student.KEY_ID,student.student_id);
 45         values.put(Student.KEY_GRADE,student.student_grade);
 46 
 47         db.update(Student.TABLE,values,Student.KEY_KEYS+"=?",new String[]{String.valueOf(student.student_keys)});
 48         db.close();
 49     }
 50     @SuppressLint("Range")
 51     public ArrayList<HashMap<String,String>> getStudentList(){
 52         SQLiteDatabase db = dbHelper.getReadableDatabase();
 53         String selectQuery = "SELECT "+
 54                 Student.KEY_KEYS+","+
 55                 Student.KEY_NAME+","+
 56                 Student.KEY_ID+","+
 57                 Student.KEY_GRADE+" FROM "+Student.TABLE;
 58         ArrayList<HashMap<String,String>> studentList = new ArrayList<HashMap<String,String>>();
 59         Cursor cursor = db.rawQuery(selectQuery,null);
 60 
 61         if (cursor.moveToFirst()){
 62             do{
 63                 HashMap<String,String> student = new HashMap<String,String>();
 64                 student.put("name",cursor.getString(cursor.getColumnIndex(Student.KEY_NAME)));
 65                 student.put("id",cursor.getString(cursor.getColumnIndex(Student.KEY_ID)));
 66                 student.put("grade",cursor.getString(cursor.getColumnIndex(Student.KEY_GRADE)));
 67                 studentList.add(student);
 68             }while (cursor.moveToNext());
 69         }
 70         cursor.close();
 71         db.close();
 72         return studentList;
 73     }
 74     @SuppressLint("Range")
 75     public Student getStudentByKeys(int keys){
 76         SQLiteDatabase db = dbHelper.getReadableDatabase();
 77         String selectQuery = "SELECT "+
 78                 Student.KEY_KEYS +","+
 79                 Student.KEY_NAME +","+
 80                 Student.KEY_ID + ","+
 81                 Student.KEY_GRADE+
 82                 " FROM " + Student.TABLE
 83                 +" WHERE " +
 84                 Student.KEY_KEYS + "=?";
 85         int count = 0;
 86         Student student = new Student();
 87         Cursor cursor = db.rawQuery(selectQuery,new String[]{String.valueOf(keys)});
 88         if (cursor.moveToFirst()){
 89             do {
 90                 student.student_keys = cursor.getInt(cursor.getColumnIndex(Student.KEY_KEYS));
 91                 student.student_name = cursor.getString(cursor.getColumnIndex(Student.KEY_NAME));
 92                 student.student_id = cursor.getString(cursor.getColumnIndex(Student.KEY_ID));
 93                 student.student_grade = cursor.getString(cursor.getColumnIndex(Student.KEY_GRADE));
 94             }while (cursor.moveToNext());
 95         }
 96         cursor.close();
 97         db.close();
 98         return student;
 99     }
100 }

 

标签:Student,数据库,public,KEY,student,import,Android,数据,android
From: https://www.cnblogs.com/rsy-bxf150/p/17154986.html

相关文章

  • BIP change数据改变前的事件
    viewModel.get("certMaterialClass_name").on("beforeValueChange",function(data){if(!!data.oldValue){varret......
  • 数据结构(借鉴408)-图
    数据结构图及其应用存储及基本操作邻接矩阵法邻接表法遍历深度优先搜索(DFS)遍历广度优先搜索(BFS)遍历最小生成树边稠密:普里姆(Prim)算法边稀疏:克鲁斯卡尔(......
  • 路由的params参数 组件之间通过路由传递数据
    介绍:组件之间可以通过路由query参数传递数据需求:  路由配置:importVuefrom"vue";importRouterfrom"vue-router";importHomefrom"./views/Home.vue";i......
  • 案例_分析&环境搭建与案例_实现_查询数据库
    案例_分析&环境搭建案例需求:1.提供index.html页面,页面中有一个省份下拉列表 2.当页面加载......
  • Android中drawable和mipmap到底有什么区别
    欢迎通过我的个人博客来查看此文章老项目代码中发现有的图片放到了drawable中,有的图片放到了mipmap中,开发时秉承哪个目录下文件多放哪里的原则,偶尔有疑惑搜一搜文......
  • 数据库索引失效的场景
    1.建表createtablet1(aintPRIMARYkey,bint,cint,dint,evarchar(20))ENGINE=INNODBinsertintot1value(4,3,1,1,'d');inser......
  • 使用sys用户imp导入数据遇到字符集转换后列长度不够报错
    使用sys用户imp数据遇到字符集转换时列长度不够的报错,ORA-12899:valuetoolargeforcolumn"SYS"."M_ASH"."ACTION"(actual:40,maximum:32)。从报错可以看到具体的表......
  • 10.2.0.4 RAC hang住的分析-从ash基表数据查出RLM$EVTCLEANUP job异常
    记录一下之前的一次故障排查过程。用户反映8点左右业务部分反映应用卡,8点多以后应用无法进行,用户DBA紧急重启了数据库实例,之后数据库系统恢复正常。了解到这是一套AIX+两......
  • java-树形结构数据
    在我们实际开发中会接触到树形结构,根节点子节点,然后添加数据构成了我们的树形结构,在Java后台利用递归思路进行构建树形结构数据,返回给前端,能以下拉菜单等形式进行展示......
  • android stdio中marqueeRepeatLimit无法循环播放
    首先,这个控件需要得到焦点,因此来实现循环播放,因此我们要 等我们再次启动虚拟机时,我们能够发现再次点击文字,就能够使控件文字循环播放。 但是这样点击文字循环会让整......