根据DBflow3.0移植过来的,移植过程中有很多问题,比如类找不到啊,因为代码比较多修改起来比较慢,而且移植到eclipse上面提前编译直接报错,还找不到错误的原因,没办法只有一步一步的通过文件打log的方式才找到原因,我修改的地方我都写在com.gengsheng 包下面了Util.java
我还增加了可以支持数据库放入到SD卡中
还可以用sqlite3工具类创建好数据库然后把数据库放入到res/raw/initdb.db,注意把名字修改成initdb.db文件
DBFlow3 好处在于效率比反射的高
使用方式和github 上面的一样
1 初始化
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语句直接写 // 执行原生sql BaseDatabaseDefinition 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); } @Override public 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"; }
/** * 物料编号表 * <p> * * @author * @version 2016年3月26日 */ @ModelContainer @Table(database = ToolsDB.class) public class McItem extends BaseModel { @PrimaryKey(autoincrement = true) public int id; @NotNull @Column private String itemnum; @Column private String description; @Column private int rotating; @Column private String lottype; @Column private long capitalized; @Column private String in19; @Column private String in20; @Column private String in21; @Column private String siteid; @Column private String orgid; @Column private String itemmid; @Column private String itemtype_t; @Column private String commodity; @Column private String metername; @Column private String itemsetid; @Column private String status; @Column private String c_model; @Column private String materialtype; @Column private long itemid; @Column private long outside; @Column private String msdsnum; @Column private String sendersysid; @Column private String orderunit; @Column private String issueunit; @Column private String conditionenabled; @Column private String groupname; @Column private String newcost; @Column private String temp1; @Column private 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; } }
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>(); McItem mc = 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