Android 数据库操作封装,简化操作

package com.abc.lyq.util;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseTool extends SQLiteOpenHelper {

    private final static String DATABASE_NAME = "****.db";
    private final static int DATABASE_VERSION = 1;

    public final static String SETTING_TABLE_NAME = "setting_tb_yq";
  
    public final static String ID = "lyq001000";

    private final static String SET_NAME = "lyq001";
    private final static String SET_VALUE = "lyq010";

    private final static String NAME = "lyq001";
 
    public final static String[] setting_cols = {SET_NAME, SET_VALUE};


    public DataBaseTool(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        createTable(db, createSQL(SETTING_TABLE_NAME, setting_cols));
    }

    private void createTable(SQLiteDatabase db, String tableSQL) {
        try {
            db.execSQL(tableSQL);
        } catch (SQLException e) {
            Log.e(ConstantUtil.TAG, "create table failed! " + tableSQL, e);
        }
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long insert(String tablename, ContentValues values) {
        SQLiteDatabase db = getWritableDatabase();
        return db.insert(tablename, null, values);
    }

    public void batchInsert(String tablename, List<ContentValues> records) {
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        for (ContentValues values : records) {
            db.insert(tablename, null, values);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    public int update(String tablename, ContentValues values, String whereClause, String[] whereArgs) {
        SQLiteDatabase db = getWritableDatabase();
        return db.update(tablename, values, whereClause, whereArgs);
    }

    public int remove(String tablename, String whereClause, String[] whereArgs) {
        SQLiteDatabase db = getWritableDatabase();
        return db.delete(tablename, whereClause, whereArgs);
    }

    public Cursor select(String tablename, String[] cols, String where, String[] whereArgs, String groupBy, String having, String orderBy) {
        SQLiteDatabase db = getReadableDatabase();
        return db.query(tablename, cols, where, whereArgs, groupBy, having, orderBy);
    }

    private String createSQL(String table, String[] cols) {
        StringBuffer sb = new StringBuffer("CREATE TABLE if not exists ");
        sb.append(table);
        sb.append(" ( ").append(ID).append(" INTEGER PRIMARY KEY autoincrement ");
        for (String col : cols) {
            sb.append(" , ").append(col).append(" TEXT");
        }
        sb.append(" ) ");
        return sb.toString();
    }

}

你可能感兴趣的:(Android 数据库操作封装,简化操作)