[Android学习系列8]数据库ormlite笔记
生活随笔
收集整理的這篇文章主要介紹了
[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; } }
?
轉載于:https://www.cnblogs.com/sleeptothedeath/p/3681455.html
總結
以上是生活随笔為你收集整理的[Android学习系列8]数据库ormlite笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sql server Always On
- 下一篇: iOS项目结构目录