DBFlow for eclipse 移植

来源:互联网 发布:dota2 数据bld是什么 编辑:程序博客网 时间:2024/06/06 03:21

根据DBflow3.0移植过来的,移植过程中有很多问题,比如类找不到啊,因为代码比较多修改起来比较慢,而且移植到eclipse上面提前编译直接报错,还找不到错误的原因,没办法只有一步一步的通过文件打log的方式才找到原因,我修改的地方我都写在com.gengsheng 包下面了Util.java


我还增加了可以支持数据库放入到SD卡中


还可以用sqlite3工具类创建好数据库然后把数据库放入到res/raw/initdb.db,注意把名字修改成initdb.db文件


DBFlow3 好处在于效率比反射的高


使用方式和github 上面的一样


 

</pre>//初始化<span style="white-space:pre"></span><p>//把数据库放入的地址,传null放在默认位置 ,true代表是用res/raw下面的数据库,必须要保证数据库还没创建成文件,也就是1</p><p></p><pre code_snippet_id="1623558" snippet_file_name="blog_20160328_1_857552" name="code" class="java"> File docFilePath = CreatFileUtil.getDownLoad(this);//数据库存储位置

FlowManager.init(this, docFilePath, true);


2    创建表对象 //注意属性是public  修饰

<pre name="code" class="java"><pre name="code" class="java">package com.jtv.testdatabase.db;import java.util.List;import com.raizlabs.android.dbflow.annotation.Column;import com.raizlabs.android.dbflow.annotation.ModelContainer;import com.raizlabs.android.dbflow.annotation.PrimaryKey;import com.raizlabs.android.dbflow.annotation.Table;import com.raizlabs.android.dbflow.sql.language.SQLite;import com.raizlabs.android.dbflow.structure.BaseModel;/** * Created by gengsheng on 2016/3/14. */@ModelContainer@Table(database = StaticCheckDatabase.class)public class Zx_lc extends BaseModel {    @PrimaryKey(autoincrement = true)    public long id;    @Column    public String wonum;    @Column    public String assetnum;    @Column    public double startlc;    @Column    public double endlc;        @Column    public String email;        @Column    public String email_hello;        @Column    public String ADD3;}3 创建数据库对象@Database(name = StaticCheckDatabase.NAME, version = StaticCheckDatabase.VERSION)public class StaticCheckDatabase {    public static final String NAME= "staticcheck";    public static final int VERSION=14;}//4 使用  zx_lc = new Zx_lc();zx_lc.assetnum = "zz1234";zx_lc.wonum = "wn3345";zx_lc.startlc = 110.234;zx_lc.endlc = 111.234;zx_lc.insert();//5  也支持sql语句直接写// 执行原生sqlBaseDatabaseDefinition database = FlowManager.getDatabase(StaticCheckDatabase.NAME);AndroidDatabase android = (AndroidDatabase) database.getWritableDatabase();Cursor rawQuery = android.rawQuery("select * from zx_lc", null);if (rawQuery != null) {while (rawQuery.moveToNext()) {String string = rawQuery.getString(0);Toast.makeText(this, string, Toast.LENGTH_SHORT).show();}rawQuery.close();}//删除表new Delete().from(Zx_lc.class).execute();// 实时保存,马上保存new SaveModelTransaction<>(ProcessModelInfo.withModels(Zx_lcList)).onExecute();// 异步保存,使用异步,如果立刻查询可能无法查到结果TransactionManager.getInstance().addTransaction(new SaveModelTransaction<>(ProcessModelInfo.withModels(Zx_lcList)));6 表结构变化如果新增表直接创建一个对象,创建对象参考第二步,并把版本增加如果增加表列/**  * 在版本11中增加了email字段 *  * @author  * */@Migration(version = 11, database = StaticCheckDatabase.class)public class AleartZx_lc extends AlterTableMigration<Zx_lc> {public AleartZx_lc(Class<Zx_lc> table) {super(table);}@Overridepublic void onPreMigrate() {addColumn(SQLiteType.TEXT, Zx_lc_Table.email.getNameAlias().getName());addColumn(SQLiteType.TEXT, Zx_lc_Table.email_hello.getNameAlias().getName());// addColumn(SQLiteType.TEXT,// Zx_lc_Table.ADD3.getNameAlias().getName());}},



//如果12版本有增加了,在创建一个这种的把版本改为12,并把数据库对象中版本改为12


7 引入jar包方式并提前编译  把dbflow3.0 放入lib下面,然后选中项目,鼠标右键properties,点击java compiler 在点annotation Processin 在点击Factory path ,把右边的ENABLE project sep...勾选上然后点击右侧的Add JARs..,选中DBflow3.0.jar ,然后apply ,然后ok 完毕







8 当你引用注解时编译器就会自动创建表所需要的对象


9 当你如果改了数据库对象值,文件最好重新编译下,(不重新编译很有可能项目直接运行奔溃)它会重新生成文件,你可以把自动生成的包删了,然后看见删了后,编译器会重新编译一个文件,



