Android菜鸟成长记12 -- ORMLite的简单使用
在我们的开发中,为了提高开发效率,我们一般都会使用到框架,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类
1 package com.example.bean; 2 3 import com.j256.ormlite.field.DatabaseField; 4 import com.j256.ormlite.table.DatabaseTable; 5 6 @DatabaseTable(tableName="t_student") 7 public class student { 8 9 //generatedId表示该字段名为主键,却自增长 10 @DatabaseField(generatedId=true) 11 private int stuId; 12 @DatabaseField(columnName="stuName") 13 private String stuName; 14 @DatabaseField(columnName="age") 15 private int age; 16 public int getStuId() { 17 return stuId; 18 } 19 public void setStuId(int stuId) { 20 this.stuId = stuId; 21 } 22 public String getStuName() { 23 return stuName; 24 } 25 public void setStuName(String stuName) { 26 this.stuName = stuName; 27 } 28 public int getAge() { 29 return age; 30 } 31 public void setAge(int age) { 32 this.age = age; 33 } 34 35 }
@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 = 1; 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类通用数据库
1 package com.example.ormlitetest; 2 3 import java.sql.SQLException; 4 import java.util.*; 5 6 import com.example.bean.student; 7 8 import android.app.Activity; 9 import android.database.sqlite.SQLiteDatabase; 10 import android.os.Bundle; 11 import android.util.Log; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.view.ViewGroup; 15 import android.widget.BaseAdapter; 16 import android.widget.ListView; 17 import android.widget.TextView; 18 19 public class MainActivity extends Activity { 20 21 22 private SQLiteDatabase db; 23 private ListView lv; 24 private List<student> stulist = new ArrayList<student>(); 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 31 DataBaseHelp help = new DataBaseHelp(MainActivity.this); 32 33 for (int i = 0; i < 100; i++) { 34 student s = new student(); 35 s.setStuName("好无聊" + i); 36 s.setAge(i); 37 try { 38 help.getDao().create(s); 39 } catch (SQLException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 try { 45 stulist = help.getDao().queryForAll(); 46 } catch (SQLException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 lv = (ListView) findViewById(R.id.listView1); 51 lv.setAdapter(new BaseAdapter() { 52 53 public View getView(int position, View convertView, ViewGroup parent) { 54 View view = null; 55 if (convertView == null) { 56 Log.i("info:", "没有缓存,重新生成" + position); 57 LayoutInflater inflater = MainActivity.this 58 .getLayoutInflater(); 59 // 因为getView()返回的对象,adapter会自动赋给ListView 60 view = inflater 61 .inflate(R.layout.list_item, null); 62 } else { 63 Log.i("info:", "有缓存,不需要重新生成" + position); 64 view = convertView; 65 } 66 student s = stulist.get(position); 67 68 TextView tv_userName = (TextView) view 69 .findViewById(R.id.tv_stuName); 70 tv_userName.setText(s.getStuName()); 71 tv_userName.setTextSize(15); 72 73 TextView tv_lastMessage = (TextView) view 74 .findViewById(R.id.tv_age); 75 tv_lastMessage.setText(s.getAge()+""); 76 tv_lastMessage.setTextSize(12); 77 78 return view; 79 } 80 81 @Override 82 public long getItemId(int arg0) { 83 // TODO Auto-generated method stub 84 return 0; 85 } 86 87 @Override 88 public Object getItem(int arg0) { 89 // TODO Auto-generated method stub 90 return null; 91 } 92 93 @Override 94 public int getCount() { 95 // TODO Auto-generated method stub 96 return stulist.size(); 97 } 98 }); 99 help.close(); 100 } 101 102 }
然后我们运行一下程序看一下
好了, 这就是本人对于ORMLite的初步了解。