java sqllite持久化_(柯昌合)Android Sqlite 持久化框架。类似于hibernate的sqlite框架。不用写SQL语句,操作数据库 | 学步园...

package com.cng.utils;

import java.sql.SQLException;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.util.Log;

import com.cng.modal.Hello;

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;

public class DataHelper extends OrmLiteSqliteOpenHelper

{

private static final String DATABASE_NAME = "HelloOrmlite.db";

private static final int DATABASE_VERSION = 1;

private Dao helloDao = null;

public DataHelper(Context context)

{

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource)

{

try

{

TableUtils.createTable(connectionSource, Hello.class);

} catch (SQLException e)

{

Log.e(DataHelper.class.getName(), "创建数据库失败", e);

e.printStackTrace();

}

}

@Override

public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,

int arg2, int arg3)

{

try

{

TableUtils.dropTable(connectionSource, Hello.class, true);

onCreate(db, connectionSource);

} catch (SQLException e)

{

Log.e(DataHelper.class.getName(), "更新数据库失败", e);

e.printStackTrace();

}

}

@Override

public void close()

{

super.close();

helloDao = null;

}

public Dao getHelloDataDao() throws SQLException

{

if (helloDao == null)

{

helloDao = getDao(Hello.class);

}

return helloDao;

}

}

package com.cng;

import java.sql.SQLException;

import java.util.List;

import com.cng.modal.Hello;

import com.cng.utils.DataHelper;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

import com.j256.ormlite.dao.Dao;

import android.os.Bundle;

import android.widget.TextView;

public class OrmliteLoginActivity extends OrmLiteBaseActivity

{

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TextView tv = (TextView) this.findViewById(R.id.output);

try

{

Dao helloDao = getHelper().getHelloDataDao();

// 添加数据

for (int i = 0; i < 2; i++)

{

Hello hello = new Hello("Hello" + i);

helloDao.create(hello);

}

tv.setText(tv.getText() + "\n" + "添加数据完成");

// 查询添加的数据

List hellos = helloDao.queryForAll();

for (Hello h : hellos)

{

tv.setText(tv.getText() + "\n" + h.toString());

}

// 删除数据第一条数据

helloDao.delete(hellos.get(0));

tv.setText(tv.getText() + "\n" + "删除数据完成");

// 重新查询数据

hellos = helloDao.queryForAll();

for (Hello h : hellos)

{

tv.setText(tv.getText() + "\n" + h.toString());

}

// 修改数据

Hello h1 = hellos.get(0);

h1.setWord("这是修改过的数据");

tv.setText(tv.getText() + "\n" + "修改数据完成");

helloDao.update(h1);

// 重新查询数据

hellos = helloDao.queryForAll();

for (Hello h : hellos)

{

tv.setText(tv.getText() + "\n" + h.toString());

}

} catch (SQLException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

package com.cng.modal;

import android.R.integer;

import com.j256.ormlite.field.DatabaseField;

public class Hello

{

@DatabaseField(generatedId = true,unique=true)

int id;

@DatabaseField

String word;

//这是必须加的,否则会出错

public Hello(){}

public int getId()

{

return id;

}

public Hello(String word)

{

super();

this.word = word;

}

public void setId(int id)

{

this.id = id;

}

public String getWord()

{

return word;

}

public void setWord(String word)

{

this.word = word;

}

@Override

public String toString()

{

StringBuilder sb = new StringBuilder();

sb.append("id=").append(id);

sb.append(" ,word=").append(word);

return sb.toString();

}

}

1365321914_7480.jpg

QQ群:166711323

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值