Sqlite数据库概述

image.png

Android中访问Sqlite数据库的类

image.png

image.png

image.png

image.png

实例

image.png

SQLiteOpenHelper的封装

image.png

关键代码

image.png

SQLiteOpenHelper的使用

image.png

activity_main.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:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <Button
  10. android:id="@+id/btn1"
  11. android:text="创建数据库"
  12. android:textSize="20sp"
  13. android:onClick="btnCreate"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content" />
  16. <Button
  17. android:id="@+id/btn2"
  18. android:text="更新数据库"
  19. android:textSize="20sp"
  20. android:onClick="btnUpdate"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content" />
  23. <Button
  24. android:id="@+id/btn3"
  25. android:text="插入数据"
  26. android:textSize="20sp"
  27. android:onClick="btnInsertData"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content" />
  30. <Button
  31. android:id="@+id/btn4"
  32. android:text="更新数据"
  33. android:textSize="20sp"
  34. android:onClick="btnUpdateData"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content" />
  37. <Button
  38. android:id="@+id/btn5"
  39. android:text="查询数据"
  40. android:textSize="20sp"
  41. android:onClick="btnSelectData"
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content" />
  44. <Button
  45. android:id="@+id/btn6"
  46. android:text="删除数据"
  47. android:textSize="20sp"
  48. android:onClick="btnDeleteData"
  49. android:layout_width="match_parent"
  50. android:layout_height="wrap_content" />
  51. </LinearLayout>

MainActivity.java

  1. package com.bluelesson.sqlitedemo;
  2. import android.content.ContentValues;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.util.Log;
  8. import android.view.View;
  9. public class MainActivity extends AppCompatActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. }
  15. public void btnCreate(View view) {
  16. //创建一个DatabaseHelper对象
  17. DBHelper dbHelper1 = new DBHelper(MainActivity.this, "test_db");
  18. //取得一个只读的数据库对象
  19. SQLiteDatabase db1 = dbHelper1.getReadableDatabase();
  20. db1.close();
  21. }
  22. public void btnUpdate(View view) {
  23. DBHelper dbHelper2 = new DBHelper(MainActivity.this, "test_db", 2);
  24. //更新版本
  25. SQLiteDatabase db2 = dbHelper2.getReadableDatabase();
  26. db2.close();
  27. }
  28. public void btnInsertData(View view) {
  29. // 1. 准备一个数据对象
  30. // 1.1 创建存放数据的ContentValues对象
  31. ContentValues values = new ContentValues();
  32. // 1.2 向ContentValues中存放数据
  33. values.put("id", 1);
  34. values.put("name","zhangsan");
  35. // 2. 插入数据
  36. // 2.1 获取数据库对象
  37. DBHelper dbHelper3 = new DBHelper(MainActivity.this, "test_db");
  38. SQLiteDatabase db3 = dbHelper3.getWritableDatabase();
  39. //2.2 执行插入命令
  40. db3.insert("user", null, values);
  41. db3.close();
  42. }
  43. public void btnUpdateData(View view) {
  44. // 1. 准备要更新的数据对象
  45. ContentValues values2 = new ContentValues();
  46. values2.put("name", "xiaosan");
  47. // 2. 获取数据库对象
  48. DBHelper dbHelper4 = new DBHelper(MainActivity.this, "test_db");
  49. SQLiteDatabase db4 = dbHelper4.getWritableDatabase();
  50. // 3. 执行更新方法,更新
  51. db4.update("user", values2, "id=?", new String[]{"1"});
  52. db4.close();
  53. }
  54. public void btnSelectData(View view) {
  55. // 1. 获取数据库对象
  56. DBHelper dbHelper5 = new DBHelper(MainActivity.this, "test_db");
  57. SQLiteDatabase db5 = dbHelper5.getReadableDatabase();
  58. // 2. 查询数据,返回游标对象,遍历
  59. // 2.1 查询并接收游标对象
  60. Cursor cursor = db5.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null, null);
  61. // 2.2 利用游标遍历所有数据对象
  62. while(cursor.moveToNext()){
  63. String name = cursor.getString(cursor.getColumnIndex("name"));
  64. //日志打印输出
  65. Log.i("MainActivity", "query-->" + name);
  66. }
  67. db5.close();
  68. }
  69. public void btnDeleteData(View view) {
  70. DBHelper dbHelper6 = new DBHelper(MainActivity.this,"test_db");
  71. SQLiteDatabase db6 = dbHelper6.getWritableDatabase();
  72. // 删除数据,指定表,条件
  73. db6.delete("user", "id=?", new String[]{"1"});
  74. db6.close();
  75. }
  76. }

DBHelper.java

  1. package com.bluelesson.sqlitedemo;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. import android.support.annotation.Nullable;
  6. import android.util.Log;
  7. public class DBHelper extends SQLiteOpenHelper {
  8. public DBHelper(@Nullable Context context, @Nullable String name) {
  9. super(context, name, null, 1);
  10. }
  11. //@注解、限定的意思,@Nullable表示可以为空
  12. public DBHelper(@Nullable Context context, @Nullable String name,
  13. int version) {
  14. super(context, name, null, version);
  15. }
  16. @Override
  17. public void onCreate(SQLiteDatabase db) {
  18. Log.i("DBHelper", "create a Database");
  19. //创建数据库sql语句
  20. String sql = "create table user(id int,name varchar(20))";
  21. // 执行创建数据库操作
  22. db.execSQL(sql);
  23. }
  24. @Override
  25. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  26. //创建成功,日志输出提示
  27. Log.i("DBHelper", "update a Database");
  28. }
  29. }