数据存储——SQLite数据库存储——API

一.特点

1.存储具有一定结构的数据

2.文件类型.db

3.存储目录:date/date/包名/datebases/数据库文件

4.应用卸载之后,数据同时被删除

5.数据不被其他应用直接操作

二.SQLite数据库

三.API

1.SQLiteOpenHelper   工具类

(1)是一个抽象类,需要继承并实现了抽象方法之后才能使用

(2)抽象方法:

①void   onCreate(SQLiteDatabase db)

1)创建和初始化数据库的回调方法

2)当连接数据库,未找到数据库文件时调用

②void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

1)升级数据库的回调方法

2)当连接数据库,传入的版本号高于现有的版本号时调用

(3)普通方法:

得到连接类:①getReadableDatabase()  ②getWritableDatabase()

(4)构造方法:

显示调用父类的构造方法:super(context, 数据库名, 游标(一般写null),  版本号)

(5)特点:

①是一个抽象类,需要继承并实现了抽象方法之后才能使用

②没有提供默认的构造方法

2.SQLiteDatabase  数据库连接类

(1)通常是通过工具类来获得

(2)方法

①void  execSQL(sql语句)

1)执行sql语句

2)通常是建表,修改表或删除表等语句

②close()关闭连接

③long  insert(表名,字段的缺省值,ContentValues字段和值的对应)

1)执行数据插入

2)返回值代表新插入的数据的主键值,失败返回-1

④int update(表名,ContentValues,带占位符 ? 的Where条件,String[ ]匹配?的条件值的数组)

1)执行数据修改

