zoukankan      html  css  js  c++  java
  • [Android学习系列8]数据库ormlite笔记

    一.下载包

    http://ormlite.com/

    http://ormlite.com/releases/

    把core包 和 android包   build path到项目里

    二.参考资料

    http://ormlite.com/

    http://blog.csdn.net/joker_zhou/article/details/7869244

    https://www.youtube.com/watch?v=beb-n2yq0kM&hd=1

    三.自己写的

    1.写一个类代表一个表

    package com.example.test_ormlite;  
    import com.j256.ormlite.field.DatabaseField;  
      
    public class Person {  
          
        public static final String ID = "person_id";  
        public static final String Name = "person_name";  
        public static final String Info = "persin_info";  
          
        @DatabaseField(useGetSet = true , columnName = ID , generatedId = true)  
        private int id;  
          
        @DatabaseField(useGetSet = true , columnName = Name)  
        private String name;  
          
        @DatabaseField(useGetSet = true , columnName = Info)  
        private String info;  
          
        //必须提供一个无参数的构造函数,这个不能少  
        public Person() {}  
          
        //自定义构造函数  
        public Person( String name , String info) {  
            //super();  
            this.name = name;  
            this.info = info;  
        }  
          
        @Override  
        //方便输出查看  
        public String toString() {  
            return "id:" + id + " ,name:" + name + " ,info:" + info;  
        }  
      
        //get,set方法不能漏 之前就是漏了 结果报错无法运行  
        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 getInfo() {  
            return info;  
        }  
      
        public void setInfo(String info) {  
            this.info = info;  
        }  
          
    }  
    

    2.在res目录新建一个raw文件夹

    继承一个OrmLiteConfigUtil类,  用来生成配置文件

    package com.example.test_ormlite;  
      
    import java.io.IOException;  
    import java.sql.SQLException;  
      
    import com.j256.ormlite.android.apptools.OrmLiteConfigUtil;  
      
    public class MyConfigUtil extends OrmLiteConfigUtil {  
          
        public static final Class<?>[] classes = new Class[]{ Person.class };  
      
        public static void main(String[] args) throws SQLException,IOException {  
      
            writeConfigFile("my_ormlite_config.txt",classes);  
              
        }  
      
    }  
    

      

    以j2se的形式run这个类,  对MyConfigUtil.java  进行 run as  的配置

    3.第2步完成后会在raw里面生成表的配置文件my_ormlite_config.txt,然后我们就可以写一个DatabaseHelper加载它

      并在里面实现创建DAO的方法

    package com.example.test_ormlite;  
      
    import java.sql.SQLException;  
      
    import android.R.integer;  
    import android.content.Context;  
    import android.database.sqlite.SQLiteDatabase;  
      
    import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;  
    import com.j256.ormlite.dao.Dao;  
    import com.j256.ormlite.dao.RuntimeExceptionDao;  
    import com.j256.ormlite.support.ConnectionSource;  
    import com.j256.ormlite.table.TableUtils;  
      
    public class MyDatabaseHelper extends OrmLiteSqliteOpenHelper {  
          
        public static final String DATABASE_NAME = "mydatabase.db";  
        public static final int DATABASE_VERSION = 1;  
          
        private Dao<Person,Integer> personDao = null;  
        private RuntimeExceptionDao<Person, Integer> personRuntimeDao = null;  
          
        public MyDatabaseHelper(Context context) {  
            //加载数据库 和  表的配置文件  
            super(context, DATABASE_NAME,  null , R.raw.my_ormlite_config);  
        }  
          
        @Override  
        public void onCreate(SQLiteDatabase db, ConnectionSource con) {  
            try {  
                  
                //创建表  
                TableUtils.createTable(con, Person.class);  
                  
            } catch (SQLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
          
        @Override  
        public void onUpgrade(SQLiteDatabase db, ConnectionSource con, int oldVersion,  
                int newVersion) {  
            try {  
                //删除表  
                TableUtils.dropTable(con, Person.class, true);  
                //重建表  
                TableUtils.createTable(con, Person.class);  
                  
            } catch (SQLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
          
        //person类的DAO  
        public Dao<Person, Integer> getDao() throws SQLException {  
              
            if( personDao == null )  
            {  
                personDao = getDao(Person.class);  
            }  
              
            return personDao;   
                  
        }  
          
        //person类的RuntimeDao  
        public RuntimeExceptionDao<Person, Integer> getPersonRuntimeExceptionDao() {  
              
            if( personRuntimeDao == null )  
            {  
                personRuntimeDao = getRuntimeExceptionDao(Person.class);  
            }  
            return personRuntimeDao;  
              
        }  
          
    }  
    

      

    4.然后就可以在activity里面做一些测试了

    package com.example.test_ormlite;  
      
    import java.util.List;  
      
    import com.j256.ormlite.android.apptools.OpenHelperManager;  
    import com.j256.ormlite.dao.RuntimeExceptionDao;  
      
    import android.os.Bundle;  
    import android.app.Activity;  
    import android.database.DatabaseUtils;  
    import android.util.Log;  
    import android.view.Menu;  
      
    public class MainActivity extends Activity {  
      
        MyDatabaseHelper myDbHelper = null;  
          
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
              
                  
            doSomeTestWithOrmlite();  
        }  
      
        private void doSomeTestWithOrmlite() {  
              
            //建立databaseHelper  
            myDbHelper = OpenHelperManager.getHelper(this,MyDatabaseHelper.class);  
              
            //用databaseHelper 建立 dao  
            RuntimeExceptionDao<Person, Integer> personDao = myDbHelper.getPersonRuntimeExceptionDao();  
              
            //插入三条数据  
            personDao.create(new Person("姓名1","猜猜他是谁") );  
            personDao.create(new Person("姓名2","猜猜他是谁") );  
            personDao.create(new Person("姓名s","猜猜他是谁") );  
              
            //输出全部数据  
            List<Person>  list_person = personDao.queryForAll();  
            Log.d( "mytag", list_person.toString() );  
              
              
            //释放helper  
            OpenHelperManager.releaseHelper();  
      
              
        }  
      
        @Override  
        public boolean onCreateOptionsMenu(Menu menu) {  
            // Inflate the menu; this adds items to the action bar if it is present.  
            getMenuInflater().inflate(R.menu.main, menu);  
            return true;  
        }  
      
    }  
    

      

  • 相关阅读:
    AngularJS入门教程
    mobile web retina 下 1px 边框解决方案
    Python字典猜解
    20145313exp9
    20145313张雪纯Exp8 Web基础
    20145313张雪纯exp7
    20145313张雪纯信息搜集与漏洞扫描
    计算机病毒静态分析2
    计算机病毒静态分析1
    20145313张雪纯MSF基础应用实验
  • 原文地址:https://www.cnblogs.com/sleeptothedeath/p/3681455.html
Copyright © 2011-2022 走看看