android Sqlite 模板 。

DatabaseHelper.java


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper{

    private static final String DB_NAME = "foodtherapy.db";
    private static final int VERSION = 1;

    public DatabaseHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
    }

    public DatabaseHelper(Context context) {
        //TODO 初始化数据库
        super(context, DB_NAME, null, VERSION);
    } 
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO 创建数据库表
        String foodtable = "create table foodtable (id long primary key ,name varchar(45),img varchar(45),menu varchar(255),bar varchar(255),count integer,rcount integer ,fcount integer,summary varchar(255),detailText varchar(255))";
        String cooktable = "create table cooktable (id long primary key ,name varchar(45),tag varchar(245),bar TEXT,img varchar(45),message varchar(255),count integer,food varchar(255))";
        String newstable = "create table newstable (id long primary key ,title varchar(245),tag varchar(245),message varchar(255),img varchar(45),count integer,author varchar(45),focal integer,time datetime)";
        db.execSQL(foodtable);
        db.execSQL(cooktable);
        db.execSQL(newstable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO 更新数据库
        db.execSQL("drop table if exists foodtable");
        db.execSQL("drop table if exists cooktable");
        db.execSQL("drop table if exists newstable");
        onCreate(db);

    }

}

数据库模版生成器

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

/**
 * Database builder template - facilitates adding and getting
 * objects from SQLite database
 * 
 * @author Lukasz Wisniewski
 *
 * @param <T>
 */
public abstract class BaseDao<T> {

    public DatabaseHelper dbHelper;
    public Context context = null;
    /**
     * Creates object out of cursor
     * 
     * @param c
     * @return
     */
    public abstract T build(Cursor c);

    /**
     * Puts an object into a ContentValues instance
     * 
     * @param t
     * @return
     */
    public abstract ContentValues deconstruct(T t);

    public abstract void insert(T t);

    public abstract void delete(long id);

    public abstract T select(long id);

    public abstract ArrayList<T> findAll();

    public abstract boolean isExits(long id);

}

Example

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import iiijiaban.foodtherapy.db.DatabaseHelper;
import iiijiaban.foodtherapy.db.FoodDB;
import iiijiaban.foodtherapy.db.NewsDB;

public class NewsDBDao extends BaseDao<NewsDB> {

    public NewsDBDao(Context context) {
        this.context = context;
        this.dbHelper = new DatabaseHelper(context);
    }

    // private long id;//资讯id
    // private String title; //资讯标题
    // private String tag ;//资讯标签 tag
    // private String message ;//资讯 详细内容
    // private String img ;//图片
    // private int count ;//浏览此时
    // private String author; //作者
    // private int focal; //是否焦点
    // private Date time ;//发布时间
    @Override
    public NewsDB build(Cursor c) {
        int columnid = c.getColumnIndex("id");
        int columntitle = c.getColumnIndex("title");
        int columntag = c.getColumnIndex("tag");
        int columnmessage = c.getColumnIndex("message");
        int columnimg = c.getColumnIndex("img");
        int columncount = c.getColumnIndex("count");
        int columnauthor = c.getColumnIndex("author");
        int columnfocal = c.getColumnIndex("focal");
        int columntime = c.getColumnIndex("time"); 
        NewsDB fooddb = new NewsDB();
        fooddb.setId(c.getLong(columnid));
        fooddb.setTitle(c.getString(columntitle));
        fooddb.setTag(c.getString(columntag));
        fooddb.setMessage(c.getString(columnmessage));
        fooddb.setImg(c.getString(columnimg));
        fooddb.setCount(c.getInt(columncount));

        fooddb.setAuthor(c.getString(columnauthor));
        fooddb.setFocal(c.getInt(columnfocal));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        Date date = null;
        try {
            date = sdf.parse(c.getString(columntime));
        } catch (ParseException e) { 
            e.printStackTrace();
        }
        fooddb.setTime(date);  
        return fooddb;
    }

    @Override
    public ContentValues deconstruct(NewsDB t) {
        ContentValues values = new ContentValues();
        values.put("id", t.getId());
        values.put("title", t.getTitle());
        values.put("tag", t.getTag());
        values.put("message", t.getMessage());
        values.put("img", t.getImg()); 
        values.put("count", t.getCount());
        values.put("author", t.getAuthor());
        values.put("focal", t.getFocal());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        String date = sdf.format(t.getTime());
        values.put("time", date); 
        return values;
    }

    @Override
    public void insert(NewsDB t) {
        // TODO Auto-generated method stub
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = deconstruct(t);
        db.insert("newstable", null, values);
        db.close();

    }

    @Override
    public void delete(long id) {
        // TODO Auto-generated method stub
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete("newstable", "id = " + id, null);
        db.close();
    }

    @Override
    public NewsDB select(long id) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        NewsDB newsDB = new NewsDB();
        Cursor cursor = db.query("newstable", null, "id = " + id, null, null,
                null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            newsDB = build(cursor);
            cursor.moveToNext();
        }
        db.close();
        return newsDB;
    }
    @Override
    public ArrayList<NewsDB> findAll() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ArrayList<NewsDB> list = new ArrayList<NewsDB>();
        Cursor cursor = db.query("newstable", null, null, null, null, null,
                null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            NewsDB newsDB = new NewsDB();
            newsDB = build(cursor);
            list.add(newsDB);
            cursor.moveToNext();
        }
        db.close();
        return list;
    }

    @Override
    public boolean isExits(long id) { 

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        NewsDB newsDB = null;
        Cursor cursor = db.query("newstable", null, "id = " + id, null, null,null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            newsDB = build(cursor);
            cursor.moveToNext();
        }
        db.close();
        if(newsDB!=null){
            return true;
        }else{
            return false;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值