关闭

1.ormlite 学习笔记

标签: Androidormlite
135人阅读 评论(0) 收藏 举报
分类:
1.下载并导入到libs jar包 
orm(对象关系映射)
对于Android为:ormlite-android-4.48.jar 和 ormlite-core-4.48.jar ;

2.创建Bean类

@DatabaseTable(tableName = "user")//声明表名
public class User {
@DatabaseField(generatedId = true)//是否为自增id
private int id;

//列名和是否允许为空
@DatabaseField(columnName = "name",canBeNull = true)
private String name;

@DatabaseField(columnName = "age" ,canBeNull = true)
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}
3.创建数据库操作helper类

package com.shengqi.ormlite.utils;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

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;
import com.shengqi.model.User;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
* Created by Administrator on 2016/5/26.
*/
public class SqliteHelper extends OrmLiteSqliteOpenHelper {

private static String tableName = "User.db";
private Map<String, Dao> daos = new HashMap<String, Dao>();


public SqliteHelper(Context context) {
super(context, tableName, null, 1);
}



@Override
public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {

try {
TableUtils.createTable(connectionSource , User.class);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
if (oldVersion != newVersion){
try {
TableUtils.dropTable(connectionSource, User.class, true);
                onCreate(sqLiteDatabase, connectionSource);

} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
}

private static SqliteHelper sqliteHelper = null;

public static synchronized SqliteHelper getSqliteHelper(Context mContext){
if (sqliteHelper == null){
sqliteHelper = new SqliteHelper(mContext);
}
return sqliteHelper;
}

@Override
public void close() {
super.close();
for (String key : daos.keySet())
{
Dao dao = daos.get(key);
dao = null;
}
}

@Override
public <D extends Dao<T, ?>, T> D getDao(Class<T> clazz) throws java.sql.SQLException {

Dao dao = null;

String classname = clazz.getSimpleName();

if (daos.containsKey(classname)){
dao = daos.get(classname);
}

if (dao == null){
dao = super.getDao(clazz);
daos.put(classname, dao);
}

return (D) dao;
}
}
4.为每个bean类创建dao类
package com.shengqi.ormlite.utils;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.shengqi.model.User;

import java.sql.SQLException;
import java.util.List;

/**
* Created by Administrator on 2016/5/26.
*/
public class UserDao {
private Context mContext;
private Dao<User,Integer> userDaoOpe;
private SqliteHelper mSqliteHelper;

public UserDao(Context context){
mContext = context;

mSqliteHelper = SqliteHelper.getSqliteHelper(mContext);
try {
//获取bean的dao类
userDaoOpe = mSqliteHelper.getDao(User.class);
User user = new User();
user.setAge(10);
user.setName("wu");
userDaoOpe.create(user);
} catch (SQLException e) {
e.printStackTrace();
}
}

public void getName(){

try {
List<User> list = userDaoOpe.queryBuilder().selectColumns("name").query();

for (User user:list) {
Log.e("user", user.toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
}

}
5.在应用程序中操作数据库
6.关闭数据库连接
切记关闭数据库连接



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2879次
    • 积分:141
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    最新评论