标签:
//当数据库存在时:
String path = SDCardHelper.getSDCardRoot() + File.separator + "steven" + File.separator + "android_manual.db";
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READWRITE);
Cursor cursor = db.rawQuery("select * from android_basic order by id desc limit 0 , 15",null);
//当数据库不存在时:
String path = SDCardHelper.getSDCardRoot() + File.separator + "steven" + File.separator + "words.db";
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(path, null);
db.execSQL("create table tb_words (_id integer primary key autoincrement ,english , chinese)");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(path, null);
db.execSQL("create table tb_words (_id integer primary key autoincrement ,english , chinese)");
String path = SDCardHelper.getSDCardRoot() + File.separator + "steven" + File.separator + "words.db";
FLAG_AUTO_REQUERY
and FLAG_REGISTER_CONTENT_OBSERVER。
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item_listview_main,
cursor, new String[] { "news_title","news_addtime" },
new int[] { R.id.text_item_title, R.id.text_item_addtime });
注意:在绑定数据时,Cursor对象返回的记录集中必须包含一个叫"_id"的字段,否则将无法完成数据绑定。也就是说SQL语句不能是select name from tb_contacts.如果在数据表中没有"_id"字段,可以采用其他方法来处理。
那么数据库存到哪里?系统在手机内存中的/data/data/<package name>/databases目录中创建数据库文件。
public class MySQLiteDatabaseHelper{
private String DBPath = Environment.getExternalStorageDirectory().getAbsolutePath();
private String DBName = "android_manual.db";
private SQLiteDatabase db;
/**
* @作用: 构造方法。目的是创建数据库文件、创建SQLiteDatabase实例。
*/
public MySQLiteDatabaseHelper() {
getConnection();
}
/**
*
* @作用:获得一个数据库连接对象,即SQLiteDatabase对象。
* @return void
*/
public void getConnection() {
db = SQLiteDatabase.openDatabase(DBPath + File.seperator + DBName, null, 0);
}
/**
* @作用:执行带占位符的select语句,查询数据,返回Cursor
* @param sql
* @param selectionArgs
* @return Cursor
*/
public Cursor selectCursor(String sql, String[] selectionArgs) {
return db.rawQuery(sql, selectionArgs);
}
/**
* @作用:执行带占位符的select语句,返回结果集的个数
* @param sql
* @param selectionArgs
* @return int
*/
public int selectCount(String sql, String[] selectionArgs) {
Cursor cursor = db.rawQuery(sql, selectionArgs);
int count = cursor.getCount();
if (cursor != null) {
cursor.close();
}
return count;
}
/**
* @作用:执行带占位符的update、insert、delete语句,更新数据库,返回true或false
* @param sql
* @param bindArgs
* @return boolean
*/
public boolean updateData(String sql, String[] bindArgs) {
boolean flag = false;
try {
db.execSQL(sql, bindArgs);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* @作用:执行带占位符的select语句,返回多条数据,放进List集合中。
* @param sql
* @param selectionArgs
* @return List<Map<String, Object>>
*/
public List<Map<String, Object>> selectData(String sql,String[] selectionArgs) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Cursor cursor = db.rawQuery(sql, selectionArgs);
String[] cols_names = cursor.getColumnNames();
boolean flag = cursor.moveToFirst();
while (flag) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < cols_names.length; i++) {
String cols_value = cursor.getString(i);
map.put(cols_names[i], cols_value);
}
list.add(map);
flag = cursor.moveToNext();
}
if (cursor != null) {
cursor.close();
}
return list;
}
/**
* 作用:将cursor转成list集合
*
* @param cursor
* @return List<Map<String, Object>>
*/
public List<Map<String, Object>> cursorToList(Cursor cursor) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
String[] cols_names = cursor.getColumnNames();
while (cursor.moveToNext()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < cols_names.length; i++) {
Object cols_value = null;
switch (cursor.getType(i)) {
case 1:
cols_value = cursor.getInt(cursor
.getColumnIndex(cols_names[i]));
break;
case 2:
cols_value = cursor.getFloat(cursor
.getColumnIndex(cols_names[i]));
break;
case 3:
cols_value = cursor.getString(cursor
.getColumnIndex(cols_names[i]));
break;
case 4:
cols_value = cursor.getBlob(cursor
.getColumnIndex(cols_names[i]));
break;
default:
cols_value = null;
break;
}
map.put(cols_names[i], cols_value);
}
list.add(map);
}
if (cursor != null) {
cursor.close();
}
return list;
}
public void closeDb() {
if (db != null) {
db.close();
}
}
}
标签:
原文地址:http://blog.csdn.net/xdf0101/article/details/51918333