import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteDatabase.CursorFactory;
import android.database.sqlite.sqliteOpenHelper;
import android.util.Log;
public class MyHelper extends sqliteOpenHelper{
};
public MyHelper(Context context,String name,
CursorFactory factory,int version) {
super(context,name,factory,version);
}
@Override
public void onCreate(sqliteDatabase db) {
//这儿添加建表语句
} @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { } /** * 执行一条sql语句 * @param sql */ public void executesql(String sql) { sqliteDatabase db = null; try{ db = this.getWritableDatabase(); db.execsql(sql); }catch(Exception e){ e.printStackTrace(); }finally{ db.close(); } } /** * 执行一条sql语句,返回一个游标 * @param sql * @return */ public Cursor executesqlwithCursorReturn(String sql) { Cursor retCursor = null; sqliteDatabase db = null; try{ db = this.getReadableDatabase(); retCursor = db.rawQuery(sql,null); }catch(Exception e){ Log.d("qiaoning_debug","DB ERROR"); e.printStackTrace(); } return retCursor; } /** * 执行一条sql语句,判断是否存在该记录,存在返回true,不存在返回false * @param sql * @return */ public boolean isExist(String sql) { boolean retBoolean = false; Cursor c = null; sqliteDatabase db = null; try{ db = this.getReadableDatabase(); c = db.rawQuery(sql,null); if(c.getCount() == 0) { retBoolean = false; } else { retBoolean = true; } }catch(Exception e){ Log.e("ERROR",e.toString()); //e.printStackTrace(); }finally{ db.close(); } return retBoolean; } /** * * @param table * @param columns * @param selection * @param selectionArgs * @param groupBy * @param having * @param orderBy * @return */ public Cursor select(String table,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy) { sqliteDatabase db = null; Cursor cursor = null; try { db = this.getReadableDatabase(); cursor = db.query(table,columns,selection,selectionArgs,groupBy,having,orderBy); } catch (Exception e) { // TODO: handle exception }finally{ db.close(); } return cursor; } /** * * @param table * @param fields * @param values * @return the row ID of the newly inserted row,or -1 if an error occurred */ public long insert(String table,String[] fields,String[] values) { sqliteDatabase db = this.getWritableDatabase(); ContentValues cv = new ContentValues(); for(int i = 0;i < fields.length;i ++) { cv.put(fields[i],values[i]); } return db.insert(table,null,cv); } /** * * @param table * @param cv * @return the row ID of the newly inserted row,or -1 if an error occurred */ public long insertWithContentValues(String table,ContentValues cv) { sqliteDatabase db = this.getWritableDatabase(); long i = 0; i = db.insert(table,cv); db.close(); return i; } /** * * @param table * @param cv * @param whereClause * @param whereArgs * @return the number of rows affected */ public int updateTableWithContentValues(String table,ContentValues cv,String whereClause,String[] whereArgs) { sqliteDatabase db = this.getWritableDatabase(); int i = 0; i = db.update(table,cv,whereClause,whereArgs); return i; } /** * @param table * @param updateFields * @param updateValues * @param whereClause * @param whereArgs * @return */ public int update(String table,String[] updateFields,String[] updateValues,String[] whereArgs) { sqliteDatabase db = this.getWritableDatabase(); ContentValues cv = new ContentValues(); for(int i=0;i<updateFields.length;i++) { cv.put(updateFields[i],updateValues[i]); } return db.update(table,whereArgs); } /** * @param table * @param where * @param whereArgs * @return */ public int delete(String table,String where,String[] whereArgs) { sqliteDatabase db = this.getWritableDatabase(); return db.delete(table,where,whereArgs); } @Override public synchronized void close() { // TODO Auto-generated method stub super.close(); } } 原文链接:https://www.f2er.com/sqlite/202432.html