在我们的开发中,为了提高开发效率,我们一般都会使用到框架,ormilte则是我们必不可少的数据库框架。
对于ORMLite我也是今天才刚刚接触,我们先从一个简单的项目来了解它吧。
ORMLite jar
要想使用ormlite,我们要先下载其jar包。
下载方法:
我们可以到官网去下载:http://ormlite.com/releases/
下载的时候,我们要下载一个core的jar和android的jar,本人用的是ormlite-android-5.0.jar和ormlite-core-5.0.jar
然后把我们下载好的jar放在我项目中的lib下,然后加载到项目中即可
编写Bean类
package com.example.bean; import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable; @DatabaseTable(tableName="t_student")
public class student { //generatedId表示该字段名为主键,却自增长
@DatabaseField(generatedId=true)
private int stuId;
@DatabaseField(columnName="stuName")
private String stuName;
@DatabaseField(columnName="age")
private int age;
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
@DatabaseTable(tableName = "tb_user"),标明这是数据库中的一张表
@DatabaseField(columnName = "name") ,columnName的值为该字段在数据中的列名
@DatabaseField(generatedId = true) ,generatedId 表示id为主键且自动生成
编写ORMilte的dao包
package com.example.ormlitetest; 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.example.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 DataBaseHelp extends OrmLiteSqliteOpenHelper{ private static final int version = ;
private static final String DB_NAME = "text_data.db";
//每张表对应一个
private Dao<student,Integer> studao ; public Dao<student,Integer> getDao() throws SQLException{
if(studao == null){
studao = getDao(student.class);
}
return studao;
} public DataBaseHelp(Context context) {
super(context, DB_NAME, null, version);
// TODO Auto-generated constructor stub
} @Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, student.class);
Log.i("tag", "创建数据库success");
} catch (Exception e) {
Log.i("tag", "错误");
} } @Override
public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub } @Override
public void close() {
// TODO Auto-generated method stub
super.close();
studao = null;
} }
这里我们需要继承OrmLiteSqliteOpenHelper,其实就是间接继承了SQLiteOpenHelper
然后需要实现两个方法:
1、onCreate(SQLiteDatabase database,ConnectionSource connectionSource)
创建表,我们直接使用ormlite提供的TableUtils.createTable(connectionSource, User.class);进行创建~
2、onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion)
更新表,使用ormlite提供的TableUtils.dropTable(connectionSource, User.class, true);进行删除操作~
删除完成后,别忘了,创建操作:onCreate(database, connectionSource);
MainActivity类通用数据库
package com.example.ormlitetest; import java.sql.SQLException;
import java.util.*; import com.example.bean.student; import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity { private SQLiteDatabase db;
private ListView lv;
private List<student> stulist = new ArrayList<student>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); DataBaseHelp help = new DataBaseHelp(MainActivity.this); for (int i = ; i < ; i++) {
student s = new student();
s.setStuName("好无聊" + i);
s.setAge(i);
try {
help.getDao().create(s);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
stulist = help.getDao().queryForAll();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new BaseAdapter() { public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
Log.i("info:", "没有缓存,重新生成" + position);
LayoutInflater inflater = MainActivity.this
.getLayoutInflater();
// 因为getView()返回的对象,adapter会自动赋给ListView
view = inflater
.inflate(R.layout.list_item, null);
} else {
Log.i("info:", "有缓存,不需要重新生成" + position);
view = convertView;
}
student s = stulist.get(position); TextView tv_userName = (TextView) view
.findViewById(R.id.tv_stuName);
tv_userName.setText(s.getStuName());
tv_userName.setTextSize(); TextView tv_lastMessage = (TextView) view
.findViewById(R.id.tv_age);
tv_lastMessage.setText(s.getAge()+"");
tv_lastMessage.setTextSize(); return view;
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return ;
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return stulist.size();
}
});
help.close();
} }
然后我们运行一下程序看一下
好了, 这就是本人对于ORMLite的初步了解。