首页 > android > android安卓如何操作sqllite,增删改查+自定义类

android安卓如何操作sqllite,增删改查+自定义类

2015年11月27日 发表评论 阅读评论

package util.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import data.config.Constants;

public class MysqlDo {

private MysqlDb mysqldb = null;
private SQLiteDatabase database = null;
private boolean flag = false;

public class MysqlDb extends SQLiteOpenHelper {

public MysqlDb(Context context, int version, String db) {
super(context, db, null, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
“Create table user_avatar_name(id integer primary key autoincrement,uid int(11) NOT NULL DEFAULT 0,avatar varchar(255),nickname varchar(255))”);
db.execSQL(“create index if not exists key1 on user_avatar_name(uid)”);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

}

public MysqlDo(Context context, int version) {
try {
/*
String db = null;
if (Constants.sdCardExist == true) {
db = Constants.baseFilePath + “dataBase”;
File dbDirectory = new File(db);
if (!dbDirectory.exists()) {
dbDirectory.mkdirs();
}
}
if (db != null) {
db = db + File.separator + Constants.sqlDbName;
} else {
db = Constants.sqlDbName;
}
*/
mysqldb = new MysqlDb(context, version, Constants.sqlDbName);
} catch (Exception e) {
} finally {
if (database != null) {
database.close();
}
}
}

public boolean addRecord(String sql, Object[] mValue) {
flag = false;
try {
database = mysqldb.getWritableDatabase();
database.execSQL(sql, mValue);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (database != null) {
database.close();
}
}
return flag;
}

public boolean deleteRecord(String sql) {
flag = false;
try {
database = mysqldb.getWritableDatabase();
database.execSQL(sql);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (database != null) {
database.close();
}
}
return flag;
}

public boolean updateRecord(String sql, Object[] mValue) {
flag = false;
try {
database = mysqldb.getWritableDatabase();
database.execSQL(sql, mValue);
flag = true;
} catch (Exception e) {
} finally {
if (database != null) {
database.close();
}
}
return flag;
}

public Map<String, String> viewSingleRecord(String sql) {
Map<String, String> map = new HashMap<String, String>();
try {
database = mysqldb.getReadableDatabase();
Cursor cursor = database.rawQuery(sql, null);
if (cursor.getCount() > 0) {
int columns = cursor.getColumnCount();
while (cursor.moveToNext()) {
for (int i = 0; i < columns; i++) {
String cols_name = cursor.getColumnName(i);
String cols_value = cursor.getString(cursor.getColumnIndex(cols_name));
if (cols_value == “”) {
cols_value = “”;
}
map.put(cols_name, cols_value);
}
}
cursor.close();
return map;
} else {
return null;
}
} catch (Exception e) {
} finally {
if (database != null) {
database.close();
}
}
return null;
}

public List<Map<String, Object>> viewListRecord(String sql) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
database = mysqldb.getReadableDatabase();
Cursor cursor = database.rawQuery(sql, null);
if (cursor.getCount() > 0) {
int columns = cursor.getColumnCount();
while (cursor.moveToNext()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < columns; i++) {
String cols_name = cursor.getColumnName(i);
String cols_value = cursor.getString(cursor.getColumnIndex(cols_name));
if (cols_value == “”) {
cols_value = “”;
}
map.put(cols_name, cols_value);
}
list.add(map);
}
cursor.close();
return list;
} else {
return null;
}
} catch (Exception e) {
} finally {
if (database != null) {
database.close();
}
}
return null;
}

}

 

上面是自定义的操作类

下面是使用方法:

private MysqlDo mysqlDo = null;

mysqlDo = new MysqlDo(context, Constants.mysqlVersion);

 

Map<String, String> rs = mysqlDo.viewSingleRecord(
“select * from user_avatar_name where uid=” + obj.getInt(“uid”) + ” limit 1″);
if (rs != null) {
mysqlDo.updateRecord(“update user_avatar_name set avatar=?,nickname=? where uid=?”,
new Object[] { obj.getString(“avatar”), obj.getString(“nickname”),
obj.getInt(“uid”) });
} else {
mysqlDo.addRecord(“insert into user_avatar_name(uid,avatar,nickname) values(?,?,?)”,
new Object[] { obj.getInt(“uid”), obj.getString(“avatar”),
obj.getString(“nickname”) });
}

分类: android 标签: ,
  1. 本文目前尚无任何评论.
您必须在 登录 后才能发布评论.
css.php