一般java环境使用ormlite驱动sqlite

来源:互联网 发布:maya mac 2017中文版 编辑:程序博客网 时间:2024/05/16 04:34

ormlite有android包,在一般java环境中,这个包是用不上的,只用到ormlite.core和ormlite.jdbc。

另外,还需要下载一个sqlite-jdbc-x.x.x,注意这个包需要能支持自动增长id字段类型的。这个在ormlite官方网站有说明。

安卓包封装了一些东西,使得安卓用ormlite的时候很方便。但是一般java环境不同。



首先,关于驱动,sqlite-jdbc,要下载 xerial版本的。

但是,下载使用了最新版的 sqlite-jdbc-3.8.7.jar之后,系统log仍然会提示

SEVERE: WARNING: you seem to not be using the Xerial SQLite driver.  See ORMLite docs on SQLite: http://ormlite.com/docs/sqlite

不用理。


第二,不需要进行驱动装载

            Class.forName("org.sqlite.JDBC");

这一句估计已经被ormlite封装执行。


第三,判断表是否存在,从而建表。无须判断数据库文件是否存在

public class SqliteOpenHelper {

    private static Dao<lmPara, Integer> lmParaDao;


    public static Dao GetlmParaDao() throws SQLException {
        if (lmParaDao == null) {
            lmParaDao = DaoManager.createDao(GetConnectionSource(),
                    lmPara.class);
        }
        return lmParaDao;
    }


    public static void setupDatabase() throws Exception {
        GetlmParaDao();
        
        if(!lmParaDao.isTableExists()){    //这一句用语判断数据库及表是否存在
            // if you need to create the table
            TableUtils.createTable(GetConnectionSource(), lmPara.class);  //建表同时建库

...加入纪录什么的

加上这一句后,再使用ormlite官方的例子,改巴改巴,应该就没问题了。

表的描述:

@DatabaseTable(tableName = "lmPara")
public class lmPara {
    @DatabaseField(generatedId = true)
    private int id;
    @DatabaseField(columnName = "paraName")
    private String paraName;
    ……


    

ormlite层:用法:


            Dao<lmPara, Integer> lmParaDao = SqliteOpenHelper.GetlmParaDao();
            List<lmPara> dblmParas = lmParaDao.queryForAll();


查询所有记录,其他类似。



搞清楚之后,上述都是废话。
0 0
原创粉丝点击