一.下载包
把core包 和 android包 build path到项目里
二.参考资料
http://blog.csdn.net/joker_zhou/article/details/7869244
https://www.youtube.com/watch?v=beb-n2yq0kM&hd=1
三.自己写的
1.写一个类代表一个表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 |
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类, 用来生成配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
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的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 |
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里面做一些测试了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 |
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 ; } } |
[Android数据库2]ormlite笔记,布布扣,bubuko.com
原文:http://www.cnblogs.com/sleeptothedeath/p/3681455.html