初学android不久,由于我是以前学过j2ee,什么ssh曾经用过,java的数据库持久化框架hibernate给程序操作数据库提供了方便,学习android的时候,想看看是否有个操作数据库的框架,谷歌了下,发现android的数据库框架很多什么,但都缺乏中文文档(本人还没过六级。。英语很差),几乎找不到例子,最后找到了ormlite,一个android orm映射框架,可像hibernate一样操作数据库。 以下代码是我从网上摘录下来的,仅供参考.

package com.cng.utils;import java.sql.SQLException;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.util.Log;import com.cng.modal.Hello;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 DataHelper extends OrmLiteSqliteOpenHelper{private static final String DATABASE_NAME = "HelloOrmlite.db";private static final int DATABASE_VERSION = 1;private Dao<Hello, Integer> helloDao = null;public DataHelper(Context context){super(context, DATABASE_NAME, null, DATABASE_VERSION);}@Overridepublic void onCreate(SQLiteDatabase db, ConnectionSource connectionSource){try{TableUtils.createTable(connectionSource, Hello.class);} catch (SQLException e){Log.e(DataHelper.class.getName(), "创建数据库失败", e);e.printStackTrace();}}@Overridepublic void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,int arg2, int arg3){try{TableUtils.dropTable(connectionSource, Hello.class, true);onCreate(db, connectionSource);} catch (SQLException e){Log.e(DataHelper.class.getName(), "更新数据库失败", e);e.printStackTrace();}}@Overridepublic void close(){super.close();helloDao = null;}public Dao<Hello, Integer> getHelloDataDao() throws SQLException{if (helloDao == null){helloDao = getDao(Hello.class);}return helloDao;}}

package com.cng;import java.sql.SQLException;import java.util.List;import com.cng.modal.Hello;import com.cng.utils.DataHelper;import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;import com.j256.ormlite.dao.Dao;import android.os.Bundle;import android.widget.TextView;public class OrmliteLoginActivity extends OrmLiteBaseActivity<DataHelper>{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);TextView tv = (TextView) this.findViewById(R.id.output);try{Dao<Hello, Integer> helloDao = getHelper().getHelloDataDao();// 添加数据for (int i = 0; i < 2; i++){Hello hello = new Hello("Hello" + i);helloDao.create(hello);}tv.setText(tv.getText() + "\n" + "添加数据完成");// 查询添加的数据List<Hello> hellos = helloDao.queryForAll();for (Hello h : hellos){tv.setText(tv.getText() + "\n" + h.toString());}// 删除数据第一条数据helloDao.delete(hellos.get(0));tv.setText(tv.getText() + "\n" + "删除数据完成");// 重新查询数据hellos = helloDao.queryForAll();for (Hello h : hellos){tv.setText(tv.getText() + "\n" + h.toString());}// 修改数据Hello h1 = hellos.get(0);h1.setWord("这是修改过的数据");tv.setText(tv.getText() + "\n" + "修改数据完成");helloDao.update(h1);// 重新查询数据hellos = helloDao.queryForAll();for (Hello h : hellos){tv.setText(tv.getText() + "\n" + h.toString());}} catch (SQLException e){// TODO Auto-generated catch blocke.printStackTrace();}}}

package com.cng.modal;import android.R.integer;import com.j256.ormlite.field.DatabaseField;public class Hello{@DatabaseField(generatedId = true,unique=true)int id;@DatabaseFieldString word;//这是必须加的,否则会出错public Hello(){}public int getId(){return id;}public Hello(String word){super();this.word = word;}public void setId(int id){this.id = id;}public String getWord(){return word;}public void setWord(String word){this.word = word;}@Overridepublic String toString(){StringBuilder sb = new StringBuilder();sb.append("id=").append(id);sb.append(" ,word=").append(word);return sb.toString();}}

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

ormlite还在学习中,表示这个android框架很强大>_<。

更多相关文章

  1. 使用android快速开发框架afinal 开发android应用程序
  2. flutter与android混合开发一:Android原生项目创建flutter模块、An
  3. Android学习札记13:为什么更推荐使用Parcelable来在Activity间传
  4. Android的快速开发框架afinal
  5. Android 之读取元素中的数据
  6. Android 读取元素中的数据
  7. ViewPager的定时滚动,动态加载数据

随机推荐

  1. Android系列教程之四:Android项目的目录结
  2. Android名词解释之什么是APK文件
  3. 如何在Android(安卓)Studio中导入包:andro
  4. Android的背景
  5. Android周学习Step By Step(6)--Android的
  6. Android用户体验毁在框架设计?
  7. Android的一些属性
  8. Android(安卓)开发中Parcel存储类型和数
  9. android开发学习笔记(4)导入Android工程
  10. 在Mac OS X下安裝Android開發環境和Eclip