本博客文章中曾經提到過Ormlite的第三方組件包,Ormlite 是一種ORM工具,并且是一種輕量級別的工具。我們可以使用它來對Android中內嵌的sqlite數據庫進行相關的操作。Android 的應用程序應使用 Ormlite for android 版本來進行相關的開發。Ormlite是對android提供的sqlite部分的API進行了封裝。提供了更加方便的接口來供使用。
本文以一個學生的信息實例程序來展示如何使用ormlite for android的第三方組件來開發Sqlite的C[增加],R[查詢],U[更新],D[查詢]應用程序,以便更方便的對sqlite數據庫的操作。我們先看下程序的結構圖:
【1】.程序結構圖如下:

其中包com.andyidea.bean下Student.java為實體類,包com.andyidea.db下DatabaseHelper.java為數據庫輔助類,包com.andyidea.ormsqlite下的MainActivity.java和StudentListActivity.java是界面信息類。同時我們別忘了在根目錄下創建一個lib的文件夾,把第三方組件包ormlite-android-4.31.jar ,ormlite-core-4.31.jar,ormlite-jdbc-4.31.jar放到lib文件夾下,然后在項目中引用這三個包就OK了。
【2】布局文件源碼如下:
main.xml源碼:
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip">
<TextView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center" android:text="ORMLite-AddPage"/>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:padding="1dip" android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="學號: "/>
<EditText
android:id="@+id/stuno"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:padding="1dip" android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名: "/>
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:padding="1dip" android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年齡: "/>
<EditText
android:id="@+id/age"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:padding="1dip" android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性別: "/>
<EditText
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:padding="1dip" android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分數: "/>
<EditText
android:id="@+id/score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:padding="1dip" android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="地址: "/>
<EditText
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
~~~
students.xml源碼:
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ORMLite-Students"/>
<ListView
android:id="@+id/stulist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
~~~
studentitem.xml源碼:
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/itemno"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="學號"
android:gravity="center"/>
<TextView
android:id="@+id/itemname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="姓名"
android:gravity="center"/>
<TextView
android:id="@+id/itemscore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="分數"
android:gravity="center"/>
</LinearLayout>
~~~
【3】包com.andyidea.bean下Student.java源碼:
~~~
package com.andyidea.bean;
import java.io.Serializable;
import com.j256.ormlite.field.DatabaseField;
public class Student implements Serializable {
private static final long serialVersionUID = -5683263669918171030L;
@DatabaseField(id=true)
private String stuNO;
@DatabaseField
private String name;
@DatabaseField
private int age;
@DatabaseField
private String sex;
@DatabaseField
private double score;
@DatabaseField
private String address;
public String getStuNO() {
return stuNO;
}
public void setStuNO(String stuNO) {
this.stuNO = stuNO;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
~~~
【4】包com.andyidea.db下DatabaseHelper.java源碼:
~~~
package com.andyidea.db;
import java.sql.SQLException;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.andyidea.bean.Student;
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 DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "ormlite.db";
private static final int DATABASE_VERSION = 1;
private Dao<Student,Integer> stuDao = null;
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/**
* 創建SQLite數據庫
*/
@Override
public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, Student.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Unable to create datbases", e);
}
}
/**
* 更新SQLite數據庫
*/
@Override
public void onUpgrade(
SQLiteDatabase sqliteDatabase,
ConnectionSource connectionSource,
int oldVer,
int newVer) {
try {
TableUtils.dropTable(connectionSource, Student.class, true);
onCreate(sqliteDatabase, connectionSource);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(),
"Unable to upgrade database from version " + oldVer + " to new "
+ newVer, e);
}
}
public Dao<Student,Integer> getStudentDao() throws SQLException{
if(stuDao == null){
stuDao = getDao(Student.class);
}
return stuDao;
}
}
~~~
【5】包com.andyidea.ormsqlite下源碼:
MainActivity.java源碼:
~~~
package com.andyidea.ormsqlite;
import java.sql.SQLException;
import com.andyidea.bean.Student;
import com.andyidea.db.DatabaseHelper;
import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class MainActivity extends OrmLiteBaseActivity<DatabaseHelper> {
private EditText stuNO;
private EditText stuName;
private EditText stuAge;
private EditText stuSex;
private EditText stuScore;
private EditText stuAddress;
private Student mStudent;
private Dao<Student,Integer> stuDao;
private final int MENU_ADD = Menu.FIRST;
private final int MENU_VIEWALL = Menu.FIRST+1;
private final int MENU_EDIT = Menu.FIRST+2;
private Bundle mBundle = new Bundle();
/**Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeViews();
}
/**
* 初始化UI界面
*/
private void initializeViews(){
stuNO = (EditText)findViewById(R.id.stuno);
stuName = (EditText)findViewById(R.id.name);
stuAge = (EditText)findViewById(R.id.age);
stuSex = (EditText)findViewById(R.id.sex);
stuScore = (EditText)findViewById(R.id.score);
stuAddress = (EditText)findViewById(R.id.address);
mBundle = getIntent().getExtras();
if(mBundle!=null && mBundle.getString("action").equals("viewone")){
mStudent = (Student)getIntent().getSerializableExtra("entity");
setStudentUIData(mStudent);
}
if(mBundle!=null && mBundle.getString("action").equals("edit")){
mStudent = (Student)getIntent().getSerializableExtra("entity");
setStudentUIData(mStudent);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(mBundle!=null && mBundle.getString("action").equals("viewone"))
return false;
else
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if(mBundle!=null && mBundle.getString("action").equals("edit")){
menu.add(1,MENU_EDIT,0,"保存");
}else{
menu.add(0,MENU_ADD,0,"增加");
menu.add(0,MENU_VIEWALL,0,"查看");
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ADD:
try {
stuDao = getHelper().getStudentDao();
getStudentData();
if(mStudent != null){
//創建記錄項
stuDao.create(mStudent);
}
} catch (SQLException e) {
e.printStackTrace();
}
break;
case MENU_VIEWALL:
Intent intent = new Intent();
intent.setClass(MainActivity.this, StudentListActivity.class);
startActivity(intent);
break;
case MENU_EDIT:
try {
getStudentData();
stuDao = getHelper().getStudentDao();
if(mStudent != null){
//更新某記錄項
stuDao.update(mStudent);
}
} catch (SQLException e) {
e.printStackTrace();
}
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/**
* 獲取界面值(實體信息)
*/
private void getStudentData(){
mStudent = new Student();
mStudent.setStuNO(stuNO.getText().toString());
mStudent.setName(stuName.getText().toString());
mStudent.setAge(Integer.parseInt(stuAge.getText().toString()));
mStudent.setSex(stuSex.getText().toString());
mStudent.setScore(Double.parseDouble(stuScore.getText().toString()));
mStudent.setAddress(stuAddress.getText().toString());
}
/**
* 賦值給UI界面
* @param student
*/
private void setStudentUIData(Student student){
stuNO.setText(student.getStuNO());
stuName.setText(student.getName());
stuAge.setText(String.valueOf(student.getAge()));
stuSex.setText(student.getSex());
stuScore.setText(String.valueOf(student.getScore()));
stuAddress.setText(student.getAddress());
}
}
~~~
StudentListActivity.java源碼:
~~~
package com.andyidea.ormsqlite;
import java.sql.SQLException;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.andyidea.bean.Student;
import com.andyidea.db.DatabaseHelper;
import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;
public class StudentListActivity extends OrmLiteBaseActivity<DatabaseHelper> {
private Context mContext;
private ListView lvStudents;
private Dao<Student,Integer> stuDao;
private List<Student> students;
private StudentsAdapter adapter;
private Student mStudent;
private final int MENU_VIEW = Menu.FIRST;
private final int MENU_EDIT = Menu.FIRST+1;
private final int MENU_DELETE = Menu.FIRST+2;
private int position;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.students);
mContext = getApplicationContext();
lvStudents = (ListView)findViewById(R.id.stulist);
registerForContextMenu(lvStudents); //注冊上下文菜單
queryListViewItem();
adapter = new StudentsAdapter(students);
lvStudents.setAdapter(adapter);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if(v == lvStudents)
position = ((AdapterContextMenuInfo)menuInfo).position;
menu.add(0,MENU_VIEW, 0, "查看");
menu.add(0,MENU_EDIT, 0, "編輯");
menu.add(0,MENU_DELETE,0,"刪除");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_VIEW:
viewListViewItem(position);
break;
case MENU_EDIT:
editListViewItem(position);
break;
case MENU_DELETE:
deleteListViewItem(position);
break;
default:
break;
}
return super.onContextItemSelected(item);
}
/**
* 查詢記錄項
*/
private void queryListViewItem(){
try {
stuDao = getHelper().getStudentDao();
//查詢所有的記錄項
students = stuDao.queryForAll();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 查看記錄項
* @param position
*/
private void viewListViewItem(int position){
mStudent = students.get(position);
Intent intent = new Intent();
intent.setClass(mContext, MainActivity.class);
intent.putExtra("action", "viewone");
intent.putExtra("entity", mStudent);
startActivity(intent);
}
/**
* 編輯記錄項
*/
private void editListViewItem(int position){
mStudent = students.get(position);
Intent intent = new Intent();
intent.setClass(mContext, MainActivity.class);
intent.putExtra("action", "edit");
intent.putExtra("entity", mStudent);
startActivity(intent);
}
/**
* 刪除記錄項
* @param position
*/
private void deleteListViewItem(int position){
final int pos = position;
AlertDialog.Builder builder2 = new AlertDialog.Builder(StudentListActivity.this);
builder2.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("警告")
.setMessage("確定要刪除該記錄");
builder2.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Student mDelStudent = (Student)lvStudents.getAdapter().getItem(pos);
try {
stuDao.delete(mDelStudent); //刪除記錄
queryListViewItem();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
builder2.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder2.show();
}
class StudentsAdapter extends BaseAdapter{
private List<Student> listStu;
public StudentsAdapter(List<Student> students){
super();
this.listStu = students;
}
@Override
public int getCount() {
return listStu.size();
}
@Override
public Student getItem(int position) {
return listStu.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.studentitem, null);
holder = new ViewHolder();
holder.tvNO = (TextView)convertView.findViewById(R.id.itemno);
holder.tvName = (TextView)convertView.findViewById(R.id.itemname);
holder.tvScore = (TextView)convertView.findViewById(R.id.itemscore);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
Student objStu = listStu.get(position);
holder.tvNO.setText(objStu.getStuNO());
holder.tvName.setText(objStu.getName());
holder.tvScore.setText(String.valueOf(objStu.getScore()));
return convertView;
}
}
static class ViewHolder{
TextView tvNO;
TextView tvName;
TextView tvScore;
}
}
~~~
【6】成功運行程序的截圖效果:


