💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
SQLite数据库的创建和操作 Email:chentravelling@163.com 前天学习了一下界面布局和activity,昨天抽了一会时间捣腾了一下SQLite,总体感觉Android的数据库服务还算方便,这都是两个类的功劳:SQLiteOpenHelper和SQLiteDatabase。关于这两个类,后续再讲。 ### 一、环境 IDE:Android Studio JDK:1.8 系统:win 7 64位 ### 二、设计 我拿了一个简单的例子:people表字段如下 <table border="1" width="200" cellspacing="1" cellpadding="1" align="center"><tbody><tr><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">字段</span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">类型</span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">主键</span></span></td></tr><tr><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">id</span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">TEXT</span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">KEY</span></span></td></tr><tr><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">name</span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">TEXT</span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px"><br/></span></span></td></tr><tr><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">workplace</span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">TEXT</span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px"><br/></span></span></td></tr><tr><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">phone</span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">TEXT</span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px"><br/></span></span></td></tr><tr><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">gender<span style="white-space:pre"/></span></span></td><td><span style="font-family:'Microsoft YaHei'"><span style="font-size:14px">INTEGER</span></span></td><td><span style="font-family:Microsoft YaHei; font-size:18px"><br/></span></td></tr></tbody></table> 2.1)首先我写了一个people类:peopleBean。为什么要新建一个类呢?首先是对需要传递和保存的信息进行封装,这样个人觉得更加符合面向对象的思想,也使得代码更规范,可读性更高,安全性更高。 ~~~ /** * Created by chen on 2015/12/16. */ public class peopleBean { private String id;//主键 private String name; private String workplace; private String phone; private int gender; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getWorkplace() { return workplace; } public void setWorkplace(String workplace) { this.workplace = workplace; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public int getGender() { return gender; } public void setGender(int gender) { this.gender = gender; } } </span> ~~~ 2.2)新建一个对数据库进行操作的类:AddressDAO,该类继承自SQLiteOpenHelper类,关于SQLiteOpenHelper类的说明将在另一个章节中讨论。 ~~~ <pre name="code" class="java"><span style="font-family:Microsoft YaHei;font-size:18px;"> import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; /** * Created by chen on 2015/12/16. */ public class AddressDAO extends SQLiteOpenHelper{ private SQLiteDatabase db; public AddressDAO(Context context) { super(context,"address_db",null,1); } //创建数据库 @Override public void onCreate(SQLiteDatabase db) { Log.e("onCreate", "create"); String sql = "CREATE TABLE IF NOT EXISTS people(id integer primary key autoincrement,name text,phone text,workplace text,gender integer);"; db.execSQL(sql); } //当数据库升级的时候,Android系统会自动调用onUpgrade方法 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql = "DROP TABLE IF EXISTS address_db"; db.execSQL(sql); onCreate(db); } //关闭数据库服务 public void closeDB(SQLiteDatabase db) { if(db!=null) { db.close(); } } //插入数据 public void add(peopleBean people) { /* String name = people.getName(); String phone = people.getPhone(); String workplace = people.getWorkplace(); String gender = people.getGender()==0?"男":"女";*/ String sql = "insert into people values(null,'"+people.getName()+"','"+people.getPhone()+"','"+people.getWorkplace()+"','"+people.getGender()+"')"; //String sql = "insert into people values(null,'张亚茹','13716762131','北京','0');"; //获取SQLiteDatabase对象实例 db = this.getWritableDatabase(); db.execSQL(sql); } //查询数据:返回的是一个Cursor对象 public Cursor query() { String sql = "select * from people"; //获取SQLiteDatabase对象实例 db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(sql, null); return cursor; } } </span> ~~~ 其中关键的几个地方: (1)重写**onCreate()**函数,功能:新建数据库,库名:address_db (2)重写**onUpdate()**函数,数据库升级时,Android系统会自动调用该方法 (3)查询的时候,调用的是**rawQuery()**方法,返回的是**Cursor对象**,该对象是一个数据集合,关于Cursor的操作,后续再讲。 其他的删除和查询暂时还没写。 当然还可以通过调用SQLiteDatabase类中的insert()、delete()、update()、rawQuery()进行数据的增删改查,但是个人喜欢通过执行sql语句来做,比较方便。 ### 三、关于SQLite可视化 系统中的数据库在哪里呢?见下图: 打开android device monitor ![](https://box.kancloud.cn/2016-03-18_56eb6a3476878.jpg) ![](https://box.kancloud.cn/2016-03-18_56eb6a349c204.jpg) 将数据库表导入和导出: ![](https://box.kancloud.cn/2016-03-18_56eb6a34ae300.jpg) 然后就可以利用SQLite可视化工具查看了:SQLite Expert