2)返回值代表修改数据的条数

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp3.TestActivity2"
11     android:orientation="vertical">
12 
13     <Button
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="初始化数据库"
17         android:onClick="bt1_OnClick"/>
18     <Button
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:text="升级数据库"
22         android:onClick="bt2_OnClick"/>
23     <LinearLayout
24         android:layout_width="match_parent"
25         android:layout_height="wrap_content">
26         <EditText
27             android:layout_width="0dp"
28             android:layout_height="wrap_content"
29             android:layout_weight="1"
30             android:id="@+id/et_id"
31             android:hint="id"/>
32         <EditText
33             android:layout_width="0dp"
34             android:layout_height="wrap_content"
35             android:layout_weight="1"
36             android:id="@+id/et_name"
37             android:hint="名称"/>
38     </LinearLayout>
39     <LinearLayout
40         android:layout_width="match_parent"
41         android:layout_height="wrap_content">
42         <EditText
43             android:layout_width="0dp"
44             android:layout_height="wrap_content"
45             android:layout_weight="1"
46             android:id="@+id/et_sex"
47             android:hint="性别"/>
48         <EditText
49             android:layout_width="0dp"
50             android:layout_height="wrap_content"
51             android:layout_weight="1"
52             android:id="@+id/et_age"
53             android:hint="年龄"/>
54     </LinearLayout>
55     <Button
56         android:layout_width="match_parent"
57         android:layout_height="wrap_content"
58         android:text="新增数据"
59         android:onClick="bt3_OnClick"/>
60     <Button
61         android:layout_width="match_parent"
62         android:layout_height="wrap_content"
63         android:text="查询数据"
64         android:onClick="bt4_OnClick"/>
65 
66 
67 
68 </LinearLayout>
  1 package com.hanqi.testapp3;
  2 
  3 import android.content.ContentValues;
  4 import android.database.Cursor;
  5 import android.database.sqlite.SQLiteDatabase;
  6 import android.database.sqlite.SQLiteOpenHelper;
  7 import android.os.Bundle;
  8 import android.support.v7.app.AppCompatActivity;
  9 import android.util.Log;
 10 import android.view.View;
 11 import android.widget.EditText;
 12 import android.widget.Toast;
 13 
 14 public class TestActivity2 extends AppCompatActivity {
 15 
 16     EditText et_id,et_name,et_sex,et_age;
 17 
 18     @Override
 19     protected void onCreate(Bundle savedInstanceState) {
 20         super.onCreate(savedInstanceState);
 21         setContentView(R.layout.activity_test2);
 22 
 23         et_id=(EditText)findViewById(R.id.et_id);
 24         et_name=(EditText)findViewById(R.id.et_name);
 25         et_sex=(EditText)findViewById(R.id.et_sex);
 26         et_age=(EditText)findViewById(R.id.et_age);
 27     }
 28 
 29     //初始化数据库
 30     public void bt1_OnClick(View v)
 31     {
 32         //使用工具类得到数据库对象
 33         MyDBHelper myDBHelper=new MyDBHelper("test.db",1);
 34 
 35         //得到连接
 36         SQLiteDatabase sd=myDBHelper.getWritableDatabase();
 37 
 38         Toast.makeText(TestActivity2.this, "连接数据库成功", Toast.LENGTH_SHORT).show();
 39 
 40         //关闭连接
 41         sd.close();
 42     }
 43 
 44     //升级数据库
 45     public void bt2_OnClick(View v)
 46     {
 47         //使用工具类得到数据库对象
 48         MyDBHelper myDBHelper=new MyDBHelper("test.db",2);
 49 
 50         //得到连接
 51         SQLiteDatabase sd=myDBHelper.getReadableDatabase();
 52 
 53         Toast.makeText(TestActivity2.this, "连接数据库成功", Toast.LENGTH_SHORT).show();
 54 
 55         //关闭连接
 56         sd.close();
 57     }
 58 
 59     //插入新数据
 60     public void bt3_OnClick(View v)
 61     {
 62         //1.连接数据库,得到数据库连接对象
 63 
 64         //得到连接
 65         SQLiteDatabase sd=new MyDBHelper("test.db",2).getReadableDatabase();
 66 
 67         //2.准备数据
 68         ContentValues cv=new ContentValues();
 69         cv.put("name",et_name.getText().toString());
 70         cv.put("sex",et_sex.getText().toString());
 71         cv.put("age",et_age.getText().toString());
 72 
 73         //3.调用insert(),插入数据
 74         long l=sd.insert("t_user", null, cv);
 75 
 76         Toast.makeText(TestActivity2.this, "插入数据的主键="+l, Toast.LENGTH_SHORT).show();
 77 
 78         //4.关闭连接
 79         sd.close();
 80     }
 81 
 82     //数据查询
 83     public void bt4_OnClick(View v)
 84     {
 85         //1.连接数据库,得到数据库连接对象
 86         //得到连接
 87         SQLiteDatabase sd=new MyDBHelper("test.db",2).getReadableDatabase();
 88 
 89         //2.全表全字段查询
 90         Cursor c=sd.query("t_user", null, null, null, null, null, null);
 91 
 92         //3.遍历结果集
 93         while (c.moveToNext())
 94         {
 95             //读取数据
 96             String str="_id="+c.getLong(c.getColumnIndex("_id"))+"name="+c.getString(1)
 97                     +"sex="+c.getString(2)+"age"+c.getString(3);
 98 
 99             Log.e("TAG",str);
100         }
101 
102         c.close();
103 
104         //4.关闭连接
105         sd.close();
106     }
107 
108     //实现SQLiteOpenHelper的内部类
109     class MyDBHelper extends SQLiteOpenHelper
110     {
111         //构造方法
112         public MyDBHelper(String dbname,int ver)
113         {
114             //显示调用父类的构造方法
115             //必须在第一行
116             super(TestActivity2.this,dbname,null,ver);
117         }
118 
119         //创建初始化数据库
120         @Override
121         public void onCreate(SQLiteDatabase db) {
122 
123             //1.执行创建数据库的语句
124             String sql="
" +
125                     "CREATE TABLE t_user" +
126                     " (_id  INTEGER NOT NULL," +
127                     "name  VARCHAR(20) NOT NULL," +
128                     "sex  CHAR(1),
" +
129                     "age  INTEGER,
" +
130                     "PRIMARY KEY ("_id"))";
131             db.execSQL(sql);
132 
133 
134             Log.e("TAG","表创建成功");
135 
136             //2.执行初始化数据的语句,insert语句
137             ContentValues cv=new ContentValues();
138 
139             cv.put("name", "张三");
140             cv.put("sex","男");
141             cv.put("age",20);
142 
143             //执行插入
144             long l=db.insert("t_user",null,cv);
145 
146             Log.e("TAG","初始化数据="+1);
147 
148         }
149 
150         //升级数据库
151         //触发条件:当版本号增大
152         @Override
153         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
154 
155             //修改数据
156             if (newVersion==2)
157             {
158                 ContentValues cv=new ContentValues();
159                 cv.put("name","李四");
160 
161                 String sql="update t_user set name='李四' where _id=1";
162 
163                 String[] str={"1","18"};
164 
165                 //调用db的更新方法
166                 int i=db.update("t_user",cv,"_id=? and age>?",str);
167 
168 
169                 Log.e("TAG","升级数据 数据条数="+i);
170             }
171         }
172     }
173 }

 

原文地址:https://www.cnblogs.com/cycanfly/p/5557897.html