Android之ORMLite使用(1)
@DatabaseTable (tableName = "country" ) public class CountryInfo { @DatabaseField(generatedId = true) // 主键 自增 private int id; @DatabaseField(columnName = "name") // 表中列名 private String name; @DatabaseField(columnName = "no") private String no; public CountryInfo() { super(); } public CountryInfo(String name, String no) { super(); this. name = name; this. no = no; } 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 getNo() { return no; } public void setNo(String no) { this. no = no; } }
public class DBHelper extends OrmLiteSqliteOpenHelper { private static final String DB_NAME = "app_noname.db"; private static final int DB_VERSION = 1; private static DBHelper instance; private Map<String, Dao> daos = new HashMap<String, Dao>(); public DBHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); // 创建数据库 } public static synchronized DBHelper getInstance(Context context){ if ( instance == null) { synchronized (DBHelper. class) { if ( instance == null) { instance = new DBHelper(context); } } } return instance; } @Override public void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) { try { TableUtils. createTable(connectionSource, CountryInfo.class ); } catch (SQLException e) { // TODO e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3) { try { TableUtils. dropTable(arg1, CountryInfo. class, true); } catch (SQLException e) { // TODO e.printStackTrace(); } } public synchronized Dao getDao(Class clazz) throws SQLException{ Dao dao = null; String className = clazz.getSimpleName(); if ( daos.containsKey(className)) { dao = daos.get(className); } if (dao == null) { dao = super .getDao(clazz); daos.put(className, dao); } return dao; } @Override public void close() { for (String key : daos.keySet()) { Dao dao = daos .get(key); dao = null; } super.close(); } }
public class CountryDao { private Context mContext; private DBHelper mDbHelper; private Dao<CountryInfo, Integer> mCountryDao; public CountryDao(Context context) { mContext = context; mDbHelper = DBHelper. getInstance( mContext); try { mCountryDao = mDbHelper .getDao(CountryInfo. class); } catch (SQLException e) { // TODO e.printStackTrace(); } } /** * @Title : addCountry * @Description : 新增一个country信息 * @param countryInfo * @return void 返回类型 * @throws */ public void addCountry(CountryInfo countryInfo){ try { mCountryDao.create(countryInfo); System. out.println( "country数据添加成功" ); } catch (SQLException e) { // TODO e.printStackTrace(); } } public List<CountryInfo> findByNo(String value){ List<CountryInfo> countryInfos = null; try { countryInfos = mCountryDao.queryBuilder().where().eq( "no", value).query(); System. out.println( "country数据查询成功" ); } catch (SQLException e) { e.printStackTrace(); } return countryInfos; } }
CountryDao countryDao = new CountryDao( this); countryDao.addCountry(getCountryData()); List<CountryInfo> findByNos = countryDao.findByNo("12345" ); if (findByNos.size() > 0) { for (CountryInfo countryInfo : findByNos) { System. err.println(countryInfo.getName()); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。