关于如何建立类生成一个演示project。今天介绍如何使用。

这是ExampleDaoGeneratorproject代码,做了一些改动

/*
* Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.greenrobot.daogenerator.gentest; import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Property;
import de.greenrobot.daogenerator.Schema;
import de.greenrobot.daogenerator.ToMany; /**
* Generates entities and DAOs for the example project DaoExample.
*
* Run it as a Java application (not Android).
*
* @author Markus
*/ public class ExampleDaoGenerator { public static void main(String[] args) throws Exception {
//该方法第一个參数用来更新数据库版本。第二个參数为要生成的DAO类所在包路径。
Schema schema = new Schema(1, "de.greenrobot.daoexample"); //然后进行建表
addNote(schema);
//addCustomerOrder(schema); //设置要生成DAO文件的目标工程的项目路径,当中src-gen这个文件夹名须要在执行前手动创建。否则会报错
new DaoGenerator().generateAll(schema, "../../HelloWorld/src-gen");
} private static void addNote(Schema schema) {
/**
* 在使用greenDAO时,一个实体类仅仅能相应一个表,眼下没法做到一个表相应多个实体类。或者多个表共用一种对象类型。 * 兴许的升级也不会针对这一点进行扩展。
* */
Entity note = schema.addEntity("Note");//创建表
//note.addIdProperty(); //添加ID列
note.addIdProperty().primaryKey().autoincrement();//设置一个自增长ID列为主键:
note.addStringProperty("text").notNull();//创建非空的列
note.addStringProperty("comment").unique();//创建唯一的
//note.addDateProperty("date");
} private static void addCustomerOrder(Schema schema) {
Entity customer = schema.addEntity("Customer");
customer.addIdProperty();
customer.addStringProperty("name").notNull(); Entity order = schema.addEntity("Order");
order.setTableName("ORDERS"); // "ORDER" is a reserved keyword
order.addIdProperty();
Property orderDate = order.addDateProperty("date").getProperty();
Property customerId = order.addLongProperty("customerId").notNull().getProperty();
order.addToOne(customer, customerId); ToMany customerToOrders = customer.addToMany(order, customerId);
customerToOrders.setName("orders");
customerToOrders.orderAsc(orderDate);
} }

上面代码主要有两个

Schema schema = new Schema(1, "de.greenrobot.daoexample");

第一个參数为版本,第二个參数为产生的类放在引用project的那个地方,须要结合以下代码

new DaoGenerator().generateAll(schema, "../../HelloWorld/src-gen");

那么产生的文件将放到

ExampleDaoGeneratorproject文件夹的上上两级文件夹下的  HelloWorld/src-gen/de/greenrobot/daoexample 文件夹下。

1、我们在ExampleDaoGeneratorproject下,按下键盘的 ctrl+F11 则会自己主动生成四个java类放到HelloWorld/src-gen/de/greenrobot/daoexample文件夹以下

2、把这4个类依赖的相关类放到一起。依赖的类直接把 "greenDAO-master\DaoCore\src\de"文件夹下的内容放到一起4个类一起。

3、将src-gen文件夹设置为编译文件夹

4、依据生成类添加相关代码,这里仅仅贴相关用到的代码

    private DaoMaster daoMaster;
private DaoSession daoSession;
private NoteDao noteDao; private Cursor cursor;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); DevOpenHelper helper = new DaoMaster.DevOpenHelper(MainActivity.this, "Note", null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
noteDao = daoSession.getNoteDao();
}
		final Button btn6 = (Button) findViewById(R.id.button6);
