码迷,mamicode.com
首页 > 数据库 > 详细

关系数据库ORMlite的用法;

时间:2015-11-11 19:21:50      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:

首先要在 http://ormlite.com/releases 页面下载两个jar 包:

(1)core列表下的jar包; (2)android列表下的jar包。

将上面的两个jar包下载后放到Android工程项目中的libs包中。

在创建一个java类来存放数据,

 1 package zhangphil.ormlitetest.database;
 2 
 3 import com.j256.ormlite.field.DatabaseField;
 4 import com.j256.ormlite.table.DatabaseTable;
 5 
 6 @DatabaseTable(tableName = "users")
 7 public class User {
 8 
 9     public final static String USER_ID = "user_id";
10     public final static String NAME = "name";
11     public final static String AGE = "age";
12 
13     public User() {
14 
15     }
16 
17     public User(String name, int age) {
18         this.name = name;
19         this.age = age;
20     }
21 
22     // @DatabaseField(generatedId = true)
23     // public int id;
24 
25     @DatabaseField(id = true, columnName = USER_ID)
26     public int user_id;
27 
28     @DatabaseField(columnName = NAME)
29     public String name;
30 
31     @DatabaseField(columnName = AGE)
32     public int age;
33 
34     public int getUserId() {
35         return user_id;
36     }
37 
38     public void setUserId(int uid) {
39         this.user_id = uid;
40     }
41 
42     public String getName() {
43         return name;
44     }
45 
46     public void setName(String name) {
47         this.name = name;
48     }
49 
50     public int getAge() {
51         return age;
52     }
53 
54     public void setAge(int age) {
55         this.age = age;
56     }
57 
58     @Override
59     public String toString() {
60         return "user_id:" + user_id + " 姓名:" + name + " 年龄:" + age;
61     }
62 }

创建一个ORMLite数据库管理工具类:

 1 2 
 3 import java.sql.SQLException;
 4 
 5 import android.content.Context;
 6 import android.database.sqlite.SQLiteDatabase;
 7 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 8 import android.util.Log;
 9 
10 import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
11 import com.j256.ormlite.dao.Dao;
12 import com.j256.ormlite.support.ConnectionSource;
13 import com.j256.ormlite.table.TableUtils;
14 
15 public class ORMLiteDatabaseHelper extends OrmLiteSqliteOpenHelper {
16 
17     private static ORMLiteDatabaseHelper mDatabaseHelper = null;
18     private Dao<User, Integer> mUserDao = null;
19 
20     private final static String DataBase_NAME = "ormlite.db";
21     private final static int DataBase_VERSION = 1;
22 
23     public ORMLiteDatabaseHelper(Context context, String databaseName,
24             CursorFactory factory, int databaseVersion) {
25         super(context, DataBase_NAME, factory, DataBase_VERSION);
26     }
27 
28     public static ORMLiteDatabaseHelper getInstance(Context context) {
29         if (mDatabaseHelper == null) {
30             mDatabaseHelper = new ORMLiteDatabaseHelper(context, DataBase_NAME,
31                     null, DataBase_VERSION);
32         }
33 
34         return mDatabaseHelper;
35     }
36 
37     @Override
38     public void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) {
39 
40         Log.d(this.getClass().getName(), "ORMLite数据库 -> onCreate");
41 
42         try {
43             TableUtils.createTableIfNotExists(connectionSource, User.class);
44         } catch (Exception e) {
45             e.printStackTrace();
46         }
47     }
48 
49     @Override
50     public void onUpgrade(SQLiteDatabase database, ConnectionSource arg1,
51             int arg2, int arg3) {
52 
53         Log.i(this.getClass().getName(), "数据库 -> onUpgrade");
54 
55         try {
56             // 删除旧的数据库表。
57             TableUtils.dropTable(connectionSource, User.class, true);
58 
59             // 重新创建新版的数据库。
60             onCreate(database, connectionSource);
61         } catch (SQLException e) {
62             e.printStackTrace();
63         }
64     }
65 
66     /**
67      * 每一个数据库中的表,要有一个获得Dao的方法。 可以使用一种更通用的模板方法如:
68      * 
69      * public Dao<Class, Integer> getORMLiteDao(Class cls) throws SQLException {
70      * if (dao == null) { dao = getDao(cls); }
71      * 
72      * return dao; }
73      */
74     public Dao<User, Integer> getUserDao() {
75         if (mUserDao == null) {
76             try {
77                 mUserDao = getDao(User.class);
78             } catch (java.sql.SQLException e) {
79                 e.printStackTrace();
80             }
81         }
82 
83         return mUserDao;
84     }
85 
86     @Override
87     public void close() {
88         super.close();
89         mUserDao = null;
90     }
91 }


最后在mainactivity中操作添加数据:

 1 import java.sql.SQLException;
 2 import java.util.List;
 3 import java.util.Random;
 4 
 5 import com.j256.ormlite.dao.Dao;
 6 
 7 import zhangphil.ormlitetest.database.ORMLiteDatabaseHelper;
 8 import zhangphil.ormlitetest.database.User;
 9 import android.support.v7.app.ActionBarActivity;
10 import android.widget.Toast;
11 import android.os.Bundle;
12 
13 public class MainActivity extends ActionBarActivity {
14 
15     private Dao<User, Integer> mUserDao;
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20 
21         ORMLiteDatabaseHelper mDatabaseHelper = ORMLiteDatabaseHelper
22                 .getInstance(this);
23         mUserDao = mDatabaseHelper.getUserDao();
24 
25         Random rand = new Random();
26         for (int i = 0; i < 3; i++) {
27             User user = new User();
28             user.setUserId(i);
29             user.setName("name" + i);
30             // 生成随机测试的年龄。
31             user.setAge(rand.nextInt(100));
32 
33             try {
34                 mUserDao.createOrUpdate(user);
35             } catch (SQLException e) {
36                 e.printStackTrace();
37             }
38         }
39     }
40 
41     @Override
42     public void onStart() {
43         super.onStart();
44 
45         // 全局查询
46         try {
47             List<User> users = mUserDao.queryForAll();
48 
49             for (User u : users) {
50                 Toast.makeText(this, u.toString(), Toast.LENGTH_SHORT).show();
51             }
52         } catch (SQLException e) {
53             e.printStackTrace();
54         }
55 
56         // 条件查询QueryBuilder
57         // 假设我们给定一个用户的id=1
58         int uid = 1;
59         try {
60             List<User> users = mUserDao.queryBuilder().where()
61                     .eq(User.USER_ID, uid).query();
62 
63             for (User u : users) {
64                 Toast.makeText(this, "查询结果 : " + u.toString(),
65                         Toast.LENGTH_SHORT).show();
66             }
67         } catch (SQLException e) {
68             e.printStackTrace();
69         }
70     }
71 }

 

关系数据库ORMlite的用法;

标签:

原文地址:http://www.cnblogs.com/labixiaoxin/p/4956721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!