SQLiteHelper .class (升级的时候,做点小技巧)
package com.keyue.qlm.util;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLiteHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "qlm.db";
private static final int DATABASE_VERSION = 1;
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS user" +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT,userid VARCHAR, username VARCHAR, userimage VARCHAR,userphone VARCHAR,userwd VARCHAR,userjd VARCHAR,useraddress VARCHAR,useremail VARCHAR)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_NAME); //这里做点小技巧
onCreate(db);
}
}
DBManager .class
package com.keyue.qlm.util;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DBManager {
private SQLiteHelper helper;
private SQLiteDatabase db;
public DBManager(Context context) {
helper = new SQLiteHelper(context);
}
public void saveorupdate(String sql){
db.beginTransaction();
try {
db.execSQL(sql);
db.setTransactionSuccessful();
}catch (Exception e) {
// TODO: handle exception
}finally{
db.endTransaction();
}
}
public List<Object[]> sel(String sql,Object[] lies){
ArrayList<Object[]> objects = new ArrayList<Object[]>();
Cursor c = db.rawQuery(sql, null);
while (c.moveToNext()) {
Object[] objects2 = new Object[lies.length];
for(int i=0;i<lies.length;i++){
objects2[i]=c.getString(c.getColumnIndex(lies[i].toString()));
}
objects.add(objects2);
}
c.close();
return objects;
}
public void closedb(){
db.close();
}
public void opendb(){
db = helper.getWritableDatabase();
}
}