当前位置:   article > 正文

Anroid-Sqlite 数据存储、查询_android sqlite查询

android sqlite查询
m//功能描述:
//实现数据的插入、查询和根据ID查询
//2023.6.23
//Liyumin

MainActivity:

  1. public class MainActivity extends AppCompatActivity {
  2. //将需要用到的类单独拉出来
  3. Button addBtn;
  4. Button queryallButton;
  5. Button queryoneButton;
  6. EditText nameField;
  7. EditText ageField;
  8. EditText highFiled;
  9. TextView resultField;
  10. TextView idField;
  11. DbAdapter dbAdapter;//声明
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. //实例化
  17. dbAdapter = new DbAdapter(this);
  18. dbAdapter.open(); //链接数据库
  19. //1.组件相关配置
  20. addBtn = findViewById(R.id.addbutton);
  21. nameField = findViewById(R.id.namefileld);
  22. ageField = findViewById(R.id.agefield);
  23. highFiled = findViewById(R.id.highfield);
  24. queryallButton = findViewById(R.id.queryallButton);
  25. queryoneButton = findViewById(R.id.queryone);
  26. idField = findViewById(R.id.idField);
  27. resultField = findViewById(R.id.result);
  28. //2.保存按键中断
  29. addBtn.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. //拿到数据
  33. String name = nameField.getText().toString();
  34. int age = Integer.parseInt(ageField.getText().toString());
  35. float high = Float.parseFloat(highFiled.getText().toString());
  36. Person p = new Person(name,age,high);
  37. dbAdapter.insert(p);
  38. }
  39. });
  40. //3.查询按键中断
  41. queryallButton.setOnClickListener(new View.OnClickListener() {
  42. @Override
  43. public void onClick(View v) {
  44. String result = "";
  45. Person[] ps = dbAdapter.getALLData();
  46. for(Person p:ps)
  47. {
  48. result += p.name + "," + p.age + "," + p.high + "\n";
  49. }
  50. resultField.setText(result);
  51. }
  52. });
  53. //4.根据ID查询
  54. queryoneButton.setOnClickListener(new View.OnClickListener() {
  55. @Override
  56. public void onClick(View v) {
  57. String result = "";
  58. int id = Integer.parseInt(idField.getText().toString());
  59. Person p = dbAdapter.queryById(id);
  60. result += p.name + "," + p.age + "," + p.high + "\n";
  61. resultField.setText(result);
  62. }
  63. });
  64. }
  65. }

DbAdapter:

  1. //通过这个文件与Sqlite打交道
  2. public class DbAdapter {
  3. SQLiteDatabase db;
  4. Context context;
  5. DBOpenHelper dBopenhelper;
  6. //创建一个名称为people的数据库
  7. public static final String mysql = "people";
  8. public DbAdapter(Context context) {
  9. this.context = context;
  10. }
  11. //打开数据库
  12. public void open(){
  13. dBopenhelper = new DBOpenHelper(
  14. context,mysql,null,1);
  15. //真正的实例化db
  16. db = dBopenhelper.getReadableDatabase();
  17. try {
  18. db = dBopenhelper.getWritableDatabase();
  19. } catch (SQLException e) {
  20. db = dBopenhelper.getReadableDatabase();
  21. }
  22. }
  23. //关闭数据库
  24. public void close(){
  25. if (db!= null)
  26. {
  27. db.close();
  28. db = null;
  29. }
  30. }
  31. //添加数据到表格------!!!!!!重点看看!!!!!!!!!!!!
  32. public void insert(Person p) {
  33. ContentValues cv = new ContentValues();
  34. cv.put("name",p.name);
  35. cv.put("age",p.age);
  36. cv.put("high",p.high);
  37. db.insert(mysql,null,cv);
  38. }
  39. //读取数据用到------!!!!!!重点看看!!!!!!!!!!!!
  40. public Person[] getALLData() {
  41. //cursor为指针
  42. Cursor cursor = db.query(mysql,new String[]{"_id","name","age","high"},
  43. null,null,null,null,null);
  44. return convertToPerson(cursor);
  45. }
  46. @SuppressLint("Range")
  47. //------!!!!!!重点看看!!!!!!!!!!!!
  48. //这个函数是用来处理从Sqlite中获取的数据
  49. private Person[] convertToPerson(Cursor cursor) {
  50. int rowCount = cursor.getCount();
  51. if(rowCount == 0 || !cursor.moveToFirst())
  52. {
  53. return null;
  54. }
  55. Person[] ps = new Person[rowCount];
  56. for(int i=0;i<rowCount;i++)
  57. {
  58. ps[i] = new Person();
  59. //重点掌握!!!!!!!!!!!!!!!!!!!!!!!!!!!
  60. ps[i].name = cursor.getString(cursor.getColumnIndex("name"));
  61. ps[i].age = cursor.getInt(cursor.getColumnIndex("age"));
  62. ps[i].high = cursor.getFloat(cursor.getColumnIndex("high"));
  63. cursor.moveToNext();
  64. }
  65. return ps;
  66. }
  67. public Person queryById(int id) {
  68. //重点掌握!!!!!!!!!!!!!!!!!!!!!!!!!!!
  69. Cursor cursor = db.query(mysql,new String[]{"_id","name","age","high"},
  70. "_id="+id,null,null,null,null);
  71. Person[] ps = convertToPerson(cursor);
  72. if(ps != null)
  73. {
  74. return ps[0];
  75. }
  76. return null;
  77. }
  78. private class DBOpenHelper extends SQLiteOpenHelper {
  79. //构造方法
  80. public DBOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
  81. super(context, name, factory, version);
  82. }
  83. private static final String DB_CREATE =
  84. "create table "+ mysql +"("+
  85. "_id integer primary key autoincrement,"+
  86. "name text,"+
  87. "age integer,"+
  88. "high float)";
  89. //重要
  90. @Override
  91. public void onCreate(SQLiteDatabase db) {
  92. db.execSQL(DB_CREATE);//执行SQL代码
  93. }
  94. @Override
  95. public void onUpgrade(
  96. SQLiteDatabase sqLiteDatabase, int i, int i1) {
  97. }
  98. }
  99. }

Person.class

  1. //描述数据表的结构
  2. public class Person {
  3. String name;
  4. int age;
  5. float high;
  6. public Person() {
  7. }
  8. //增加一个构造方法 -- 本质就是用来创建实例时进行赋值
  9. public Person(String name, int age, float high) {
  10. this.name = name;
  11. this.age = age;
  12. this.high = high;
  13. }
  14. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/744237
推荐阅读
相关标签
  

闽ICP备14008679号