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) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
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) {
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);
}
@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) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = deconstruct(t);
db.insert("newstable", null, values);
db.close();
}
@Override
public void delete(long id) {
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;
}
}
}