Android 通过SQLite数据库实现数据存储管理
0 实验环境
在android studio中进行有关代码的编写和界面效果展示。
sqlite数据库的图形化工具sqlitestudio
下载网址:sqlitestudio官网
1 界面展示
2 功能说明
(1)需实现一个应用可供用户进行数据的录入存储
(2)能实现基础crud操作,对数据进行的删、查、改等操作
(3)同时要有输入栏和结果的展示。
3 设计原理
sqliteopenhelper 是android 提供的一个抽象工具类,负责管理数据库的创建、升级工作。如果我们想创建数据库,就需要自定义一个类继承sqliteopenhelper,然后覆写其中的抽象方法。
其中dbhelper类继承了sqliteopenhelper 类。在oncreate()方法中通过执行sql 语句实现表的创建。mydao类定义操作数据库的增删改查方法,insert(),delete(),update(),select()
4 核心代码
4.1 ui设计
activity_main.xml对主界面进行设计,需要有输入栏、操作按钮、结果显示,其中结果显示使用listview组件进行展示。
部分代码:
<linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:addstatesfromchildren="true" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" android:fontfamily="@font/huawencaiyun" android:textcolor="@color/blue" android:textsize="25sp"/> <edittext android:id="@+id/et_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:singleline="true" /> </linearlayout> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:addstatesfromchildren="true" android:gravity="center" > <button android:id="@+id/bt_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> </linearlayout> <listview android:gravity="center" android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dip" > </listview>
对上述listview组件的item元素设计:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <textview android:id="@+id/tv_id" android:textsize="25sp" android:layout_width="50dp" android:layout_height="wrap_content"/> </linearlayout>
4.2 编写有关java类
(1)mainactivity类,用于初始化一些变量和注册组件:
核心代码:(这里只展示 oncreate和displayrecords两个方法)
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mydao = new mydao(this); //创建数据库访问对象 if(mydao.getrecordsnumber()==0) { //防止重复运行时重复插入记录 mydao.insertinfo("王家伟", 20); //插入记录 mydao.insertinfo("结衣", 19); //插入记录 } initview(); initevent(); displayrecords(); //显示记录 } public void displayrecords(){ //显示记录方法定义 listview = (listview)findviewbyid(r.id.listview); listdata = new arraylist<map<string,object>>(); cursor cursor = mydao.allquery(); while (cursor.movetonext()){ int id=cursor.getint(0); //获取字段值 string name=cursor.getstring(1); //int age=cursor.getint(2); @suppresslint("range") int age=cursor.getint(cursor.getcolumnindex("age"));//推荐此种方式 listitem=new hashmap<string,object>(); //必须在循环体里新建 listitem.put("_id", id); //第1参数为键名,第2参数为键值 listitem.put("name", name); listitem.put("age", age); listdata.add(listitem); //添加一条记录 } listadapter = new simpleadapter(this, listdata, r.layout.list_item, //自行创建的列表项布局 new string[]{"_id","name","age"}, new int[]{r.id.tv_id,r.id.tv_name,r.id.tv_age}); listview.setadapter(listadapter); //应用适配器 listview.setonitemclicklistener(new adapterview.onitemclicklistener() { //列表项监听 @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { map<string,object> rec= (map<string, object>) listadapter.getitem(position); //从适配器取记录 et_name.settext(rec.get("name").tostring()); //刷新文本框 et_age.settext(rec.get("age").tostring()); log.i("ly",rec.get("_id").tostring()); selid=rec.get("_id").tostring(); //供修改和删除时使用 } }); }
(2)dbhelper类是sqlite数据库打开助手类,用来创建和打开数据库,并创建数据表:
核心代码:
public class dbhelper extends sqliteopenhelper { public static final string tb_name = "friends"; //表名 //构造方法:第1参数为上下文,第2参数库库名,第3参数为游标工厂,第4参数为版本 public dbhelper(context context, string dbname, sqlitedatabase.cursorfactory factory, int version) { super(context, dbname, factory, version); //创建或打开数据库 } @override public void oncreate(sqlitedatabase db) { //当表不存在时,创建表;第一字段为自增长类型 db.execsql("create table if not exists " + tb_name + "( _id integer primary key autoincrement," + "name varchar," + "age integer"+ ")"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // 执行sql命令 db.execsql("drop table if exists " + tb_name); oncreate(db); } }
(3)mydao类中对数据库的进行增删查改操作进行了定义,使得可以在主程序文件中直接调用有关api接口:
核心代码:
//构造方法,参数为上下文对象 public mydao(context context) { //第1参数为上下文,第2参数为数据库名 dbhelper = new dbhelper(context, "test.db", null, 1); } //查询所有记录 public cursor allquery() { mydb = dbhelper.getreadabledatabase(); return mydb.rawquery("select * from friends", null); } //返回数据表记录数 public int getrecordsnumber() { mydb = dbhelper.getreadabledatabase(); cursor cursor = mydb.rawquery("select * from friends", null); return cursor.getcount(); } //插入记录 public void insertinfo(string name, int age) { mydb = dbhelper.getwritabledatabase(); contentvalues values = new contentvalues(); values.put("name", name); values.put("age", age); long rowid = mydb.insert(dbhelper.tb_name, null, values); if (rowid == -1) log.i("mydbdemo", "数据插入失败!"); else log.i("mydbdemo", "数据插入成功!" + rowid); } //删除记录 public void deleteinfo(string selid) { string where = "_id=" + selid; int i = mydb.delete(dbhelper.tb_name, where, null); if (i > 0) log.i("mydbdemo", "数据删除成功!"); else log.i("mydbdemo", "数据未删除!"); } //修改记录 public void updateinfo(string name, int age, string selid) { //方法中的第三参数用于修改选定的记录 contentvalues values = new contentvalues(); values.put("name", name); values.put("age", age); string where = "_id=" + selid; int i = mydb.update(dbhelper.tb_name, values, where, null); //上面几行代码的功能可以用下面的一行代码实现 //mydb.execsql("update friends set name = ? ,age = ? where _id = ?",new object[]{name,age,selid}); if (i > 0) log.i("mydbdemo", "数据更新成功!"); else log.i("mydbdemo", "数据未更新!"); }
(4)使用sqlite数据库的图形化工具sqlitestudio,来查看在应用中创建的数据文件里的表数据:
注意:创建的数据库位于:/data/data/包名/databases/目录中
最后使用sqlite数据库的图形化工具sqlitestudio,将保存的db文件导入sqlitestudio中,来查看在应用中创建的数据文件里的表数据
数据表的结构:
数据表的数据:
5 代码仓库
具体代码已上传至gitee代码仓库
6 总结
学习了如何创建sqlite数据库和基础crud操作,最后用图形化工具sqlitestudio查看生成的数据库中的数据表!
到此这篇关于android 通过sqlite数据库实现数据存储管理的文章就介绍到这了,更多相关sqlite数据库实现数据存储管理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
android通过jxl读excel存入sqlite3数据库
-
Android Studio 通过一个登录功能介绍SQLite数据库的使用
-
android之存储篇_SQLite数据库_让你彻底学会SQLite的使用
-
Android 数据存储笔记-数据库SQLite
-
Android Studio连接SQLite数据库的登录注册实现
-
android studio3.0以上如何通过navicat访问SQLite数据库文件
-
Android如何通过命令行操作Sqlite3数据库的方法
-
C#语言和SQLServer数据库结合使用,通过数据库中的表实现存储数据
-
java 项目中通过JDBC 实现sqlite数据库的增删改查操作
-
Android Studio 通过登录功能介绍SQLite数据库的使用流程