android orm映射框架(类似hibernate)基本使用
来源:互联网 发布:用g71编程内孔 编辑:程序博客网 时间:2024/06/05 00:13
转载自 http://www.2cto.com/kf/201208/150354.html
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();
}
www.2cto.com
}
就这三个类,datahelper是操作数据库的类,可新建,更新表,Hello是一个映射到数据库表的类,具体的看api 文档的下载地址是http://ormlite.com/releases/(其中的jar包也在这里下载) OrmliteLoginActivity就是activity类,它没继承activity,而是继承了OrmLiteBaseActivity类。
- android orm映射框架(类似hibernate)基本使用
- android orm映射框架(类似hibernate)基本使用
- android开发中类似hibernate的orm框架-ormlite
- IOS对象关系映射(ORM)之coreData框架的学习——(一)基本使用
- Hibernate 框架对象/关系(ORM)映射文件
- Hibernate(2)-ORM数据映射
- hibernate的ORM映射
- Hibernate是一个开放源代码的对象关系映射框架(ORM)
- ORM框架Hibernate (四) 一对一单向、双向关联映射
- ORM框架Hibernate (五) 多对多关联映射
- android orm 框架 ActiveAndroid 使用
- Android ORM框架 greenDao使用
- Android ORM框架 LiteOrm使用
- SSH:Hibernate框架(关系映射及基本配置)
- Hibernate ORM框架
- hibernate 标准 ORM框架
- Mongodb 的ORM框架 Morphia 之 使用Morphia(映射对象)
- Mongodb 的ORM框架 Morphia 之 使用Morphia(映射对象) .
- UIView你知道多少
- HTML5资源地址
- HTML5 移动开发 -- Canvas 9.4 表格 , 折线图
- C# WinForm应用程序创建启动界面
- Winform编程基础之窗体单例模式和单线程操作
- android orm映射框架(类似hibernate)基本使用
- Delphi中@,^,#,$特殊符号意义
- windows 系统下忘记mysql root 密码 重设root密码
- xode一些配置参数的理解
- vi/vim 查找替换
- android用http下载图片
- 字典树的应用和好处
- iPhone缓存网络数据
- Qt 事件系统