android orm映射框架,可像hibernate一样操作数据库。 以下代码是我从网上摘录下来的,仅供参考.


  1. packagecom.cng.utils;
  2. importjava.sql.SQLException;
  3. importandroid.content.Context;
  4. importandroid.database.sqlite.SQLiteDatabase;
  5. importandroid.util.Log;
  6. importcom.cng.modal.Hello;
  7. importcom.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
  8. importcom.j256.ormlite.dao.Dao;
  9. importcom.j256.ormlite.support.ConnectionSource;
  10. importcom.j256.ormlite.table.TableUtils;
  11. publicclassDataHelperextendsOrmLiteSqliteOpenHelper
  12. {
  13. privatestaticfinalStringDATABASE_NAME="HelloOrmlite.db";
  14. privatestaticfinalintDATABASE_VERSION=1;
  15. privateDao<Hello,Integer>helloDao=null;
  16. publicDataHelper(Contextcontext)
  17. {
  18. super(context,DATABASE_NAME,null,DATABASE_VERSION);
  19. }
  20. @Override
  21. publicvoidonCreate(SQLiteDatabasedb,ConnectionSourceconnectionSource)
  22. {
  23. try
  24. {
  25. TableUtils.createTable(connectionSource,Hello.class);
  26. }catch(SQLExceptione)
  27. {
  28. Log.e(DataHelper.class.getName(),"创建数据库失败",e);
  29. e.printStackTrace();
  30. }
  31. }
  32. @Override
  33. publicvoidonUpgrade(SQLiteDatabasedb,ConnectionSourceconnectionSource,
  34. intarg2,intarg3)
  35. {
  36. try
  37. {
  38. TableUtils.dropTable(connectionSource,Hello.class,true);
  39. onCreate(db,connectionSource);
  40. }catch(SQLExceptione)
  41. {
  42. Log.e(DataHelper.class.getName(),"更新数据库失败",e);
  43. e.printStackTrace();
  44. }
  45. }
  46. @Override
  47. publicvoidclose()
  48. {
  49. super.close();
  50. helloDao=null;
  51. }
  52. publicDao<Hello,Integer>getHelloDataDao()throwsSQLException
  53. {
  54. if(helloDao==null)
  55. {
  56. helloDao=getDao(Hello.class);
  57. }
  58. returnhelloDao;
  59. }
  60. }

  1. packagecom.cng;
  2. importjava.sql.SQLException;
  3. importjava.util.List;
  4. importcom.cng.modal.Hello;
  5. importcom.cng.utils.DataHelper;
  6. importcom.j256.ormlite.android.apptools.OrmLiteBaseActivity;
  7. importcom.j256.ormlite.dao.Dao;
  8. importandroid.os.Bundle;
  9. importandroid.widget.TextView;
  10. publicclassOrmliteLoginActivityextendsOrmLiteBaseActivity<DataHelper>
  11. {
  12. @Override
  13. publicvoidonCreate(BundlesavedInstanceState)
  14. {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. TextViewtv=(TextView)this.findViewById(R.id.output);
  18. try
  19. {
  20. Dao<Hello,Integer>helloDao=getHelper().getHelloDataDao();
  21. //添加数据
  22. for(inti=0;i<2;i++)
  23. {
  24. Hellohello=newHello("Hello"+i);
  25. helloDao.create(hello);
  26. }
  27. tv.setText(tv.getText()+"\n"+"添加数据完成");
  28. //查询添加的数据
  29. List<Hello>hellos=helloDao.queryForAll();
  30. for(Helloh:hellos)
  31. {
  32. tv.setText(tv.getText()+"\n"+h.toString());
  33. }
  34. //删除数据第一条数据
  35. helloDao.delete(hellos.get(0));
  36. tv.setText(tv.getText()+"\n"+"删除数据完成");
  37. //重新查询数据
  38. hellos=helloDao.queryForAll();
  39. for(Helloh:hellos)
  40. {
  41. tv.setText(tv.getText()+"\n"+h.toString());
  42. }
  43. //修改数据
  44. Helloh1=hellos.get(0);
  45. h1.setWord("这是修改过的数据");
  46. tv.setText(tv.getText()+"\n"+"修改数据完成");
  47. helloDao.update(h1);
  48. //重新查询数据
  49. hellos=helloDao.queryForAll();
  50. for(Helloh:hellos)
  51. {
  52. tv.setText(tv.getText()+"\n"+h.toString());
  53. }
  54. }catch(SQLExceptione)
  55. {
  56. //TODOAuto-generatedcatchblock
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  1. packagecom.cng.modal;
  2. importandroid.R.integer;
  3. importcom.j256.ormlite.field.DatabaseField;
  4. publicclassHello
  5. {
  6. @DatabaseField(generatedId=true,unique=true)
  7. intid;
  8. @DatabaseField
  9. Stringword;
  10. //这是必须加的,否则会出错
  11. publicHello(){}
  12. publicintgetId()
  13. {
  14. returnid;
  15. }
  16. publicHello(Stringword)
  17. {
  18. super();
  19. this.word=word;
  20. }
  21. publicvoidsetId(intid)
  22. {
  23. this.id=id;
  24. }
  25. publicStringgetWord()
  26. {
  27. returnword;
  28. }
  29. publicvoidsetWord(Stringword)
  30. {
  31. this.word=word;
  32. }
  33. @Override
  34. publicStringtoString()
  35. {
  36. StringBuildersb=newStringBuilder();
  37. sb.append("id=").append(id);
  38. sb.append(",word=").append(word);
  39. returnsb.toString();
  40. }
  41. }

就这三个类,datahelper是操作数据库的类,可新建,更新表,Hello是一个映射到数据库表的类,具体的看api 文档的下载地址是http://ormlite.com/releases/(其中的jar包也在这里下载) OrmliteLoginActivity就是activity类,它没继承activity,而是继承了OrmLiteBaseActivity类。


更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. python起点网月票榜字体反爬案例
  3. Android中使用Gson解析JSON数据
  4. Android中使用Gson解析JSON数据
  5. 安卓9.0 http请求数据失败解决办法
  6. Android保存32位BMP格式图片
  7. Android(安卓)基础UI编程2
  8. android之launcher时序图and图标的建立
  9. android bluetooth 移植相关注意事项

随机推荐

  1. 设置Android默认锁定屏幕旋转
  2. EditText常用属性
  3. Android与H5互调
  4. Android(安卓)屏幕适配,分辨率适配
  5. Android(安卓)systemserver 解析
  6. android 经典文章收集
  7. Android(安卓)Framework(II)Person Sampl
  8. 准备一个rubymotion for android的实际项
  9. android获取bluetooth的信号强度(RSSI)
  10. [译] Android(安卓)架构:Part 4 —— 实践