
SQLite数据库管理 读取react-native-sqlite-storage 生成的db文件
SQLite数据库管理的工具类 ,使用,和react-native-sqlite-storage ,读取相同的储存文件。
·
react-native-sqlite-storage
生成的db文件在/data/data/<包名>/databases/key_cabinet.db;
在系统自带的文件管理是无法看到的。
android SQLite数据库管理 工具类
package com.db;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;
import com.facebook.react.bridge.ReactContext;
import java.io.File;
/**
* SQLite数据库管理
*/
public class SQLiteDBUtil {
/**
* 打开或者创建数据库
* @param name 数据库创建的路径
* 参数2 一般设置为null就可以了
* 路径: /storage/emulated/0/staff.db
* /data/data/com.facereactnative/databases/key_cabinet.db
*/
public static SQLiteDatabase openOrCreateDatabase(ReactContext getContext, String name){
// File dbfile = getContext.getDatabasePath(name);
// String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + name;
// return SQLiteDatabase.openOrCreateDatabase(path,null);
int openFlags = SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY;
Log.e("SQLiteDatabasepath", openFlags+"");
final String inFileName = "/data/data/com.facereactnative/databases/"+name;
File dbFile = new File(inFileName);
Log.e("SQLiteDatabasepath", dbFile.getAbsolutePath());
// SQLiteDatabase mydb = SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), null, openFlags);
// return mydb;
return SQLiteDatabase.openOrCreateDatabase(inFileName,null);
}
/**
* 执行语句
* @param db 数据库
* @param content 执行的语句
*
* (执行语句)创建表例子: "create table usertable(_id integer primary key autoincrement,sname text,snumber text)"
*/
public static void execSQL(SQLiteDatabase db, String content){
//执行SQL语句
db.execSQL(content);
}
/**
* 插入数据
* @param db 数据库
* @param table 表名
* @param cValue 插入的数据
*
* (执行语句)插入数据: "insert into userinfo(id,sname,age) values('1','xiaoxin','22')"
*/
public static void insert(SQLiteDatabase db, String table, ContentValues cValue) {
db.insert(table, null, cValue);
}
/**
* 查询表中所有数据
* @param db 数据库
* @param table 表名
*/
public static String query(SQLiteDatabase db, String table, String selction, String[] selArgs) {
String temp = "";
//查询获得游标
Cursor cursor = db.query(table, null, selction + "=?", selArgs, null, null, null);
//判断游标是否为空
while(cursor.moveToNext()){
temp += String.valueOf(cursor.getInt(0) + "*" + cursor.getInt(1)) + "*" + cursor.getString(2) + "*" + String.valueOf(cursor.getInt(3) + "=");
}
cursor.close();
return temp;
}
/**
* 删除
* 删除大于_index=15的所有值: (db, "userinfo", "_index>?", new String[]{"15"})
* 删除id=2和sname=xiaoxin的所有值: (db, "userinfo", "id=? and sname=?", new String[]{"2", "xiaoxin"})
* 删除_index=temp的所有值: (db, "userinfo", "_index = ?", new String[]{temp})
*
* (执行语句)删除该表的所有数据: "delete from 表名"
* (执行语句)删除_index=1的值: "delete from userinfo where _index=1"
*/
public static void deleteData(SQLiteDatabase db, String tableName, String key, String[] values) {
db.delete(tableName, key, values);
}
/**
* 更新
* @param contentValues 改的值
* @param key 条件带?的key
* @param value 条件值value
*
* (执行语句)更新数据: "update userinfo set sname = '华华' where _index = 18"
*/
public static void update(SQLiteDatabase db, String tableName, ContentValues contentValues, String key, String[] value){
db.update(tableName, contentValues, key, value);
}
}
直接用语句查询
SQLiteDatabase db = SQLiteDBUtil.openOrCreateDatabase(myContext,"key_cabinet.db");
String[] selArgs = new String['1'];
Cursor cursor = db.rawQuery("select name from sqlite_master;",null);
String a ="";
if (cursor.moveToFirst()) {
do {
int i = 0;
a=a+cursor.getString(i++)+";";
} while (cursor.moveToNext());
}
cursor.close();
更多推荐
所有评论(0)