您的位置:首页 > 移动开发 > Android开发

(柯昌合)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分):androidSqlite持久化框架

DEMO下载地址2(5分):SQLite执久化框架

框架内核代码



QQ群:166711323
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