21.Android之SQLite数据库学习

Google为Andriod的较大的数据处理提供了SQLite,他在数据存储、管理、维护等各方面都相当出色,功能也非常的强大。SQLite具备下列特点:

1.轻量级

使用 SQLite 只需要带一个动态库,就可以享受它的全部功能,而且那个动态库的尺寸想当小。

2.独立性

SQLite 数据库的核心引擎不需要依赖第三方软件,也不需要所谓的“安装”。

3.隔离性

SQLite 数据库中所有的信息(比如表、视图、触发器等)都包含在一个文件夹内,方便管理和维护。

4.跨平台

SQLite 目前支持大部分操作系统,不至电脑操作系统更在众多的手机系统也是能够运行,比如:Android。

5.多语言接口

SQLite 数据库支持多语言编程接口。

6.安全性

SQLite 数据库通过数据库级上的独占性和共享锁来实现独立事务处理。这意味着多个进程可以在同一时间从同一数据库读取数据,但只能有一个可以写入数据。

接下来我们用代码实现下:

XML文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <Button
 8         android:id="@+id/create_database"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="创建数据库" />
12 
13     <Button
14         android:id="@+id/update_database"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:text="更改数据库" />
18 
19     <Button
20         android:id="@+id/insert"
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:text="插入数据" />
24 
25     <Button
26         android:id="@+id/query"
27         android:layout_width="match_parent"
28         android:layout_height="wrap_content"
29         android:text="查询数据" />
30 
31     <Button
32         android:id="@+id/update"
33         android:layout_width="match_parent"
34         android:layout_height="wrap_content"
35         android:text="修改数据" />
36 
37     <Button
38         android:id="@+id/delete"
39         android:layout_width="match_parent"
40         android:layout_height="wrap_content"
41         android:text="删除数据" />
42 
43 </LinearLayout>

如图:

