如何导出(转储)WebSQL数据 [英] How to export (dump) WebSQL data
问题描述
我正在使用使用WebSQL存储历史数据的Chrome扩展程序。
作为WebSQL,数据库存储在客户端。
I'm working on a Chrome Extension that uses WebSQL to store historical data. Being WebSQL, the DB is stored on the client.
我想添加一个选项来导出/导入这样的数据,以便用户可以共享/与其他用户或其他PC使用此数据。
I'd like to add an option to export/import such data so that the user can share/use this data with other users, or with other PCs.
这是我们在仅客户端数据库的第一步,所以我不知道该怎么做。
我正在考虑将数据库转换为一个巨大的 json
字符串,用户可以复制/粘贴,但看起来不是很友好。
These are my first steps on a client-only database, so I wonder how to do this.
I was thinking to convert the DB to a huge json
string that the user can copy/paste but doesn't look very user-friendly.
有没有更好的解决方案?
Is there any better solution?
推荐答案
我有一个单表转储解决方案在几天前我写的 HTML5数据库客户端上。
I got a single table dump solution working on a HTML5 database client I wrote a few days ago.
查看 http://html5db.desalasworks.com/script。 js 并向下滚动到SqlClient.exportTable,这有一个需要扩展以覆盖整个数据库的示例。
Check out http://html5db.desalasworks.com/script.js and scroll down to SqlClient.exportTable, this has an example that needs to be expanded to cover the whole database.
步骤是:
步骤1:创建模式:
SELECT sql FROM sqlite_master
步骤2:获取表格列表:
Step 2: Get a list of tables:
SELECT tbl_name from sqlite_master WHERE type = 'table'
步骤3:循环遍历每个人,并创建一个INSERT脚本,结果
Step 3: Loop through each of them and create an INSERT script with the results
transaction.executeSql("SELECT * FROM " + _tbl_name + ";", [],
function(transaction, results) {
if (results.rows) {
for (var i = 0; i < results.rows.length; i++) {
var row = results.rows.item(i);
var _fields = [];
var _values = [];
for (col in row) {
_fields.push(col);
_values.push('"' + row[col] + '"');
}
_exportSql += ";\nINSERT INTO " + _tbl_name + "(" + _fields.join(",") + ") VALUES (" + _values.join(",") + ")";
}
}
}
);
希望这是有用的。
strong> UPDATE JAN 2016 - 全部数据库导出
UPDATE JAN 2016 - WHOLE DB EXPORT
我有一个JS websqldump库,您也可以从github下载。
I've got a JS websqldump library that you can download from github as well.
这个将导出整个数据库。查看代码:
This one will export the whole database. Check out the code on:
https:// github .com / sdesalas / websqldump
使用方法如下
websqldump.export({
database: 'NorthwindLite',
success: function(sql) {alert(sql);}
});
这篇关于如何导出(转储)WebSQL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!