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

android操作sqlite数据库

2015-07-07 16:52 721 查看
C#作业android操作sqlite数据库
android自带数据库sqlite,但是他不像mysql那样有非常方便的图形化界面,而且测试起来超级麻烦,每次还要部署到手机上面看看。所以这次的作业主要是想通过学习litepal框架去操作数据库。LitePal是一款开源的Android数据库框架,它采用了对象关系映射(ORM)的模式,并将我们平时开发时最常用到的一些数据库功能进行了封装,使得不用编写一行SQL语句就可以完成各种建表、増删改查的操作。并且LitePal很“轻”,jar包只有100k不到,而且近乎零配置,这一点和Hibernate这类的框架有很大区别。
使用方法如下,首先导入jar包,然后创建你需要的实体,我这里有4个然后在asserts文件夹里面写配置文件litepal,代码如下:
<?xml version="1.0" encoding="utf-8"?>

<litepal>

<!--

Define the database name of your application.

By default each database name should be end with .db.

If you didn't name your database end with .db,

LitePal would plus the suffix automaticly for you.

For example:

<dbname value="demo" ></dbname>

-->

<dbname value="my_db" ></dbname>

<!--

Define the version of your database. Each time you want

to upgrade your database, the version tag would helps.

Modify the models you defined in the mapping tag, and just

make the version value plus one, the upgrade of database

will be processed automaticly without concern.

For example:

<version value="1" ></version>

-->

<version value="2" ></version>

<!--

Define your models in the list with mapping tag, LitePal will

create tables for each mapping class. The supported fields

defined in models will be mapped into columns.

For example:

<list>

<mapping class="com.test.model.Reader"></mapping>

<mapping class="com.test.model.Magazine"></mapping>

</list>

-->

<list>

<mapping class="com.zgrjb.model.Customer"></mapping>

<mapping class="com.zgrjb.model.Friend"></mapping>

<mapping class="com.zgrjb.model.LastRecord"></mapping>

<mapping class="com.zgrjb.model.MsgRecord"></mapping>

</list>

</litepal>

接着在application中继承LitePalApplication,并打开数据库,代码如下
public class BaseApp extends LitePalApplication{



public static boolean isQuickIn = false;

public static BaseApp mInstance;

private MsgDBUtils utils = MsgDBUtils.getInstance();

private SQLiteDatabase db;

@Override

public void onCreate() {

// TODO Auto-generated method stub

super.onCreate();

db = utils.createDB();

mInstance = this;

// serviceManager = new ServiceManager(this);

// serviceManager.setNotificationIcon(R.drawable.notification);

// serviceManager.startService();

initNetworkImageLoader();

}

只截了部分图,文件太多代码不好看。
下面是插入和查询所有的代码
public static LastRecord searchByLastRecord() {

return DataSupport.findFirst(LastRecord.class);

}

public static void insert(List<LastRecord> list){

DataSupport.saveAll(list);

}

是不是非常简洁,对于数据库开发来说简直是超级无敌方便!
总结:android数据库开发框架很多,但这个框架是中国人写的,支持国产,而且使用起来也很方便,很喜欢这个框架,对以后的学习一定很有帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: