http://www.huangyunkun.com/2011/10/30/Android_Ormlite_1/
Android中使用ormlite实现持久化--HelloOrmLite
2011-10-30 更新日期:2014-03-18
Android中内置了sqlite,但是常用的开发语言java是面向对象的,而数据库是关系型的,二者之间的转化每次都很麻烦(主要是我对sql语言不熟悉)。而Java Web开发中有很多orm框架,但是想直接放到Android上用有些麻烦。尝试了一下找Android的orm框架,说实话还有好几个。
实现考虑的是:androrm
官网:http://androrm.the-pixelpla.net/
说实话,这个我实在没有弄懂,一共两个包。
一个是依赖包:Apache Commons - Lang (2.6)
另外一个就是主包:androrm.jar 不管怎么下载的都不能使用…
然后有考虑了一下db4o
官网:http://www.db4o.com/
官网上的介绍说是已经支持Android了,但是我一是觉得包有点大,而是觉得速度有点慢
最后看到的就是ormlite
官网:http://ormlite.com/
一共两个包:一个是ormlite-core-4.24.jar,另一个是ormlite-android-4.24.jar
从以下网址可以下载到:http://ormlite.com/releases/
下面按照惯例来个Hello world
新建Android项目:HelloOrmLite
添加文件夹:libs,将所需的两个包复制到其中。添加引用
新建一个model:Hello.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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(); } } |
@DatabaseField是声明id为数据库字段,generatedId =true声明id为自增长
然后重写了toString()
再添加一个DataHelper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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; } } |
在布局文件中添加一个TextView
HelloOrmliteActivity.java中添加对数据库的操作
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 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();
}
}
}
|
以上实现了数据库操作相关的增删改,下面是效果:
OrmLite的功能非常强大,Model类的声明中非常重要,外键约束,非空检查等等问题都有相对的处理方法。
Android中使用Ormlite实现持久化(二)--持久化类的详细配置
2011-11-01 更新日期:2014-03-16
上一篇文章简单的使用了Ormlite一下,但是我觉得Ormlite出色之处就是对于需要字段,表等等的配置设计的比较好。
下面来说说类的配置:
如果我们开发的Android应用需要保持用户信息,那么现在新建一个类:UserAccount
该类有六个变量:
1 | private int id; private String username; private String password; private Date regTime; private String tellphone; private String email; |
1 | public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getRegTime() { return regTime; } public void setRegTime(Date regTime) { this.regTime = regTime; } public String getTellphone() { return tellphone; } public void setTellphone(String tellphone) { this.tellphone = tellphone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } |
下面通过注解的方式配置该类的持久化参数:
1.表名:
1 | @DatabaseTable(tableName="dataTableName") |
不指定的话表名就是类名。
2.字段
1 | @DatabaseField
|
这个可以配置的属性有点多。
2.1 主键
1 | @DatabaseField(id=true) |
2.2 列名
1 | @DatabaseField(columnName="columnName") |
不指定的话就是和变量名一样的
2.3 数据类型
1 | @DatabaseField(dataType=DataType.INTEGER)
|
这个一般情况下都不用指定,可以根据java 类获得
2.4 默认值
1 | @DatabaseField(defaultValue="0") |
2.5 长度
1 | @DatabaseField(width=13) |
一般用于String型
2.6 能否为空
1 | @DatabaseField(canBeNull=false) |
默认为True
2.7 是否自增长
1 | @DatabaseField(generatedId=true) |
这是一些比较简单的,关于外键啥的下篇文章再说吧。



1万+



被折叠的 条评论
为什么被折叠?



