(柯昌合)Android Sqlite 持久化框架。类似于hibernate的sqlite框架。不用写SQL语句,操作数据库
2011-08-19 18:45
627 查看
packagecom.cng.utils;
importjava.sql.SQLException;
importandroid.content.Context;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.util.Log;
importcom.cng.modal.Hello;
importcom.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
importcom.j256.ormlite.dao.Dao;
importcom.j256.ormlite.support.ConnectionSource;
importcom.j256.ormlite.table.TableUtils;
publicclassDataHelperextendsOrmLiteSqliteOpenHelper
{
privatestaticfinalStringDATABASE_NAME="HelloOrmlite.db";
privatestaticfinalintDATABASE_VERSION=1;
privateDao<Hello,Integer>helloDao=null;
publicDataHelper(Contextcontext)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
publicvoidonCreate(SQLiteDatabasedb,ConnectionSourceconnectionSource)
{
try
{
TableUtils.createTable(connectionSource,Hello.class);
}catch(SQLExceptione)
{
Log.e(DataHelper.class.getName(),"创建数据库失败",e);
e.printStackTrace();
}
}
@Override
publicvoidonUpgrade(SQLiteDatabasedb,ConnectionSourceconnectionSource,
intarg2,intarg3)
{
try
{
TableUtils.dropTable(connectionSource,Hello.class,true);
onCreate(db,connectionSource);
}catch(SQLExceptione)
{
Log.e(DataHelper.class.getName(),"更新数据库失败",e);
e.printStackTrace();
}
}
@Override
publicvoidclose()
{
super.close();
helloDao=null;
}
publicDao<Hello,Integer>getHelloDataDao()throwsSQLException
{
if(helloDao==null)
{
helloDao=getDao(Hello.class);
}
returnhelloDao;
}
}
packagecom.cng;
importjava.sql.SQLException;
importjava.util.List;
importcom.cng.modal.Hello;
importcom.cng.utils.DataHelper;
importcom.j256.ormlite.android.apptools.OrmLiteBaseActivity;
importcom.j256.ormlite.dao.Dao;
importandroid.os.Bundle;
importandroid.widget.TextView;
publicclassOrmliteLoginActivityextendsOrmLiteBaseActivity<DataHelper>
{
@Override
publicvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextViewtv=(TextView)this.findViewById(R.id.output);
try
{
Dao<Hello,Integer>helloDao=getHelper().getHelloDataDao();
//添加数据
for(inti=0;i<2;i++)
{
Hellohello=newHello("Hello"+i);
helloDao.create(hello);
}
tv.setText(tv.getText()+"\n"+"添加数据完成");
//查询添加的数据
List<Hello>hellos=helloDao.queryForAll();
for(Helloh:hellos)
{
tv.setText(tv.getText()+"\n"+h.toString());
}
// 删除数据第一条数据
helloDao.delete(hellos.get(0));
tv.setText(tv.getText()+"\n"+"删除数据完成");
//重新查询数据
hellos=helloDao.queryForAll();
for(Helloh:hellos)
{
tv.setText(tv.getText()+"\n"+h.toString());
}
//修改数据
Helloh1=hellos.get(0);
h1.setWord("这是修改过的数据");
tv.setText(tv.getText()+"\n"+"修改数据完成");
helloDao.update(h1);
//重新查询数据
hellos=helloDao.queryForAll();
for(Helloh:hellos)
{
tv.setText(tv.getText()+"\n"+h.toString());
}
}catch(SQLExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
packagecom.cng.modal;
importandroid.R.integer;
importcom.j256.ormlite.field.DatabaseField;
publicclassHello
{
@DatabaseField(generatedId=true,unique=true)
intid;
@DatabaseField
Stringword;
//这是必须加的,否则会出错
publicHello(){}
publicintgetId()
{
returnid;
}
publicHello(Stringword)
{
super();
this.word=word;
}
publicvoidsetId(intid)
{
this.id=id;
}
publicStringgetWord()
{
returnword;
}
publicvoidsetWord(Stringword)
{
this.word=word;
}
@Override
publicStringtoString()
{
StringBuildersb=newStringBuilder();
sb.append("id=").append(id);
sb.append(",word=").append(word);
returnsb.toString();
}
}
DEMO下载地址(8分):
DEMO下载地址2(5分):
QQ群:166711323
相关文章推荐
- sqlite3 实现批量处理 sql语句,避免频繁操作数据库,从而影响性能的问题
- Java的Hibernate框架中用于操作数据库的HQL语句讲解
- [Sqlite] 移动嵌入式数据库Sqlite日报SQL操作语句汇总
- [Sqlite] 移动嵌入式数据库Sqlite的日常SQL操作语句汇总
- Android SQLite数据库之一,使用sql语句操作SQLite数据库
- 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷
- Yii框架SQL语句操作数据库
- oracle数据库加ssm框架出现SQL语句数据库可以查到数据,项目中查不到的问题
- 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等,需要的朋友可以参考下。
- 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等
- 数据库常用操作的SQL语句
- 数据库sql语句笔试题--包含创建数据库、表、插入记录、查询等操作
- 数据库-T-SQL 语句-创建表,删除表,CRUD操作的添加数据,修改数据,删除数据
- Stetho查看Afinal框架操作sqlite的数据库
- 在myeclipse中配置DB Driver(数据库用MySql),并在myeclipse执行sql语句操作
- SQL语句操作数据库小结
- 在Sql Server里通过Sql语句【连接】和【操作】另一个【外地数据库】
- sql语句操作数据库之新增
- MSSQL-用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等
- sql语句的用法数据库操作