package com.achilies.yuqingxitong.util; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { private static final String DB_NAME = "yuqing.db"; private static final String WARN = "t_warn"; // 创建SQL语句 private static final String warnSql = " CREATE TABLE t_warn (_ID INTEGER PRIMARY KEY autoincrement,u_aid INTEGER,u_level INTEGER,u_title text,u_url text,u_site text,u_tmpost text,u_content text)"; // SQLiteDatabase实例 private SQLiteDatabase db; public DBHelper(Context context) { super(context, DB_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(warnSql); } // 打开数据库 public void open() { db = getWritableDatabase(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub String sql = "DROP TABLE IF EXISTS " + WARN; db.execSQL(sql); onCreate(db); } public void dropTable(String tableName) { String sql = "DROP TABLE IF EXISTS " + WARN; db.execSQL(sql); } public void createTable(String tableName) { db.execSQL(warnSql); } // 插入方法 public void insert(ContentValues values, String tableName) { db.insert(tableName, null, values); } // 查询方法 public Cursor query(String tableName) { Cursor c = db.query(tableName, null, null, null, null, null, "_ID asc"); return c; } // 查询方法 public Cursor queryByName(String name) { Cursor c = db.query(WARN, null, "u_title=?", new String[] { name }, null, null, "_ID desc"); return c; } // 修改 public void updata(ContentValues values, String name) { db.update(WARN, values, "u_title=?", new String[] { name }); } // 删除方法 public void del(String u_username) { db.delete(WARN, "u_title=?", new String[] { u_username }); } // 关闭数据库 public void close() { if (db != null) { db.close(); } } }
使用:
DBHelper dbHelper = new DBHelper(context); dbHelper.open(); dbHelper.del(dataList.get(cur).getTitle()); dbHelper.close();