因爲在android開發中涉及到數據庫方面的開發的過程當中遇到不少麻煩和困擾,就着手寫了個android數據庫調試管理工具FastAndr-dbms,但願對你們有點幫助android
二話不說,先上圖git
界面仍是比較小清新....github
缺陷web
compile 'cn.hotapk:fastandr_dbms:0.4.0'
複製代碼
若是gson衝突spring
compile ('cn.hotapk:fastandr_dbms:0.4.0'){
exclude group: 'com.google.code.gson'
}
複製代碼
<uses-permission android:name="android.permission.INTERNET" />
複製代碼
try {
FDbManager.init(this).startServer();
} catch (Exception e) {
e.printStackTrace();
}
複製代碼
默認端口號爲8888,如要配置,按如下方式sql
FDbManager fDbManager= FDbManager.init(this);
fDbManager.setPort(9999);
fDbManager.startServer();
複製代碼
瀏覽器打開 http://xxx.xxx.xxx:8888 便可數據庫
1. http內網服務 因爲要使用到內網數據傳輸,全部要用到http服務. 而後就選用了以前開源過的 FHttpServer http服務框架,這框架可讓你使用得更簡單,方便. 使用上相似spring,這框架的好處就很少說了...瀏覽器
/**
* @author laijian
* @version 2017/11/25
* @Copyright (C)上午1:19 , www.hotapk.cn
* web數據操做
*/
public class FDbController {
/**
* 獲取數據庫列表
* @return
*/
@ResponseBody
@RequestMapping("getDbList")
public ResponseData getDbList() {
return FDbService.getDbList();
}
/**
* 獲取table列表
* @param dbname
* @return
*/
@ResponseBody
@RequestMapping("getTables")
public ResponseData getTables(@RequestParam("dbname") String dbname) {
return FDbService.getTableList(dbname);
}
...
複製代碼
2. 獲取db列表 獲取db數據列表bash
List<String> templs = Arrays.asList(FDbManager.getAppContext().databaseList());
List<String> rows = new ArrayList<>();
for (int i = 0; i < templs.size(); i++) {
if (!templs.get(i).matches(".*journal*.|undefined|.*xml|.*shared_prefs*.")) {
rows.add(templs.get(i));
}
}
複製代碼
Context.databaseList() sdk提供的方法就能夠獲取數據庫列表了 獲取數據庫列表時,去除一些系統沒用的數據庫表網絡
3. 獲取sharedprefs數據列表
public static String getSharePrefsRootPath() {
return FDbManager.getAppContext().getApplicationInfo().dataDir + "/shared_prefs";
}
/**
* 獲取SharedPreference xml數據
*
* @return
*/
public static List<String> getSharedPreferenceXMl() {
ArrayList<String> tags = new ArrayList<>();
String rootPath = getSharePrefsRootPath();
File root = new File(rootPath);
if (root.exists()) {
for (File file : root.listFiles()) {
String fileName = file.getName();
if (fileName.endsWith(".xml")) {
tags.add(fileName);
}
}
}
Collections.sort(tags);
return tags;
}
複製代碼
Context.dataDir + "/shared_prefs" sdk提供的方法能夠獲得存放SharedPreference的路徑
4. 獲取數據庫表名
/**
* 獲取數據庫全部表名
*
* @param database SQLiteDatabase
* @return
*/
public static List<String> getAllTableName(SQLiteDatabase database) {
List<String> tabs = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
try {
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
tabs.add(cursor.getString(0));
cursor.moveToNext();
}
}
} catch (Exception e) {
} finally {
if (cursor != null) {
cursor.close();
}
}
return tabs;
}
複製代碼
"SELECT name FROM sqlite_master WHERE type='table'" sqlite語句可獲取表名
5. 獲取表數據
先獲取表字段及類型
public static List<FieldInfor> getAllTablefield(SQLiteDatabase database, String tableName) {
List<FieldInfor> fields = new ArrayList<>();
Cursor cursor = database.rawQuery("PRAGMA table_info([" + tableName + "])", null);
if (cursor != null) {
cursor.moveToFirst();
if (cursor.getCount() > 0) {
do {
FieldInfor fieldInfor = new FieldInfor();
for (int i = 0; i < cursor.getColumnCount(); i++) {
final String columnName = cursor.getColumnName(i);
switch (columnName) {
case "name":
fieldInfor.setTitle(cursor.getString(i));
break;
case "type":
fieldInfor.setType(cursor.getString(i));
break;
case "pk":
fieldInfor.setPrimary(cursor.getInt(i) == 1);
break;
default:
}
}
fields.add(fieldInfor);
} while (cursor.moveToNext());
}
cursor.close();
}
複製代碼
"PRAGMA table_info([" + tableName + "])" sqlite語句能夠獲取表字段屬性等
名稱 | 描述 |
---|---|
name | 表字段的名稱 |
type | 表字段類型 |
pk | 是否爲主鍵 |
而後獲取表數據
private static void getCursorData(Cursor cursor, List<FieldInfor> allTablefield, List<Map<String, Object>> datas) {
if (cursor != null && cursor.moveToFirst()) {
do {
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < allTablefield.size(); i++) {
switch (allTablefield.get(i).getType()) {
case FConstant.BLOB:
map.put(allTablefield.get(i).getTitle(), blobToString(cursor.getBlob(i)));
break;
case FConstant.INTEGER:
map.put(allTablefield.get(i).getTitle(), cursor.getInt(i));
case FConstant.REAL:
map.put(allTablefield.get(i).getTitle(), cursor.getDouble(i));
default:
map.put(allTablefield.get(i).getTitle(), cursor.getString(i));
break;
}
}
datas.add(map);
} while (cursor.moveToNext());
}
}
複製代碼
sqlite 數據庫有如下幾種存儲類型
存儲類 | 描述 |
---|---|
NULL | 值是一個NULL 值。 |
INTEGER | 值是一個帶符號的整數,根據值的大小存儲在一、二、三、四、6 或8 字節中。 |
REAL | 值是一個浮點值,存儲爲8 字節的IEEE 浮點數字。 |
TEXT | 值是一個文本字符串,使用數據庫編碼(UTF-八、UTF-16BE 或UTF-16LE)存儲。 |
6. 獲取sharedprefs數據
/**
* 獲取pref的數據
*
* @param prefname
* @return
*/
public static List<Map<String, Object>> getPrefData(String prefname) {
SharedPreferences preferences = FDbManager.getAppContext().getSharedPreferences(prefname.replace(".xml", ""), Context.MODE_PRIVATE);
Map<String, ?> entries = preferences.getAll();
List<Map<String, Object>> datas = new ArrayList<>();
for (Map.Entry<String, ?> entry : entries.entrySet()) {
Map<String, Object> map = new HashMap<>();
map.put("Key", entry.getKey());
map.put("Value", entry.getValue().toString());
if (entry.getValue() instanceof Integer) {
map.put("dataType", FConstant.INTEGER);
} else if (entry.getValue() instanceof Long) {
map.put("dataType", FConstant.LONG);
} else if (entry.getValue() instanceof Float) {
map.put("dataType", FConstant.FLOAT);
} else if (entry.getValue() instanceof Boolean) {
map.put("dataType", FConstant.BOOLEAN);
} else if (entry.getValue() instanceof Set) {
map.put("dataType", FConstant.STRING_SET);
} else {
map.put("dataType", FConstant.TEXT);
}
datas.add(map);
}
return datas;
}
複製代碼
以上是FastAndr-dbms框架的所有內容,謝謝觀看,歡迎使用. 同時但願各位在使用中遇到什麼問題或建議能夠用如下聯繫方式進行反饋
github地址(感興趣的話,不妨點贊支持下)