日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

[Android学习系列8]数据库ormlite笔记

發(fā)布時(shí)間:2025/7/25 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Android学习系列8]数据库ormlite笔记 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一.下載包

http://ormlite.com/

http://ormlite.com/releases/

?

把core包 和 android包 ? build path到項(xiàng)目里

?

?

二.參考資料

http://ormlite.com/

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

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

?

?

三.自己寫的

1.寫一個(gè)類代表一個(gè)表

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; //必須提供一個(gè)無參數(shù)的構(gòu)造函數(shù),這個(gè)不能少 public Person() {} //自定義構(gòu)造函數(shù) 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方法不能漏 之前就是漏了 結(jié)果報(bào)錯(cuò)無法運(yùn)行 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目錄新建一個(gè)raw文件夾

繼承一個(gè)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這個(gè)類, ?對MyConfigUtil.java ?進(jìn)行 run as ?的配置

?

?

?

?

?

?

3.第2步完成后會(huì)在raw里面生成表的配置文件my_ormlite_config.txt,然后我們就可以寫一個(gè)DatabaseHelper加載它

? 并在里面實(shí)現(xiàn)創(chuàng)建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) { //加載數(shù)據(jù)庫 和 表的配置文件 super(context, DATABASE_NAME, null , R.raw.my_ormlite_config); } @Override public void onCreate(SQLiteDatabase db, ConnectionSource con) { try { //創(chuàng)建表 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(); //插入三條數(shù)據(jù) personDao.create(new Person("姓名1","猜猜他是誰") ); personDao.create(new Person("姓名2","猜猜他是誰") ); personDao.create(new Person("姓名s","猜猜他是誰") ); //輸出全部數(shù)據(jù) 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; } }

  

?

轉(zhuǎn)載于:https://www.cnblogs.com/sleeptothedeath/p/3681455.html

總結(jié)

以上是生活随笔為你收集整理的[Android学习系列8]数据库ormlite笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。