• Android中使用ormlite实现持久化--HelloOrmLite



    Android中内置了sqlite,但是常用的开发语言java是面向对象的,而数据库是关系型的,二者之间的转化每次都很麻烦(主要是我对sql语言不熟悉)。而Java Web开发中有很多orm框架,但是想直接放到Android上用有些麻烦。尝试了一下找Android的orm框架,说实话还有好几个。


    实现考虑的是:androrm
    说实话,这个我实在没有弄懂,一共两个包。
    一个是依赖包:Apache Commons - Lang (2.6)
    另外一个就是主包:androrm.jar   不管怎么下载的都不能使用...

    然后有考虑了一下db4o
    官网上的介绍说是已经支持Android了,但是我一是觉得包有点大,而是觉得速度有点慢

    最后看到的就是ormlite
    一共两个包:一个是ormlite-core-4.24.jar,另一个是ormlite-android-4.24.jar
    从以下网址可以下载到:http://ormlite.com/releases/
     
    下面按照惯例来个Hello world
    新建Android项目:HelloOrmLite

      
    添加文件夹:libs,将所需的两个包复制到其中。添加引用

    新建一个model:Hello.java
     
    复制代码
    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
    复制代码
     
    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中添加对数据库的操作
     
    代码如下:
     
    复制代码
    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() + " " + "添加数据完成");
    // 查询添加的数据
    List<Hello> hellos = helloDao.queryForAll();
    for (Hello h : hellos) {
    tv.setText(tv.getText() + " " + h.toString());
    }
    // 删除数据第一条数据
    helloDao.delete(hellos.get(0));
    tv.setText(tv.getText() + " " + "删除数据完成");
    // 重新查询数据
    hellos = helloDao.queryForAll();
    for (Hello h : hellos) {
    tv.setText(tv.getText() + " " + h.toString());
    }
    // 修改数据
    Hello h1 = hellos.get(0);
    h1.setWord("这是修改过的数据");
    tv.setText(tv.getText() + " " + "修改数据完成");
    helloDao.update(h1);
    // 重新查询数据
    hellos = helloDao.queryForAll();
    for (Hello h : hellos) {
    tv.setText(tv.getText() + " " + h.toString());
    }

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    }

    复制代码
    以上实现了数据库操作相关的增删改,下面是效果:
     

      
     
       OrmLite的功能非常强大,Model类的声明中非常重要,外键约束,非空检查等等问题都有相对的处理方法。
  • 相关阅读:
    初入angularJS [2]
    初入angularJS [1]
    ubuntu13.10 nginx
    Session对象详解[源于网络]
    二、Python变量
    一、计算机硬件及操作系统
    python进阶之装饰器之3如何利用装饰器强制函数上的类型检查
    python基础之闭包函数
    python进阶之装饰器之2.定义一个可接受参数的装饰器、如何定义一个属性可由用户修改的装饰器、定义一个能接受可选参数的装饰器
    python进阶之装饰器之1.如何定义一个基本的装饰器并使用,保留装饰器的元数据(原信息),逆向解得函数原信息
  • 原文地址:https://www.cnblogs.com/zsw-1993/p/4879775.html
Copyright © 2020-2023  润新知