日期:2014-05-16  浏览次数:20544 次

android SQLiteOpenHelper操作数据库 搜索关键字列表应用
package com.EleMenu_YuanXu_v12.DB;


import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import com.EleMenu_YuanXu_v12.Util.UtilValue;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
/**
 * 数据操作,
 * @author L
 *
 */
public class DBService extends SQLiteOpenHelper{
private List<String> list_menu;
public boolean isCreate = false;
public final static int DATABASE_VERSION = 1; //版本号
public final static String DATABASE_NAME = "keywords.db"; //数据库名
public final static String TABLE_NAME = "keywords";


//初始化该类的时候,创建新的数据库
public DBService(Context context) {
super(context, DBShowImpl.DATABASE_NAME, null, DBShowImpl.DATABASE_VERSION);
}




/**
* 创建数据库表
*/
@Override
public void onCreate(SQLiteDatabase db) {
//判断改表是否存在
        if(!tabbleIsExist(DBShowImpl.TABLE_NAME)){
        db.execSQL("create table "+DBShowImpl.TABLE_NAME+"(_id integer PRIMARY KEY autoincrement,menu_id varchar(20),keyword text)");
    System.out.println("成功创建");
    isCreate = true;
        }
        Log. e("Database" ,"onCreate" );
}
/**
* 更新版本号
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("onUpgrade");
}

// 执行select语句
public Cursor query(String sql, String[] args)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, args);
return cursor;
}
/**
* 创建数据库表
*/
public void creatDB(){
SQLiteDatabase db = null;
db = this.getWritableDatabase();
db.execSQL("create table if not exists "+DBShowImpl.TABLE_NAME+"(_id integer PRIMARY KEY autoincrement,menu_id varchar(20),keyword text)");
}