再修改主代码,增加数据库辅助操作类:

 1 package com.example.sqlitedemo;
 2 
 3 import android.content.Context;
 4 import android.database.sqlite.SQLiteDatabase;
 5 import android.database.sqlite.SQLiteOpenHelper;
 6 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 7 import android.util.Log;
 8 
 9 public class CustomSqlite extends SQLiteOpenHelper {
10 
11     private static final String TAG = "TestSQLite";
12     private static final int VERSION = 1;
13 
14     // 必须要有构造函数
15     public CustomSqlite(Context context, String name, CursorFactory factory,
16             int version) {
17         super(context, name, factory, version);
18     }
19 
20     // 当第一次创建数据库的时候,调用该方法
21     public void onCreate(SQLiteDatabase db) {
22         String sql = "create table user(id int, name varchar(20))";
23         // 输出创建数据库的日志信息
24         Log.i(TAG, "create Database------------->");
25         // execSQL函数用于执行SQL语句
26         db.execSQL(sql);
27     }
28 
29     // 当更新数据库的时候执行该方法
30     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
31         // 输出更新数据库的日志信息
32         Log.i(TAG, "update Database------------->");
33     }
34 
35 }
  1 package com.example.sqlitedemo;
  2 
  3 import android.app.Activity;
  4 import android.content.ContentValues;
  5 import android.database.Cursor;
  6 import android.database.sqlite.SQLiteDatabase;
  7 import android.os.Bundle;
  8 import android.view.View;
  9 import android.view.View.OnClickListener;
 10 import android.widget.Button;
 11 
 12 public class MainActivity extends Activity {
 13 
 14     private Button btn_createdb = null;
 15     private Button btn_updatedb = null;
 16     private Button btn_insert = null;
 17     private Button btn_query = null;
 18     private Button btn_update = null;
 19     private Button btn_delete = null;
 20 
 21     @Override
 22     protected void onCreate(Bundle savedInstanceState) {
 23         super.onCreate(savedInstanceState);
 24         setContentView(R.layout.activity_main);
 25 
 26         btn_createdb = (Button) findViewById(R.id.create_database);
 27         btn_updatedb = (Button) findViewById(R.id.update_database);
 28         btn_insert = (Button) findViewById(R.id.insert);
 29         btn_query = (Button) findViewById(R.id.query);
 30         btn_update = (Button) findViewById(R.id.update);
 31         btn_delete = (Button) findViewById(R.id.delete);
 32 
 33         btn_createdb.setOnClickListener(new MyClickListener());
 34         btn_updatedb.setOnClickListener(new MyClickListener());
 35         btn_insert.setOnClickListener(new MyClickListener());
 36         btn_query.setOnClickListener(new MyClickListener());
 37         btn_update.setOnClickListener(new MyClickListener());
 38         btn_delete.setOnClickListener(new MyClickListener());
 39 
 40     }
 41 
 42     class MyClickListener implements OnClickListener {
 43 
 44         @Override
 45         public void onClick(View v) {
 46             switch (v.getId()) {
 47 
 48             case R.id.create_database:
 49                 createdb(); // 创建数据库
 50                 break;
 51 
 52             case R.id.update_database:
 53                 updatedb(); // 更改数据库
 54                 break;
 55 
 56             case R.id.insert:
 57                 insert(); // 插入数据
 58                 break;
 59 
 60             case R.id.query:
 61                 query(); // 查询数据
 62                 break;
 63 
 64             case R.id.update:
 65                 update(); // 修改数据
 66                 break;
 67 
 68             case R.id.delete:
 69                 delete(); // 删除数据
 70                 break;
 71 
 72             default:
 73                 break;
 74             }
 75         }
 76     }
 77 
 78     public void createdb() {
 79         CustomSqlite helper = new CustomSqlite(MainActivity.this, "test_db",
 80                 null, 1);
 81         SQLiteDatabase db = helper.getReadableDatabase();     
 82     }
 83 
 84     public void updatedb() {
 85         CustomSqlite helper = new CustomSqlite(MainActivity.this, "test_db",
 86                 null, 2);
 87         SQLiteDatabase db = helper.getReadableDatabase();
 88 
 89     }
 90 
 91     public void insert() {
 92         CustomSqlite helper = new CustomSqlite(MainActivity.this, "test_db",
 93                 null, 1);
 94         SQLiteDatabase db = helper.getReadableDatabase();
 95         // 生成ContentValues对象 //key:列名,value:想插入的值
 96         ContentValues value = new ContentValues();
 97         // 往ContentValues对象存放数据,键-值对模式
 98         value.put("id", 2015);
 99         value.put("name", "John");
100         db.insert("user", null, value);
101         db.close();
102 
103     }
104 
105     public void query() {
106         CustomSqlite helper = new CustomSqlite(MainActivity.this, "test_db",
107                 null, 1);
108         // 得到一个可写的数据库
109         SQLiteDatabase db = helper.getReadableDatabase();
110         // 参数1:表名
111         // 参数2:要想显示的列
112         // 参数3:where子句
113         // 参数4:where子句对应的条件值
114         // 参数5:分组方式
115         // 参数6:having条件
116         // 参数7:排序方式
117         Cursor cursor = db.query("user", new String[] { "id", "name" }, "id=?",
118                 new String[] { "2015" }, null, null, null);
119         while (cursor.moveToNext()) {
120             String name = cursor.getString(cursor.getColumnIndex("name"));
121             System.out.println("query---" + name);
122         }
123 
124     }
125 
126     public void update() {
127 
128         CustomSqlite helper = new CustomSqlite(MainActivity.this, "test_db",
129                 null, 1);
130         // 得到一个可写的数据库
131         SQLiteDatabase db = helper.getReadableDatabase();
132         ContentValues value = new ContentValues();
133         value.put("name", "update_John");
134         db.update("user", value, "id=?", new String[] { "2015" });
135     }
136 
137     public void delete() {
138         CustomSqlite helper = new CustomSqlite(MainActivity.this, "test_db",
139                 null, 1);
140         // 得到一个可写的数据库
141         SQLiteDatabase db = helper.getReadableDatabase();
142         // 删除条件
143         String whereClauses = "id=?";
144         // 删除条件参数
145         String[] whereArgs = { String.valueOf(2) };
146         // 调用delete方法,删除数据
147         db.delete("user", whereClauses, whereArgs);
148     }
149 
150 }
原文地址:https://www.cnblogs.com/benchao/p/5091299.html