初学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);
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource)
{
try
{
TableUtils.createTable(connectionSource, Hello.class);
} catch (SQLException e)
{
Log.e(DataHelper.class.getName(), "创建数据库失败", e);
e.printStackTrace();
}
}
@Override
public 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();
}
}
@Override
public 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>
{
@Override
public 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 block
e.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;
@DatabaseField
String 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;
}
@Override
public 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框架很强大>_<。
分享到:
相关推荐
android开源的orm框架,使用起来很简单。避开了直接操作sqlite,只需关注java实体对象。一句代码解决CRUD。
Google开源的基于Android的Sqlite对象关系映射框架 ormlite 压缩包里包含 ormlite-android-4.30.jar 及帮助文档、ormlite-core-4.30.jar核心包 当然也支持在Java中的Jdbc中使用类似Hibernate Sqlite驱动包、ormlite...
一个简单的Android SQLite ORM框架不想花资源分的同学可以上我的github主页下载:https://github.com/chenyihan/Simple-SQLite-ORM-Android,因为要传到github,所以代码中的注释和doc文档都是英文的,对自己英文不...
ORM映射与WEB的应用ORM映射与WEB的应用ORM映射与WEB的应用ORM映射与WEB的应用ORM映射与WEB的应用ORM映射与WEB的应用
[置顶]android ORM Lite框架,对象与数据库表的映射。android的 sqlite操作不用写SQL语句.docx
分析整理了基于android orm框架的性能比对及功能完整示例,主要包括:Provider ORMLite GreenDao XutilsDao 完整示例,可直接在eclipse中运行
Hibernate框架ORM的实现原理.doc
OrmLite's goal is to provide a convenient, DRY, config-free, RDBMS-agnostic typed wrapper that retains a high affinity with SQL, exposing intuitive APIs that generate predictable SQL and maps cleanly ...
C# ORM映射 开发及原理 公司内部开发的ORM 源码 禁止商业用途,违者必究!
GreenDao是Android ORM(对象关系映射)框架,简化对数据库的操作,不用开发者自己拼装sql语句,减少出错率。开发者只需对实体类实例进行操作即可达到操作数据库的目的。Demo的GreenDao版本为3.2,通过注解方式生成...
目前有很多面向android操作系统的orm库。我们回顾了最流行的,并比较了性能和其他一些参数。
sqltoy-orm是比hibernate+myBatis(plus)更加贴合项目的orm框架(依赖spring),具有jpa式的对象CRUD的同时具有比myBatis(plus)更直观简洁性能强大的查询功能。 支持以下数据库: oracle 11g+ db2 9.5+,建议从10.5 开始...
能实现基本的数据库操作能实现基本的数据库操作
android 数据库orm框架依赖库 非常简便的数据库框架lib
一个精简的 Android ORM 框架,在 eclipse 上,有插件来生成代码
Android Sqlite 数据库通过 ORM 的方式操作起来会方便很多,效率也会提升很多。本课程将会详细讲解 ORM 数据库框架的使用,提升 Sqlite 的操作和开发效率。
Android上主流的ORM框架有很多,常用的有ORMLite、GreenDao等。ORMLite:-优点:API很友好,使用比较方便简单稳定,相关功能比较完整。-缺点:内部使用反射实现,性能并不是很好。GreenDao:-优点:性能很不错,...
实现Hibernate框架中一对一,一对多,多对多关系的单向和双向配置总结!
ORM对象关系映射。。ORM对象关系映射。。太详细了。。最全的介绍。
ORM映射实现的java源码,可以运行。他是ORM映射的彻底的底层实现,真正的ORM映射的原理。