10 注解如果没有设置源代码存放位置,默认生成在项目下面,是一个隐藏文件,如果项目报错很有可能是switch的原因,需要jre为1.7才可以



文件地址 http://download.csdn.net/detail/tomcat_lgs/9472426




完整的创建数据库和创建表代码,其他自动生成,使用方式参考上面

//1 创建数据库

import com.raizlabs.android.dbflow.annotation.Database;@Database(name = ToolsDB.NAME, version = ToolsDB.Version)public class ToolsDB {//数据库版本public final static int Version = 9;//数据库名字public final static String NAME = "tools";}


//2 创建表

/** * 物料编号表 * <p> * * @author  * @version 2016年3月26日 */@ModelContainer@Table(database = ToolsDB.class)public class McItem extends BaseModel {@PrimaryKey(autoincrement = true)public int id;@NotNull@Columnprivate String itemnum;@Columnprivate String description;@Columnprivate int rotating;@Columnprivate String lottype;@Columnprivate long capitalized;@Columnprivate String in19;@Columnprivate String in20;@Columnprivate String in21;@Columnprivate String siteid;@Columnprivate String orgid;@Columnprivate String itemmid;@Columnprivate String itemtype_t;@Columnprivate String commodity;@Columnprivate String metername;@Columnprivate String itemsetid;@Columnprivate String status;@Columnprivate String c_model;@Columnprivate String materialtype;@Columnprivate long itemid;@Columnprivate long outside;@Columnprivate String msdsnum;@Columnprivate String sendersysid;@Columnprivate String orderunit;@Columnprivate String issueunit;@Columnprivate String conditionenabled;@Columnprivate String groupname;@Columnprivate String newcost;@Columnprivate String temp1;@Columnprivate int temp2;public String getItemnum() {return itemnum;}public void setItemnum(String itemnum) {this.itemnum = itemnum;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public int getRotating() {return rotating;}public void setRotating(int rotating) {this.rotating = rotating;}public String getLottype() {return lottype;}public void setLottype(String lottype) {this.lottype = lottype;}public long getCapitalized() {return capitalized;}public void setCapitalized(long capitalized) {this.capitalized = capitalized;}public String getIn19() {return in19;}public void setIn19(String in19) {this.in19 = in19;}public String getIn20() {return in20;}public void setIn20(String in20) {this.in20 = in20;}public String getIn21() {return in21;}public void setIn21(String in21) {this.in21 = in21;}public String getSiteid() {return siteid;}public void setSiteid(String siteid) {this.siteid = siteid;}public String getOrgid() {return orgid;}public void setOrgid(String orgid) {this.orgid = orgid;}public String getItemmid() {return itemmid;}public void setItemmid(String itemmid) {this.itemmid = itemmid;}public String getItemtype_t() {return itemtype_t;}public void setItemtype_t(String itemtype_t) {this.itemtype_t = itemtype_t;}public String getCommodity() {return commodity;}public void setCommodity(String commodity) {this.commodity = commodity;}public String getMetername() {return metername;}public void setMetername(String metername) {this.metername = metername;}public String getItemsetid() {return itemsetid;}public void setItemsetid(String itemsetid) {this.itemsetid = itemsetid;}public String getStatus() {return status;}public void setStatus(String status) {this.status = status;}public String getC_model() {return c_model;}public void setC_model(String c_model) {this.c_model = c_model;}public String getMaterialtype() {return materialtype;}public void setMaterialtype(String materialtype) {this.materialtype = materialtype;}public long getItemid() {return itemid;}public void setItemid(long itemid) {this.itemid = itemid;}public long getOutside() {return outside;}public void setOutside(long outside) {this.outside = outside;}public String getMsdsnum() {return msdsnum;}public void setMsdsnum(String msdsnum) {this.msdsnum = msdsnum;}public String getTemp1() {return temp1;}public void setTemp1(String temp1) {this.temp1 = temp1;}public int getTemp2() {return temp2;}public void setTemp2(int temp2) {this.temp2 = temp2;}public String getNewcost() {return newcost;}public void setNewcost(String newcost) {this.newcost = newcost;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getSendersysid() {return sendersysid;}public void setSendersysid(String sendersysid) {this.sendersysid = sendersysid;}public String getOrderunit() {return orderunit;}public void setOrderunit(String orderunit) {this.orderunit = orderunit;}public String getIssueunit() {return issueunit;}public void setIssueunit(String issueunit) {this.issueunit = issueunit;}public String getConditionenabled() {return conditionenabled;}public void setConditionenabled(String conditionenabled) {this.conditionenabled = conditionenabled;}public String getGroupname() {return groupname;}public void setGroupname(String groupname) {this.groupname = groupname;}}

// 3 使用表 -增删改查

import java.util.ArrayList;import java.util.List;import com.raizlabs.android.dbflow.runtime.transaction.process.ProcessModelInfo;import com.raizlabs.android.dbflow.runtime.transaction.process.SaveModelTransaction;import com.raizlabs.android.dbflow.sql.language.Delete;import com.raizlabs.android.dbflow.sql.language.Select;public class Test {public void test() {// 保存 --增McItem mcAdd = new McItem();mcAdd.setItemnum("添加");mcAdd.save();// 删除 -- 删new Delete().from(McItem.class).execute();ArrayList<McItem> list = new ArrayList<McItem>();McItemmc = new McItem();mc.setItemnum("事物2");list.add(mc);mc = new McItem();mc.setItemnum("事物3");list.add(mc);// --改mcAdd.setItemnum("添加 --改动");mcAdd.update();// 实时保存,马上保存 ,--事物new SaveModelTransaction<>(ProcessModelInfo.withModels(list)).onExecute();//--查List<McItem> data = new Select().from(McItem.class).queryList();}}



//报错03-28 11:00:41.306: E/AndroidRuntime(11498): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/com.test.test.activity.Test}: com.raizlabs.android.dbflow.structure.InvalidDBConfiguration: Table: com.test.db.McItem is not registered with a Database. Did you forget the @Table annotation?03-28 11:00:41.306: E/AndroidRuntime(11498): <span style="white-space:pre"></span>at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:206// 解决方案  --  把自动生成的包删除掉 --- com.raizlabs.android.dbflow.config --或者项目clear一下
如果没有自动生成这个包com.raizlabs.android.dbflow.config,可以看下是否是把源码生成目录设置成src,参考上面第一张图片java compiler,是否创建了表@table,如果都没有问题那就手动创建这个包,然后删掉这个包再重试一下,看它自动生成不
</pre><pre code_snippet_id="1623558" snippet_file_name="blog_20160328_6_718717" name="code" class="java">如果不能生成_adapter文件,可能原因是没有继承BaseModel,或者定义一个自动增长的@PrimaryKey(autoincrement = true)
</pre><pre code_snippet_id="1623558" snippet_file_name="blog_20160328_6_718717" name="code" class="java">关于表对象.save(),方法没有返回值的问题可以这样用就会返回插入的行数 表对象.getModelAdapter().getModelSaver().insert(表对象)
</pre><pre code_snippet_id="1623558" snippet_file_name="blog_20160328_6_718717" name="code" class="java">关于这个jar包稳定性是否可以使用在开发项目里面,用在项目里面是完全没问题,而且用来来特别方便,如果项目中涉及到很多数据库操作,建议使用这个dbflow,使用起来高效,开发时提高开发效率,亲测
</pre><pre code_snippet_id="1623558" snippet_file_name="blog_20160328_6_718717" name="code" class="java">
dbflow 联表查询
<pre name="code" class="java">// 查询资产的条码编号为空的数据public static List<Asset> queryAssetNull(int page) {int count = 20;// 查询多少条Property<String> onA = Asset_Table.assetnum.as("A.assetnum");// 别名A条件Property<String> onB = JtvAssetCode_Table.assetnum.as("B.assetnum");// 别名B条件Condition whereB = JtvAssetCode_Table.codenum.as("B.codenum").isNull();// 过滤条件// 要查询的内容,如果不设置,因为是两张表,就会出现数据错乱,tickName=false,去掉默认的符号Property<Asset> selectA = new Property<Asset>(Asset.class, new NameAlias("A.*").tickName(false));// 左连接查询List<Asset> list = SQLite.select(selectA).from(Asset.class).as("A").leftOuterJoin(JtvAssetCode.class).as("B").on(onA.eq(onB)).where(whereB).offset(count * page).limit(count).queryList();return list;}


左连接去重查询数量

public static int countAssets(String itemType) {
Property<String> onA = Asset_Table.assetnum.as("A.assetnum");// 别名A条件
Property<Asset> selectA = new Property<Asset>(Asset.class, new NameAlias("A.assetnum").tickName(false));


Property<String> onB = JtvAssetCode_Table.assetnum.as("B.assetnum");// 别名A条件
// long count = SQLite.selectCountOf().from(Asset.class).as("A").where(Asset_Table.itemtype.eq(itemType))
// .exists(new Select().from(JtvAssetCode.class).as("B").where(onA.eq(onB))).count();
// 效率不太高,适合数据少的


long count = SQLite.selectCountOf(new Method("distinct", selectA)).from(Asset.class).as("A")
.innerJoin(JtvAssetCode.class).as("B").on(onA.eq(onB))
.where(Asset_Table.itemtype.eq(itemType), JtvAssetCode_Table.codenum.isNotNull()).count();
return (int) count;
}

//其他dbflow 也支持直接写sql语句返回对象

比如 我需要查HmMaterial 表,我可以直接这样写

StringQuery<HmMaterial> stringQuery = new StringQuery<>(HmMaterial.class,
"SELECT * FROM `HmMaterial` WHERE `materaialClass`='0'   and materialName='分动齿轮箱'");
List<HmMaterial> queryList = stringQuery.queryList();




3 0
原创粉丝点击