btn6.setOnClickListener(new OnClickListener() {
public void onClick(View v) { Toast.makeText(MainActivity.this, "长度 :" + noteDao.count(),
Toast.LENGTH_SHORT).show();
Note note = new Note(null, "noteText1", "comment1");
try {
noteDao.insert(note); } catch (Exception e) {
Toast.makeText(MainActivity.this, "数据反复 :" + noteDao.count(),
Toast.LENGTH_SHORT).show();
}

5、按ctrl+shift+o自己主动补齐相关类

6、下载APK到手机,就可以。

补充:

在调试过程中,改动了生成project的类之后发现,又一次编译helloworldproject有出现代码异常的情况 代码位于"db = helper.getWritableDatabase();"

该问题临时未知道什么原因。但有方法解决:在手机端-->在设置界面-->选择相应的应用程序(为了我,它是helloworld此应用程序)-->清除数据

android greenDao SQLite数据库操作使用的工具的更多相关文章

  1. Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库

    下面是最原始的方法,用SQL语句操作数据库.后面的"Android中SQLite数据库操作(2)--SQLiteOpenHelper类"将介绍一种常用的android封装操作SQL ...

  2. 我的Android六章:Android中SQLite数据库操作

    今天学习的内容是Android中的SQLite数据库操作,在讲解这个内容之前小编在前面有一篇博客也是讲解了SQLite数据库的操作,而那篇博客的讲解是讲述了 如何在Window中通过DOM来操作数据库 ...

  3. Android中SQLite数据库操作(2)——SQLiteOpenHelper类

    如果开发者对SQL语法不熟悉,我要告诉你一个好消息,Android提供了一个SQLiteOpenHelper类. 在实际项目中很少使用SQLiteDatabase的方法(请看:http://blog. ...

  4. Android中SQLite数据库操作(2)——使用SQLiteDatabase提供的方法操作数据库

    如果开发者对SQL语法不熟,甚至以前从未使用过任何数据库,Android的SQLiteDatabase提供了insert.update.delete或query语句来操作数据库. 一.insert方法 ...

  5. Android实现SQLite数据库联系人列表

    Android实现SQLite数据库联系人列表 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个通讯录查看程序: 要求使用SQLite ...

  6. [Android] Sqlite 数据库操作 工具封装类

    sqlite 数据库封装类 DatabaseUtil.java(封装的类) package com.jack.androidbase.tools; import android.content.Con ...

  7. android中的数据库操作(SQLite)

    android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库   an ...

  8. 【Android】实验8 SQLite数据库操作2016.5.12

    实验8  SQLite数据库操作 [目的] 设计一个个人通讯录,掌握Android平台下的数据库开发,该个人通讯录主要包括联系人列表和联系人详细信息等界面. [要求] 程序主界面是通讯录的目录显示手机 ...

  9. 安卓 SQLite数据库操作实例

    前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...

随机推荐

  1. webstorm与phpstorm主题配置

    原创. 更换webstorm的主题的,照着网上的教程试了好多次都发现不行,而且我之前有个同学也是这样的问题,找不到相关的colors文件夹,所以在网上教程的基础上对于更改主题做了细微的修改. 1.下载 ...

  2. 跨平台编程中的宏定义(__LINE__和__DATE__极其有用)

    要用师兄的计算机算东西,无赖那上面是WINDOWS的系统,为了写出能够跨平台的代码,需要在代码中用到宏来选择编译.一种方法是自己在Makefile里面定义好该平台对应的宏.实际上,编译器基本上都会有一 ...

  3. QT工程pro设置实践(with QtCreator)----非弄的像VS一样才顺手?

    源地址:http://my.oschina.net/jinzei/blog/100989?fromerr=DhQJzZQe 相信大家很多和我一样,用多了微软给的便利,用人家的就十分不习惯.于是就琢磨原 ...

  4. ArcGIS制图——多图层道路压盖处理

    [1]      启动ArcMap,载入各道路要素类.生成名为All-Roads的图层组,如他所看到的,符号级别绘制将对图层组内图层符号起作用: 图层组设置符号等级 [2]      在内容列表中,右 ...

  5. Effective C++_笔记_条款03_尽可能使用const

    (整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 关键字const多才多艺,语法变化多端.关于const的基本用法 ...

  6. JS - 循环添加 DropDownList(Select)

    代码: <td style="padding-left: 10px;"> <select id="ddl_picture_3"> < ...

  7. poj 2786 - Keep the Customer Satisfied

    Description   Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousa ...

  8. TControl.GetDeviceContext会给图形控件建立新的坐标原点和建立新的剪裁区域

    这是取得DC句柄的其中一种方法(会重定义原点和建立新的剪裁区): function TControl.GetDeviceContext(var WindowHandle: HWnd): HDC; be ...

  9. LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)

    Best Time to Buy and Sell Stock Total Accepted: 14044 Total Submissions: 45572My Submissions Say you ...

  10. "Invalid username/password or database/scan listener not up"

        文档 ID …         11.2 RAC DBconsole Creation Fails With Error: "Invalid username/password or ...