Android中使用ormlite实现持久化(一)--HelloOrmLite
openkk
13年前
<p><span>Android中内置了sqlite,但是常用的开发语言java是面向对象的,而数据库是关系型的,二者之间的转化每次都很麻烦(主 要是我对sql语言不熟悉)。而Java Web开发中有很多orm框架,但是想直接放到Android上用有些麻烦。尝试了一下找Android的orm框架,说实话还有好几个。</span></p> <span><br /> </span> <div> <span>实现考虑的是:androrm</span> </div> <div> <span>官网:<a href="/misc/goto?guid=4959500141775320948" rel="nofollow">http://androrm.the-pixelpla.net/</a></span> </div> <div> <span>说实话,这个我实在没有弄懂,一共两个包。</span> </div> <div> <span>一个是依赖包:<a href="/misc/goto?guid=4958199548559953718" rel="nofollow">Apache Commons - Lang</a> (2.6)</span> </div> <div> <span>另外一个就是主包:androrm.jar 不管怎么下载的都不能使用...</span> </div> <div> <span><br /> </span> </div> <div> <span>然后有考虑了一下db4o</span> </div> <div> <span>官网:<a href="/misc/goto?guid=4958530976378782071" rel="nofollow">http://www.db4o.com/</a></span> </div> <div> <span>官网上的介绍说是已经支持Android了,但是我一是觉得包有点大,而是觉得速度有点慢</span> </div> <div> <span><br /> </span> </div> <div> <span>最后看到的就是ormlite</span> </div> <div> <span>官网:</span> <a href="/misc/goto?guid=4959500141929672163" rel="nofollow">http://ormlite.com/</a> </div> <div> 一共两个包:一个是ormlite-core-4.24.jar,另一个是ormlite-android-4.24.jar </div> <div> 从以下网址可以下载到: <a href="/misc/goto?guid=4959500142018538375" rel="nofollow">http://ormlite.com/releases/</a> </div> <div> </div> <div> 下面按照惯例来个Hello world </div> <div> 新建Android项目:HelloOrmLite </div> <div> <div> <p><img alt="Android中使用ormlite实现持久化(一)--HelloOrmLite" src="https://simg.open-open.com/show/2a9fcf5a667b4574e092192dd0715a8c.jpg" width="524" height="837" /></p> </div> </div> <div> 添加文件夹:libs,将所需的两个包复制到其中。添加引用 </div> <div> <div> <p><img alt="Android中使用ormlite实现持久化(一)--HelloOrmLite" src="https://simg.open-open.com/show/eacfea62a8d07388698447ac3d38fc87.jpg" width="292" height="263" /></p> </div> <div> 新建一个model:Hello.java </div> <div> <pre class="brush:java; toolbar: true; auto-links: false;">package cn.sdx.model; import com.j256.ormlite.field.DatabaseField; public class Hello { @DatabaseField(generatedId = true) int id; @DatabaseField String word; public Hello() { } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("id=").append(id); sb.append(" ,word=").append(word); return sb.toString(); } } </pre> </div> <div> @DatabaseField是声明id为数据库字段,generatedId =true声明id为自增长 </div> <div> 然后重写了toString() </div> <div> </div> <div> 再添加一个DataHelper.java </div> <div> <div> <pre class="brush:java; toolbar: true; auto-links: false;">package cn.sdx.utils; import java.sql.SQLException; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import cn.sdx.model.Hello; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; public class DataHelper extends OrmLiteSqliteOpenHelper { private static final String DATABASE_NAME = "HelloOrmlite.db"; private static final int DATABASE_VERSION = 1; private Dao <hello, integer=""> helloDao = null; public DataHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource, Hello.class); } catch (SQLException e) { Log.e(DataHelper.class.getName(), "创建数据库失败", e); e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2, int arg3) { try { TableUtils.dropTable(connectionSource, Hello.class, true); onCreate(db, connectionSource); } catch (SQLException e) { Log.e(DataHelper.class.getName(), "更新数据库失败", e); e.printStackTrace(); } } @Override public void close() { super.close(); helloDao = null; } public Dao <hello, integer=""> getHelloDataDao() throws SQLException { if (helloDao == null) { helloDao = getDao(Hello.class); } return helloDao; } } </hello,> </hello,></pre> </div> </div> <div> 在布局文件中添加一个TextView </div> <div> HelloOrmliteActivity.java中添加对数据库的操作 </div> <div> </div> <div> 代码如下: </div> <pre class="brush:java; toolbar: true; auto-links: false;">package cn.sdx; import java.sql.SQLException; import java.util.List; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; import com.j256.ormlite.dao.Dao; import android.os.Bundle; import android.widget.TextView; import cn.sdx.model.Hello; import cn.sdx.utils.DataHelper; public class HelloOrmliteActivity extends OrmLiteBaseActivity <datahelper> { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) this.findViewById(R.id.output); try { Dao <hello, integer=""> helloDao = getHelper().getHelloDataDao(); // 添加数据 for (int i = 0; i < 2; i++) { Hello hello = new Hello("Hello" + i); helloDao.create(hello); } tv.setText(tv.getText() + "\n" + "添加数据完成"); // 查询添加的数据 List <hello> hellos = helloDao.queryForAll(); for (Hello h : hellos) { tv.setText(tv.getText() + "\n" + h.toString()); } // 删除数据第一条数据 helloDao.delete(hellos.get(0)); tv.setText(tv.getText() + "\n" + "删除数据完成"); // 重新查询数据 hellos = helloDao.queryForAll(); for (Hello h : hellos) { tv.setText(tv.getText() + "\n" + h.toString()); } // 修改数据 Hello h1 = hellos.get(0); h1.setWord("这是修改过的数据"); tv.setText(tv.getText() + "\n" + "修改数据完成"); helloDao.update(h1); // 重新查询数据 hellos = helloDao.queryForAll(); for (Hello h : hellos) { tv.setText(tv.getText() + "\n" + h.toString()); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </hello> </hello,> </datahelper></pre> <br /> <div> 以上实现了数据库操作相关的增删改,下面是效果: </div> <div> </div> <div> <div> <p><img style="width:675px;height:483px;" alt="Android中使用ormlite实现持久化(一)--HelloOrmLite" src="https://simg.open-open.com/show/eb464df0a4e07fa96442d32231941a91.jpg" /></p> </div> </div> <div> </div> OrmLite的功能非常强大,Model类的声明中非常重要,外键约束,非空检查等等问题都有相对的处理方法。 <br /> <br /> </div>