ORMLite -轻量级的对象关系映射(ORM)

    如果你需要在android中使用ORMLite 你需要进入官方网站http://ormlite.com/ 中下载

      

    下载了这两个包以后,你还需要在对应的项目中配置这两个包

  然后你就可以开始写你的数据库语句了!!!

  我们一步一步来建立一个简单的ORMLite的小例子

    我们要实现的也就是利用ORMLite来呈现一个列表集合

  先来看布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list_show_Person"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout>

  既然是要操作数据库,那么就得创建数据库

package com.example.learn_ormlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; public class SQLiteHelper extends SQLiteOpenHelper { private final static String DbName="test_Person.db";
private static int version=1;
public SQLiteHelper(Context context) {
super(context, DbName, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table person(id integer primary key autoincrement,name text)";
db.execSQL(sql);
ContentValues con=new ContentValues();
con.put("name", "text1");
db.insert("person",null,con);
db.insert("person",null,con);
db.insert("person",null,con);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }

然后我们来配置这次的主角,

package com.example.learn_ormlite;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource; public class myORMLiteHelper extends OrmLiteSqliteOpenHelper { private final static String DbName="test_Person.db";
private static int version=1;
private Dao<Person,Integer> person_db=null;
public myORMLiteHelper(Context context) {
super(context, DbName, null, version);
} @Override
public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) { }
public Dao<Person,Integer> getPerson()
{
if(person_db==null)
try {
person_db=getDao(Person.class);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return person_db;
}
@Override
public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
int arg3) {
}
@Override
public void close() {
if(person_db!=null)
person_db=null;
super.close();
} }

我们可以看到,上面用红色标记出来的,说明了ORMLite是自动去找到这个数据库的,也就是说用数据库名称和版本绑定数据库,

  但是现在我们只是告诉它我们要得到一个东西,但是怎么得到的规则还没有定下来,所有,我们还得还定义规则

  

package com.example.learn_ormlite;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable; @DatabaseTable(tableName = "person")
public class Person { @DatabaseField(id = true)
private Integer id;
@DatabaseField(columnName="name")
private String name;
public Person()
{
}
public Person(String name)
{
this.name=name;
}
public void setUid(int uid) {
this.id = uid;
}
public Integer getUid() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}

我们所使用的规则就是,在需要映射的字段上面添加一些属性,当然这些属性必须得和数据库里面的关系相对应,

然后我们就可以使用这个表了

package com.example.learn_ormlite;

import java.sql.SQLException;
import java.util.List; import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView; import com.j256.ormlite.dao.Dao; public class MainActivity extends Activity { private ListView listShow;
private List<Person> Tperson=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteHelper sql=new SQLiteHelper(this);
SQLiteDatabase db= sql.getWritableDatabase();
db.close();
listShow=(ListView) findViewById(R.id.list_show_Person);
myORMLiteHelper ml=new myORMLiteHelper(this); Dao<Person,Integer> db_person= ml.getPerson();
try {
Tperson= db_person.queryForAll();
listShow.setAdapter(new ArrayAdapter<Person>(MainActivity.this,android.R.layout.simple_list_item_1,Tperson){}); } catch (SQLException e) {
e.printStackTrace();
}
}
}

android ORMlite的应用的更多相关文章

  1. Android ORMLite ForeignCollection关联外部集合

     <Android ORMLite ForeignCollection关联外部集合>    Android ORMLite ForeignCollection关联外部集合的功能,适合层 ...

  2. Android Ormlite 学习笔记1 -- 基础

    Ormlite 是一个开源Java数据实体映射框架.其中依赖2个核心类库: 1.ormlite-android-4.48.jar 2.ormlite-core-4.48.jar 新建项目,引用上面2个 ...

  3. Android—Ormlite框架简单的操作数据库

    大家在Android项目中或多或少的都会使用数据库,为了提高我们的开发效率,当然少不了数据库ORM框架了,尤其是某些数据库操作特别频繁的app:本篇博客将详细介绍ORMLite的简易用法. 下面开始介 ...

  4. Android ORMLite 框架的入门用法

    大家在Android项目中或多或少的都会使用数据库,为了提高我们的开发效率,当然少不了数据库ORM框架了,尤其是某些数据库操作特别频繁的app:本篇博客将详细介绍ORMLite的简易用法. 下面开始介 ...

  5. android OrmLite

    最近在使用ormlite框架进行数据库的操作,下面简单的写个demo来学习下 1.下载jar包 这里使用的是ormlite-core-5.0.jar 和 ormlite-android-5.0.jar ...

  6. Android ormlite like() function is not working

    //http://stackoverflow.com/questions/7642161/android-ormlite-like-function-is-not-working try { Quer ...

  7. Android Ormlite 学习笔记2 -- 主外键关系

    以上一篇为例子,进行主外键的查询 定义Users.java 和 Role.java Users -- Role 关系为:1对1 即父表关系 Role -- Users 关系为:1对多 即子表关系 下面 ...

  8. android ormlite queryBuilder.where() 多条件

    QueryBuilder<VideoTagInfo, Integer> queryBuilder = videoTagInfoIntegerDao.queryBuilder();try { ...

  9. android ormlite 清空表

    delete from TableName; //清空数据 update sqlite_sequence SET seq = where name ='TableName';//自增长ID为0 Sam ...

随机推荐

  1. Html中如何让超链接a、图片img居中

    一.问题来源 修改博客页面时,突然想到 二.解决办法 2.1原来办法 在img和a中加入align="center",发现不行 2.2百度答案 <div align=&quo ...

  2. 中国海洋大学第四届朗讯杯高级组 I Cuckoo for Hashing

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2719&cid=1203 题意 :意思就是哈希来的,具体大意就是说有两个哈希表,然后有这样 ...

  3. wordpress的使用

    ubuntu 发送邮件学习资料: http://edu.51cto.com/lesson/id-6066.html 相关插件: Disable Google Fonts 使用中出现的问题: 1:wor ...

  4. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词

    一.流程分析 1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列 2.new一个hander,把时间队列和歌词队列传给自定义的线程类U ...

  5. Excel Cannot Connect to SharePoint List

    As I am working in SharePoint support, I come across so many issues on day 2 day basis and always tr ...

  6. 判断微信内置浏览器的UserAgent

    要区分用户是通过"微信内置浏览器"还是"原生浏览器"打开的WebApp, 可以通过navigator.userAgent来进行判断. 以下是对各种平台上微信内置 ...

  7. /MD, /MDD, /ML, /MT,/MTD(使用运行时库)

    1. VC编译选项 多线程(/MT)多线程调试(/MTd)多线程 DLL (/MD)多线程调试 DLL (/MDd) 2. C 运行时库                                 ...

  8. 存储过程系列之存储过程具体操作过程及sql数据库调用

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. 存 ...

  9. 【HDOJ】1033 Edge

    题目英文太多,简单翻译了一下:1. For products that are wrapped in small packings it is necessary that the sheet of ...

  10. IPv6 tutorial 1 Get started now

    https://4sysops.com/archives/ipv6-part-1-get-started-now/ You’ve probably heard the news that the In ...