MainActivity
package com.example.ormlite;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context context=this;
Button create = (Button) findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyCreateRawDataTask(context).execute();
}
});
Button query = (Button) findViewById(R.id.query);
query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyQueryTask(context).execute();
}
});
}
}
MyAppService
package com.example.ormlite;
import com.j256.ormlite.dao.Dao;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyAppService extends Service{
private Dao<Student, Integer> mStudentDao;
private Dao<AClass, Integer> mClassDao;
private ORMLiteDatabaseHelper mDatabaseHelper;
@Override
public void onCreate(){
ORMLiteDatabaseHelper mDatabaseHelper = ORMLiteDatabaseHelper.getInstance(this);
mStudentDao = mDatabaseHelper.getStudentDao();
mClassDao = mDatabaseHelper.getClassDao();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable(){
@Override
public void run() {
createRawData();
}}).start();
return super.onStartCommand(intent, flags, startId);
}
private void createRawData(){
Log.d("数据库","开始创建数据 ...");
for (int j = 0; j < 3; j++) {
// 创建1个班级用以演示。
AClass acls = new AClass();
acls.class_id = j;
acls.name = j + "班";
try {
mClassDao.createIfNotExists(acls);
} catch (Exception e) {
e.printStackTrace();
}
// 创建5个学生并且挂接到1班
for (int i = 0; i < 5; i++) {
Student s = new Student();
s.student_id = i;
s.age = 18 + i;
s.name = "张" + i;
s.sex = "男";
// 开始,建立外键,挂接到1班
s.aclass = acls;
try {
mStudentDao.createIfNotExists(s);
} catch (Exception e) {
}
}
}
Log.d("数据库","创建数据完成.");
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy(){
super.onDestroy();
mDatabaseHelper.close();
}
}
MyCreateRawDataTask
package com.example.ormlite;
import com.j256.ormlite.dao.Dao;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
public class MyCreateRawDataTask extends AsyncTask{
private Context context;
public MyCreateRawDataTask(Context ctx){
this.context=ctx;
}
@Override
protected void onPreExecute() {
}
@Override
protected Object doInBackground(Object... params) {
createRawData();
return null;
}
@Override
protected void onPostExecute(Object result) {
}
private void createRawData() {
ORMLiteDatabaseHelper mDatabaseHelper = ORMLiteDatabaseHelper
.getInstance(context);
Dao<Student, Integer> mStudentDao = mDatabaseHelper.getStudentDao();
Dao<AClass, Integer> mClassDao = mDatabaseHelper.getClassDao();
Log.d("数据库", "开始创建数据 ...");
for (int j = 0; j < 3; j++) {
// 创建1个班级用以演示。
AClass acls = new AClass();
acls.class_id = j;
acls.name = j + "班";
try {
mClassDao.createIfNotExists(acls);
} catch (Exception e) {
e.printStackTrace();
}
// 创建5个学生并且挂接到1班
for (int i = 0; i < 5; i++) {
Student s = new Student();
s.student_id = i;
s.age = 18 + i;
s.name = "张" + i;
s.sex = "男";
// 开始,建立外键,挂接到1班
s.aclass = acls;
try {
mStudentDao.createIfNotExists(s);
} catch (Exception e) {
}
}
}
Log.d("数据库", "创建数据完成.");
//mDatabaseHelper.close();
}
}
MyQueryTask
package com.example.ormlite;
import java.sql.SQLException;
import java.util.List;
import com.j256.ormlite.dao.Dao;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
public class MyQueryTask extends AsyncTask{
private Context context;
public MyQueryTask(Context ctx){
this.context=ctx;
}
@Override
protected void onPreExecute() {
}
@Override
protected Object doInBackground(Object... params) {
query();
return null;
}
@Override
protected void onPostExecute(Object result) {
}
private void query() {
ORMLiteDatabaseHelper mDatabaseHelper = ORMLiteDatabaseHelper.getInstance(context);
Dao<Student, Integer> mStudentDao = mDatabaseHelper.getStudentDao();
Dao<AClass, Integer> mClassDao = mDatabaseHelper.getClassDao();
try {
List<Student> students = mStudentDao.queryForAll();
for (Student s : students) {
Log.d("学生", s.name + "," + s.age + ",归属于:" + s.aclass.name);
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
List<AClass> classes = mClassDao.queryForAll();
for (AClass c : classes) {
Log.d("班级", c.name + "," + "," + c.class_id + ",学生个数:"
+ c.students.size());
}
} catch (SQLException e) {
e.printStackTrace();
}
//mDatabaseHelper.close();
}
}
ORMLiteDatabaseHelper
package com.example.ormlite;
import java.sql.SQLException;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
public class ORMLiteDatabaseHelper extends OrmLiteSqliteOpenHelper {
private static ORMLiteDatabaseHelper mDatabaseHelper = null;
private Dao<Student, Integer> mStudentDao = null;
private Dao<AClass, Integer> mClassDao = null;
private final static String DataBase_NAME = "ormlite.db";
private final static int DataBase_VERSION = 1;
public ORMLiteDatabaseHelper(Context context, String databaseName,
CursorFactory factory, int databaseVersion) {
super(context, databaseName, factory, databaseVersion);
}
public static ORMLiteDatabaseHelper getInstance(Context context) {
if (mDatabaseHelper == null) {
mDatabaseHelper = new ORMLiteDatabaseHelper(context, DataBase_NAME,
null, DataBase_VERSION);
}
return mDatabaseHelper;
}
@Override
public void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) {
Log.d(this.getClass().getName(), "ORMLite数据库 -> onCreate");
try {
TableUtils.createTableIfNotExists(connectionSource, AClass.class);
TableUtils.createTableIfNotExists(connectionSource, Student.class);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource arg1,
int arg2, int arg3) {
}
public Dao<AClass, Integer> getClassDao() {
if (mClassDao == null) {
try {
mClassDao = getDao(AClass.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
return mClassDao;
}
public Dao<Student, Integer> getStudentDao() {
if (mStudentDao == null) {
try {
mStudentDao = getDao(Student.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
return mStudentDao;
}
@Override
public void close() {
super.close();
mStudentDao = null;
mClassDao = null;
}
}