ORMLite的使用

首先需要下载core和android两个jar

http://ormlite.com/releases/

然后拷贝到libs文件夹下并且添加为库

具体的使用看标程:

School.java

 1 package com.turtle920.ormlitedemo;
 2 
 3 import com.j256.ormlite.field.DatabaseField;
 4 import com.j256.ormlite.table.DatabaseTable;
 5 
 6 @DatabaseTable(tableName = "school")
 7 public class School {
 8 
 9     @DatabaseField(generatedId=true)
10     private int id;
11 
12     @DatabaseField(columnName = "name")
13     private String name;
14 
15     @DatabaseField
16     private String location;
17 
18     public int getId() {
19         return id;
20     }
21 
22     public void setId(int id) {
23         this.id = id;
24     }
25 
26     public String getName() {
27         return name;
28     }
29 
30     public void setName(String name) {
31         this.name = name;
32     }
33 
34     public String getLocation() {
35         return location;
36     }
37 
38     public void setLocation(String location) {
39         this.location = location;
40     }
41 }

以上通过注释符来用框架反射table和field,(generatedId=true)表示自增长

DBHelper.java

 1 package com.turtle920.ormlitedemo;
 2 
 3 import android.content.Context;
 4 import android.database.sqlite.SQLiteDatabase;
 5 
 6 import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
 7 import com.j256.ormlite.dao.Dao;
 8 import com.j256.ormlite.support.ConnectionSource;
 9 import com.j256.ormlite.table.TableUtils;
10 
11 import java.sql.SQLException;
12 
13 public class DBHelper extends OrmLiteSqliteOpenHelper {
14 
15 
16     private final static int DATABASE_VERSION = 1;
17     Dao<School, Integer> mSchoolDao;
18     private static final String DB_NAME = "orm";
19     private static DBHelper mDbHelper;
20 
21     private DBHelper(Context context) {
22         super(context, DB_NAME, null, DATABASE_VERSION);
23     }
24 
25 
26     public static DBHelper getInstance(Context context) {
27         if (mDbHelper == null) {
28             mDbHelper = new DBHelper(context);
29         }
30         return mDbHelper;
31     }
32 
33     @Override
34     public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
35         try {
36             TableUtils.createTableIfNotExists(connectionSource, School.class);
37         } catch (SQLException e) {
38             e.printStackTrace();
39         }
40     }
41 
42 
43     @Override
44     public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
45                           int arg3) {
46     }
47 
48     public Dao<School, Integer> getSchoolDao() throws SQLException {
49         if (mSchoolDao == null) {
50             mSchoolDao = getDao(School.class);
51         }
52         return mSchoolDao;
53     }
54 
55 
56 }

这个类继承自OrmLiteSqliteOpenHelper.class继承自SqliteOpenHelper.class,相当于是增强版的

MainActivity.java

 1 package com.turtle920.ormlitedemo;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.util.Log;
 6 
 7 import com.j256.ormlite.dao.Dao;
 8 
 9 import java.sql.SQLException;
10 import java.util.List;
11 
12 public class MainActivity extends AppCompatActivity {
13 
14     private static final String TAG = "DaoDaoDao";
15     DBHelper mDbHelper;
16     Dao<School, Integer> mSchoolDao;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         mDbHelper = DBHelper.getInstance(this);
23         try {
24             mSchoolDao = mDbHelper.getSchoolDao();
25         } catch (SQLException e) {
26             Log.e(TAG, "constructor exception", e);
27         }
28         testDao();
29     }
30 
31     private void testDao() {
32 
33         School school1 = new School();
34         school1.setName("university");
35         school1.setLocation("shanghai");
36 
37         School school2 = new School();
38         school2.setName("middle school");
39         school2.setLocation("hubei");
40 
41         School school3 = new School();
42         school3.setName("CDQZ");
43         school3.setLocation("CHENGDU");
44 
45         try {
46             mSchoolDao.create(school1);
47             mSchoolDao.create(school2);
48             mSchoolDao.create(school3);
49             //获取表中所有的student。
50             Log.e(TAG,"************BEGIN*************");
51             List<School> schools=mSchoolDao.queryForAll();
52             Log.e(TAG, "before delete school1 size is:" + schools.size());
53             for (int i = 0; i < schools.size(); i++) {
54                 Log.e(TAG, schools.get(i).getId()+" "+schools.get(i).getName()+" "+schools.get(i).getLocation());
55             }
56             mSchoolDao.delete(school1);
57 
58             //schools=mSchoolDao.queryForAll();
59             schools=mSchoolDao.queryForEq("id","3");
60             Log.e(TAG, "after delete school1 list size is:"+schools.size());
61             for (int i = 0; i < schools.size(); i++) {
62                 Log.e(TAG, schools.get(i).getId()+" "+schools.get(i).getName()+" "+schools.get(i).getLocation());
63             }
64 
65         } catch (SQLException e) {
66             e.printStackTrace();
67         }
68     }
69 
70 }

其中一些操作:

1 .queryForEq("id","3");//查询“id”=“3”的
2 .queryForAll();//所有
3 .delete(school1);//删除
4 .deleteById(int id);//按id删
5 .update(school);//更新    
6 .create(school);//插入

可以看到对数据库的访问被投影成了对对象的操作,相比直接用android给的api操作数据库要短平快的多。

 
原文地址:https://www.cnblogs.com/turtle920/p/4870176.html