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

androd SQLite 自己写的数据库操作类

package com.db;


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) {

//这儿添加建表语句

? ? ? ? ? ? ? ?db.execSQL("你的SQL语句");

}


@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, null, 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.getW