標籤:
在這裡記錄下最基本的用法,官網上可瞭解相關的介紹。
1、下載OrmLite jar
在下載android的:ormlite-android-4.48.jar和ormlite-core-4.48.jar,放在你項目的libs目錄下。
2、編寫Bean類
package com.example.ormlite.bean;import java.util.Collection;import com.j256.ormlite.field.DatabaseField;import com.j256.ormlite.field.ForeignCollectionField;import com.j256.ormlite.table.DatabaseTable;@DatabaseTable(tableName="tb_user") //表明這是資料庫中的一張表public class User { @DatabaseField(generatedId=true) //generatedId表示id為主鍵切自動產生 private int id; @DatabaseField(columnName="name") //columnName的值為該欄位在資料庫表中的列名 private String name; @DatabaseField(columnName="desc") private String desc; public User() { } public User(String name, String desc) { this.name = name; this.desc = desc; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", desc=" + desc + "]"; } }
3.編寫Dao類
package com.example.ormlite.db;import java.sql.SQLException;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import com.example.ormlite.bean.User;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;/** * 編寫DAO類 * 原生的資料庫操作,需要繼承SQLiteOpenHelper,這裡我們需要繼承OrmLiteSqliteOpenHelper(間接繼承了SQLiteOpenHelper) * @author Administrator * */public class DatabaseHelper extends OrmLiteSqliteOpenHelper{ private static final String TABLE_NAME="sqlite-test.db"; private static DatabaseHelper instance; //userDao,每張表對應一個 private Dao<User,Integer> userDao; public DatabaseHelper(Context context) { super(context, TABLE_NAME, null, 2); } /** * 建表 */ @Override public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource, User.class); } catch (SQLException e) { e.printStackTrace(); } } /** * 更新表 */ @Override public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) { try { TableUtils.dropTable(connectionSource, User.class, true); onCreate(database, connectionSource); } catch (SQLException e) { e.printStackTrace(); } } /** * 單例擷取該Helper * @param context * @return */ public static synchronized DatabaseHelper getHelper(Context context){ if(instance==null){ synchronized(DatabaseHelper.class){ if(instance==null){ instance=new DatabaseHelper(context); } } } return instance; } /** * 獲得userDao * 我們會有很多表,每個表一般我們都會單獨寫個Dao用於操作,這裡為了簡單我並沒有抽取出來,直接寫在helper中,比如UserDao的擷取 * @return * @throws SQLException */ public Dao<User,Integer> getUserDao() throws SQLException{ if(userDao==null){ userDao=getDao(User.class); } return userDao; } /** * 釋放資源 */ @Override public void close() { super.close(); userDao=null; }}
4.測試
關於單元測試的配置可參考Android Junit測試架構
package com.example.ormlite.test;import java.sql.SQLException;import java.util.List;import android.test.AndroidTestCase;import android.util.Log;import com.example.ormlite.bean.User;import com.example.ormlite.dao.UserDao;import com.example.ormlite.db.DatabaseHelper;public class OrmLiteDbTest extends AndroidTestCase{ public void testAddUser(){// User u1=new User("yanqiong", "developer");// DatabaseHelper helper=DatabaseHelper.getHelper(getContext());// try {// helper.getUserDao().create(u1);// u1=new User("yanqiong2", "developer");// helper.getUserDao().create(u1);// u1=new User("yanqiong3", "developer");// helper.getUserDao().create(u1);// u1=new User("yanqiong4", "developer");// helper.getUserDao().create(u1);// // testList();// } catch (SQLException e) {// e.printStackTrace();// } User u1=new User("yanqiong5", "developer"); UserDao userDao=new UserDao(getContext()); userDao.add(u1); } public void testList() { DatabaseHelper helper=DatabaseHelper.getHelper(getContext()); User u1=new User("yanqiong-android", "developer"); u1.setId(2); List<User> users; try { users = helper.getUserDao().queryForAll(); Log.i("OrmLiteDbTest", users.toString()); } catch (SQLException e) { e.printStackTrace(); } } public void testDeleteUser(){ DatabaseHelper helper=DatabaseHelper.getHelper(getContext()); try { helper.getUserDao().deleteById(2); } catch (SQLException e) { e.printStackTrace(); } } public void testUpdateUser(){ DatabaseHelper helper=DatabaseHelper.getHelper(getContext()); User u1=new User("yanqiong-android", "developer"); u1.setId(3); try { helper.getUserDao().update(u1); } catch (SQLException e) { e.printStackTrace(); } }}
規範的寫法請參考Android 資料庫架構OrmLite的使用(二)
Android 資料庫架構OrmLite的使用(一)