• 基于ormlite创建数据库存储数据案例


    一直不知道安卓创建数据库存储数据,以前遇到过,但是没有深入研究,今天仔细的看了一下,学习到了一点知识

    直接看代码了

     1 public class DatabaseHelper extends OrmLiteSqliteOpenHelper{
     2 
     3     private static final String TABLE_NAME = "pspsp.db";
     4     private static final int DB_VERSION = 3;
     5 
     6     private Map<String, Dao> daos = new HashMap<String, Dao>();
     7 
     8     private DatabaseHelper(Context context)
     9     {
    10         super(context, TABLE_NAME, null, DB_VERSION);
    11     }
    12     @Override
    13     public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    14         try
    15         {
    16             TableUtils.createTable(connectionSource, SearchHistroy.class);
    17          
    18         } catch (SQLException e)
    19         {
    20             e.printStackTrace();
    21         }
    22     }
    23 
    24     @Override
    25     public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
    26         try
    27         {
    28             TableUtils.dropTable(connectionSource, SearchHistroy.class, true);
    29            
    30         } catch (SQLException e)
    31         {
    32             e.printStackTrace();
    33         }
    34     }
    35 
    36     private static DatabaseHelper instance;
    37 
    38     /**
    39      * 单例获取该Helper
    40      *
    41      * @param context
    42      * @return
    43      */
    44     public static synchronized DatabaseHelper getHelper(Context context)
    45     {
    46         context = context.getApplicationContext();
    47         if (instance == null)
    48         {
    49             synchronized (DatabaseHelper.class)
    50             {
    51                 if (instance == null)
    52                     instance = new DatabaseHelper(context);
    53             }
    54         }
    55 
    56         return instance;
    57     }
    58 
    59     public synchronized Dao getDao(Class clazz) throws SQLException
    60     {
    61         Dao dao = null;
    62         String className = clazz.getSimpleName();
    63 
    64         if (daos.containsKey(className))
    65         {
    66             dao = daos.get(className);
    67         }
    68         if (dao == null)
    69         {
    70             dao = super.getDao(clazz);
    71             daos.put(className, dao);
    72         }
    73         return dao;
    74     }
    75 
    76     /**
    77      * 释放资源
    78      */
    79     @Override
    80     public void close()
    81     {
    82         super.close();
    83 
    84         for (String key : daos.keySet())
    85         {
    86             Dao dao = daos.get(key);
    87             dao = null;
    88         }
    89     }
    90 
    91 }
     1 @DatabaseTable(tableName = "search_history")
     2 public class SearchHistroy {
     3 
     4     @DatabaseField(generatedId = true)
     5     private int id;
     6 
     7     @DatabaseField
     8     private String name;
     9 
    10     private boolean isDelete=false;
    11 
    12     public int getId() {
    13         return id;
    14     }
    15 
    16     public void setId(int id) {
    17         this.id = id;
    18     }
    19 
    20     public String getName() {
    21         return name;
    22     }
    23 
    24     public void setName(String name) {
    25         this.name = name;
    26     }
    27 
    28     public boolean isDelete() {
    29         return isDelete;
    30     }
    31 
    32     public void setIsDelete(boolean isDelete) {
    33         this.isDelete = isDelete;
    34     }
    35 
    36     //    @DatabaseField(canBeNull = true, foreign = true, columnName = "user_id")
    37 //    private User user;
    38 }

    然后在dao中运用

     1 public class SearchDao {
     2 
     3     private Context context;
     4     private Dao<SearchHistroy, Integer> userDaoOpe;
     5     private DatabaseHelper helper;
     6 
     7     public SearchDao(Context context) {
     8         this.context = context;
     9         try {
    10             helper = DatabaseHelper.getHelper(context);
    11             userDaoOpe = helper.getDao(SearchHistroy.class);
    12         } catch (SQLException e) {
    13             e.printStackTrace();
    14         }
    15     }
    16     /**
    17      * 增加一个用户
    18      *
    19      * @param model
    20      */
    21     public void add(SearchHistroy model) {
    22         try {
    23 
    24 
    25             SearchHistroy searchHistroy = userDaoOpe.queryBuilder().where().eq("name", model.getName()).queryForFirst();
    26             if (searchHistroy == null || StringUtils.isEmpty(searchHistroy.getName()) && !StringUtils.isEmpty(model.getName())) {
    27                 userDaoOpe.create(model);
    28             }
    29         } catch (SQLException e) {
    30             e.printStackTrace();
    31         }
    32 
    33     }
    34 
    35 
    36     /**
    37      * 清楚所有
    38      */
    39     public void deteAll() {
    40         try {
    41             userDaoOpe.deleteBuilder().delete();
    42         } catch (SQLException e) {
    43             e.printStackTrace();
    44         }
    45 
    46     }
    47 
    48     /**
    49      * 获取所有查询记录
    50      *
    51      * @return
    52      */
    53     public List<SearchHistroy> getAllList() {
    54         try {
    55             return userDaoOpe.queryForAll();
    56         } catch (SQLException e) {
    57             e.printStackTrace();
    58             return new ArrayList<SearchHistroy>();
    59         }
    60     }
    61 }

    具体运用

     1 searchDao = new SearchDao(this);
     2         list = new ArrayList<SearchHistroy>();
     3 //获取数据
     4  list = searchDao.getAllList();
     5 
     6 
     7 
     8 //添加数据
     9  SearchHistroy histroy = new SearchHistroy();
    10                 histroy.setName(strValue);
    11                 searchDao.add(histroy);
    12 
    13 //删除数据
    14 searchDao.deteAll();
  • 相关阅读:
    notepad++ remove duplicate line
    notepad++ $ ^
    中文名: 交通事故责任认定, 英文名称: Traffic accident responsibility identification
    java 反射 获取Class对象的三种方式
    [转]java中byte转换int时为何与0xff进行与运算
    java中byte取值范围为什么是 -128到127
    bytes2HexString
    DBCS 从256开始
    android开发之使用拼音搜索汉字
    电脑故障,路由器及网络
  • 原文地址:https://www.cnblogs.com/wangying222/p/8489049.html
Copyright © 2020-2023  润新知