Android菜鸟成长记12 -- ORMLite的简单使用

案头见蠹鱼,犹胜凡俦侣。这篇文章主要讲述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下,然后加载到项目中即可

Android菜鸟成长记12 -- ORMLite的简单使用

文章图片

编写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) 11private int stuId; 12@DatabaseField(columnName="stuName") 13private String stuName; 14@DatabaseField(columnName="age") 15private int age; 16public int getStuId() { 17return stuId; 18} 19public void setStuId(int stuId) { 20this.stuId = stuId; 21} 22public String getStuName() { 23return stuName; 24} 25public void setStuName(String stuName) { 26this.stuName = stuName; 27} 28public int getAge() { 29return age; 30} 31public void setAge(int age) { 32this.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 22private SQLiteDatabase db; 23private ListView lv; 24private List< student> stulist = new ArrayList< student> (); 25 26@Override 27protected void onCreate(Bundle savedInstanceState) { 28super.onCreate(savedInstanceState); 29setContentView(R.layout.activity_main); 30 31DataBaseHelp help = new DataBaseHelp(MainActivity.this); 32 33for (int i = 0; i < 100; i++) { 34student s = new student(); 35s.setStuName("好无聊" + i); 36s.setAge(i); 37try { 38help.getDao().create(s); 39} catch (SQLException e) { 40// TODO Auto-generated catch block 41e.printStackTrace(); 42} 43} 44try { 45stulist = help.getDao().queryForAll(); 46} catch (SQLException e) { 47// TODO Auto-generated catch block 48e.printStackTrace(); 49} 50lv = (ListView) findViewById(R.id.listView1); 51lv.setAdapter(new BaseAdapter() { 52 53public View getView(int position, View convertView, ViewGroup parent) { 54View view = null; 55if (convertView == null) { 56Log.i("info:", "没有缓存,重新生成" + position); 57LayoutInflater inflater = MainActivity.this 58.getLayoutInflater(); 59// 因为getView()返回的对象,adapter会自动赋给ListView 60view = inflater 61.inflate(R.layout.list_item, null); 62} else { 63Log.i("info:", "有缓存,不需要重新生成" + position); 64view = convertView; 65} 66student s = stulist.get(position); 67 68TextView tv_userName = (TextView) view 69.findViewById(R.id.tv_stuName); 70tv_userName.setText(s.getStuName()); 71tv_userName.setTextSize(15); 72 73TextView tv_lastMessage = (TextView) view 74.findViewById(R.id.tv_age); 75tv_lastMessage.setText(s.getAge()+""); 76tv_lastMessage.setTextSize(12); 77 78return view; 79} 80 81@Override 82public long getItemId(int arg0) { 83// TODO Auto-generated method stub 84return 0; 85} 86 87@Override 88public Object getItem(int arg0) { 89// TODO Auto-generated method stub 90return null; 91} 92 93@Override 94public int getCount() { 95// TODO Auto-generated method stub 96return stulist.size(); 97} 98}); 99help.close(); 100} 101 102 }

然后我们运行一下程序看一下
Android菜鸟成长记12 -- ORMLite的简单使用

文章图片

【Android菜鸟成长记12 -- ORMLite的简单使用】好了, 这就是本人对于ORMLite的初步了解。

    推荐阅读