继续写简单增删改查,终于可以实现插入数据进入数据库了
主要代码差不多了,现在主要是需要解决一个显示数据的问题,这个问题之后接着写。
初步效果
然后我们用可视化应用打开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