Android使用GreenDao连接数据库
来源:互联网 发布:淘宝隐藏优惠卷 编辑:程序博客网 时间:2024/04/30 15:03
安卓中对SQLite的操作,如果不借助工具类的话很容易出错,比如SQL语句不规范(少个空格是很经常的事),而借助一些工具类能很明显的提升编程效率。GreenDao是一个很好的开源工具。用法如下:1.在eclipse下新建一个java project,在项目中新建名为lib的package,在lib中导入两个jar包:freemarker.jar和greendao-generator-2.0.0.jar(先复制到lib目录下,然后右键lib,选择build path-->configure build path,在libraries一栏点击add jars,选择拷贝好的两个jar包点击ok)2.在项目下新建测试类:GreenDaoTest,写入如下代码:
import java.io.IOException;import de.greenrobot.daogenerator.DaoGenerator;import de.greenrobot.daogenerator.Entity;import de.greenrobot.daogenerator.Schema;public class GreenDaoTest { public static void main(String[] args) { Schema schema=new Schema(1,"com.example.ygd.jreduch08"); addUser(schema); try { new DaoGenerator().generateAll(schema, "D:\\GreenDaoSrc"); //创建任意文件夹并将文件夹位置写到这 } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static void addUser(Schema schema){ Entity entity=schema.addEntity("User"); //实体类的java文件 entity.setTableName("user"); //表名 entity.addIdProperty().primaryKey().autoincrement(); entity.addStringProperty("name"); //属性 entity.addStringProperty("pwd"); entity.addStringProperty("age"); entity.addStringProperty("imgUrl"); }}
运行该程序,得到如下结果:![这里写图片描述](http://img.blog.csdn.net/20160428155305614)在刚才新建的文件夹下找到建好的四个文件,复制到android studio的dao目录下(没有则新建)然后,在android studio中导包,先拷到libs目录下,在点击添加依赖项,选择第二项 File Dependency。导完如下图:![这里写图片描述](http://img.blog.csdn.net/20160428164951371)在Activity中调用:
userDao= MyApplication.getInstance().getDaoSession(this).getUserDao();
然后重写application,新建一个application的包,然后在下面新建一个MyApplication.class,代码如下:
package com.example.ygd.jreduch08.application;import android.app.Application;import android.content.Context;import com.example.ygd.jreduch08.dao.DaoMaster;import com.example.ygd.jreduch08.dao.DaoSession;public class MyApplication extends Application { private DaoSession daoSession; private DaoMaster daoMaster; //Application实例对象 private static MyApplication instance; @Override public void onCreate() { super.onCreate(); instance=this; } public static MyApplication getInstance(){ return instance; } public DaoMaster getDaoMaster(Context context){ if(daoMaster==null){ DaoMaster.OpenHelper helper=new DaoMaster.DevOpenHelper(context,"MyDbTest.db",null); daoMaster=new DaoMaster(helper.getWritableDatabase()); } return daoMaster; } public DaoSession getDaoSession(Context context){ if(daoSession==null){ if(daoSession==null){ daoMaster=getDaoMaster(context); } daoSession=daoMaster.newSession(); } return daoSession; }}
在清单文件中将application的名字设置为刚才重写的方法。
然后就可以使用GreenDao提供的各种工具了。
(如果要升级数据库的话,也是要在eclipse中操作,重新生成那四个文件,然后再拷贝过去)
1 0
- Android使用GreenDao连接数据库
- Android 数据库 GreenDao使用
- Android GreenDao数据库框架使用
- android使用GreenDao操作数据库
- Android--------GreenDao数据库的使用
- Android数据库 GreenDao的使用
- Android greenDao 数据库的使用(一)
- Android greenDao 数据库的使用(二)
- Android greenDao 数据库的使用(三)
- Android Studio平台使用GreenDao操作数据库
- 提高android数据库性能,GreenDao初步使用
- Android 初使用GreenDAO框架操作数据库
- android高效ORM数据库框架greenDao使用
- Android数据库操作--greenDAO的入门使用
- android 数据库greenDao的简单使用
- Android数据库之greendao的基本使用
- Android GreenDao使用(一)创建数据库
- Android GreenDao使用(三)数据库升级
- 常见Sort排序算法总结,人生第一帖
- Log4j调试
- Error:Execution failed for task ':app:transformClassesWithDexForUmengDebug'.
- Go语言实现工时计算
- docker入门时碰到的代理设置问题
- Android使用GreenDao连接数据库
- 雅思学习笔记
- Metal 着色语言编程指南 十四
- GCD 深入理解(一)
- 10分钟让你有胆量在众人前演讲(1)
- iOS开发网络篇—监测网络状态
- 最受欢迎的5个Android ORM框架
- WPF进阶之接口:INotifyPropertyChanged,ICommand
- 剑指offer(46):左转字符串