使用GreenDao对数据库进行操作
Posted 和平world
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用GreenDao对数据库进行操作相关的知识,希望对你有一定的参考价值。
在gradle中进行如下配置
buildscript repositories mavenCentral() dependencies classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' apply plugin: 'org.greenrobot.greendao' dependencies compile 'org.greenrobot:greendao:3.0.1'
greenDAO特性
对象/关系映射(ORM)
greenDAO的本质是提供一个面向对象的接口数据存储在关系数据库SQLite。 数据模型定义,greenDAO将创建Java数据对象(实体)和DAOs( 数据访问对象 )。 这将节省你很多无聊的代码,只是来回移动数据。 除此之外,greenDAO报价 一些先进的ORM特性 像一个会话缓存,立即加载和活跃的实体。
性能
orm我们知道,greenDAO是最快的。 关于性能greenDAO不做任何妥协。 数据库用于存储大量的数据,因此速度很重要。 使用greenDAO,大多数实体可以插入、更新和加载速度 每秒几千实体 。
我们有信心在greenDAO性能和邀请你来比较greenDAO orm。 我们 开源的基准 完全透明。 下面的图表比较了3个最流行的android greenDAO ORM解决方案,OrmLite,ActiveAndroid(根据基于GitHub的流行明星和Appbrain的统计数据)。 greenDAO插入和更新实体快2倍左右,并加载实体比ORMLite快4倍左右。 典型的应用程序加载速度是最相关的。
除了greenDAO的高性能核心功能,如一个会话缓存和智能立即加载技术给额外的性能提升。
加密支持
您可以使用与标准greenDAO SQLite,嵌入在Android,或与SQLCipher您可以使用它。 有看吗 数据库加密文档获取详细信息。
很小的library
greenDAO核心库的规模小于100 k,所以添加greenDAO不伤害你的APK大小。
活跃的实体
如果你愿意的话,实体可以“活跃”:积极解决实体关系透明(你只是调用getter),更新,删除和更新方法方便访问持久化功能。
Protocol buffers支持
greenDAO让你坚持 协议缓冲区(protobuf) 直接对象到数据库中。 如果你跟你的服务器通过protobuf,你不需要另一个映射。 常规实体的所有持久化操作可用于protobuf对象。 我们相信这是greenDAO的一个独特的特征。
代码生成
greenDAO将生成Java数据对象(实体)和DAO对象。 DAO对象是根据实体允许最好的映射。
未来计划:生成适配器,也许CRUD活动。
开源
在G greenDAO的源代码是完全可用 itHub 。 源分布还包含一个JUnit测试套件,它使用greenDAO的所有功能,因此对greenDAO学习的极好途径。
在项目中如何使用GreenDao进行操作。
- 先新建一个Java类型的Module,选择File -> New -> New Module ,类型选择Java Library ,当然库名、包名和类名随便啦。我这里起的名字如下:库名–greendaogenerator,包名-com.lanou3g.greenrobot,类名-ExpDaoGrnerator;
-
-
-
3.添加这个Java Module的GreenDao依赖,打开Java库下面的build.gradle文件,添加如下依赖:
compile ‘de.greenrobot:greendao-generator:2.0.0’
- 4.打开Java文件,写入如下内容:
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package</span> com.lanou3g.greenrobot; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> de.greenrobot.daogenerator.DaoGenerator; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> de.greenrobot.daogenerator.Entity; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> de.greenrobot.daogenerator.Schema; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">ExpDaoGenerator</span> </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>(String[] args) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建Schema对象</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 构造方法第一个参数为数据库版本号</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第二个参数为自动生成的实体类将要存放的位置,前面为我的Android Module的包名</span> Schema schema = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Schema(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"com.lanou3g.greendaodemo.entity.greendao"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 添加需要创建的实体类信息</span> addNote(schema); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建实体类.第二个参数填Android Module的路径</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> DaoGenerator().generateAll(schema, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"./AppExample/src/main/java"</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (Exception e) e.printStackTrace(); <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 添加将要创建的实体类的信息,会根据类名生成数据库的表,属性名生成数据库的字段<p> * 当然如果想建多张表,可以创建多个Entity对象 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> schema */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">addNote</span>(Schema schema) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 指定需要生成实体类的类名,类名确定了那么表名也是根据这个类名来自动命名的,例如下面这个,生成的表名叫做person_entity</span> Entity entity = schema.addEntity(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"PersonEntity"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 指定自增长主键</span> entity.addIdProperty().autoincrement().primaryKey(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 添加类的属性,根据属性生成数据库表中的字段</span> entity.addStringProperty(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>); entity.addIntProperty(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>); entity.addStringProperty(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sex"</span>); </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li></ul>
-
在java文件上面右键,选择Run’ExpDaoGenerator main()’,运行后,会在Android Module下面的相应包下生成一系列的java类文件,如下图:
-
5.打开Android Module,默认名字叫app,我这里改成了AppExample,打开这里面的build.gradle文件,添加依赖如下:
compile ‘de.greenrobot:greendao:2.0.0’
跟刚才Java那个不一样是吧,看出区别来了吧;
- 6.进行数据库中对应表的操作,需要进行一系列的初始化,看代码:
至此,Java程序的作用就结束了,可以退休了,下面是在Android Module中的使用方式了
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">AppCompatActivity</span> </span> <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** 省略一些控件的声明 */</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 数据库</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> SQLiteDatabase db; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 管理者</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> DaoMaster mDaoMaster; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 会话</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> DaoSession mDaoSession; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 对应的表,由java代码生成的,对数据库内相应的表操作使用此对象</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> PersonEntityDao personDao; <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onCreate</span>(Bundle savedInstanceState) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initDatabase(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">initDatabase</span>() <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化就这个顺序,记着吧 ^_^</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 此DevOpenHelper类继承自SQLiteOpenHelper,第一个参数Context,第二个参数数据库名字,第三个参数CursorFactory</span> DaoMaster.DevOpenHelper helper = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> DaoMaster.DevOpenHelper(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"daodemo.db"</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>); db = helper.getWritableDatabase(); mDaoMaster = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> DaoMaster(db); mDaoSession = mDaoMaster.newSession(); PersonEntityDao personDao = mDaoSession.getPersonEntityDao(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 删除表内所有数据(我为什么要写这个方法...太简单了吧也),写入前先清空一下,防止id冲突导致报错</span> personDao.deleteAll(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 下面对数据库内的表进行操作</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 首先模拟一些数据</span> List<PersonEntity> personList = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ArrayList<>(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>; i++) PersonEntity person = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> PersonEntity((<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">long</span>) i,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"孙悟空"</span>+i,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>+i,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"男"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用这个方法可以一条一条的写入数据库</span> personDao.insert(person); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 插入或者替换单条数据,就是id重复也可以使用</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// personDao.insertOrReplace(person);</span> personList.add(person); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 这个方法可以将一个集合直接写入数据库,很方便</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// personDao.insertInTx(personList);</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 看名字也知道是干嘛的了吧,把数据写入,或者替换</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// personDao.insertOrReplaceInTx(personList);</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 根据id删除数据 - -! 我真的一定要写出来么</span> personDao.deleteByKey(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>l); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 更新表内元素</span> personDao.update(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> PersonEntity(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>l,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"贝吉塔"</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"男"</span>)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 查表,结果直接返回到链表中,下面的三种方法都可以进行查表操作,将所有信息都查出来</span> List<PersonEntity> queryList = personDao.queryBuilder().list(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// queryList = personDao.queryBuilder().listLazy();</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// queryList = personDao.loadAll();</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 输出结果,看一下</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (PersonEntity personEntity : queryList) Log.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TAGGG"</span>,personEntity.getName()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">initView</span>() <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** 初始化控件的,没什么用,省略了 */</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li></ul>
下面看一下数据库内的内容:
可以看到id为3的那一条数据被删除掉了,id为5的那一条数据被修改了;
再看一下Logcat输出内容:
-
可以看到,数据成功的查找出来了;
-
7.很多界面都这么写数据库肯定会很麻烦嘛,所以需要使用单例.我就简单写一个了,见笑啦,代码如下
-
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">DaoSingleton</span> </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> String DATABASE_NAME = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"daodemo.db"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">volatile</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> DaoSingleton instance; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> SQLiteDatabase db; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> DaoMaster daoMaster; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> DaoSession daoSession; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> Context context; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> DaoMaster.DevOpenHelper helper; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> PersonEntityDao personDao; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-title" style="box-sizing: border-box;">DaoSingleton</span>() context = BaseApplication.getContext(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> DaoSingleton <span class="hljs-title" style="box-sizing: border-box;">getInstance</span>() <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (instance == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">synchronized</span> (DaoSingleton.class) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (instance == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) instance = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> DaoSingleton(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> instance; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> DaoMaster.DevOpenHelper <span class="hljs-title" style="box-sizing: border-box;">getHelper</span>() <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (helper == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) helper = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> DaoMaster.DevOpenHelper(context,DATABASE_NAME,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> helper; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> SQLiteDatabase <span class="hljs-title" style="box-sizing: border-box;">getDb</span>() <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (db == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) db = getHelper().getWritableDatabase(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> db; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> DaoMaster <span class="hljs-title" style="box-sizing: border-box;">getDaoMaster</span>() <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (daoMaster == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) daoMaster = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> DaoMaster(getDb()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> daoMaster; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> DaoSession <span class="hljs-title" style="box-sizing: border-box;">getDaoSession</span>() <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (daoSession == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) daoSession = getDaoMaster().newSession(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> daoSession; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> PersonEntityDao <span class="hljs-title" style="box-sizing: border-box;">getPersonDao</span>() <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (personDao == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) personDao = getDaoSession().getPersonEntityDao(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> personDao; </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;以上是关于使用GreenDao对数据库进行操作的主要内容,如果未能解决你的问题,请参考以下文章
Android 使用 GreenDAO 3.x 进行增删改查和升级