简体   繁体   English

使用 mysql node.js 驱动程序以 JSON 格式获取整个数据库

[英]Using mysql node.js driver to get an entire database as JSON

I'm working on creating a JavaScript file to get a JSON dump of an entire MySQL database, running on server side.我正在创建一个 JavaScript 文件,以获取在服务器端运行的整个 MySQL 数据库的 JSON 转储。 I found and am using the MySQL driver for node.js ( https://www.npmjs.com/package/mysql ) for queries, it's been straight forward enough to start.我发现并正在使用用于 node.js ( https://www.npmjs.com/package/mysql ) 的 MySQL 驱动程序进行查询,它已经足够直接开始。 My issue is that I need to call multiple queries and get the results from all of them to put into a single JSON file and I can't quite get that to work.我的问题是我需要调用多个查询并从所有查询中获取结果以放入单个 JSON 文件中,但我无法让它正常工作。 I'm entirely new to JavaScript (basically never touched it before now) so it's probably a relatively simple solution that I'm just missing.我对 JavaScript 完全陌生(以前基本上从未接触过它),所以这可能是一个相对简单的解决方案,我只是缺少。

Currently I do a query of 'SHOW TABLES' to get a list of all the tables (this can change so I can't just assume a constant list).目前我查询“SHOW TABLES”以获取所有表的列表(这可能会改变,所以我不能只假设一个常量列表)。 I then just want to basically loop through the list and call 'SELECT * from table_name' for each table, combining the results as I go to get one big JSON.然后,我只想基本上遍历列表并为每个表调用“SELECT * from table_name”,将结果组合起来以获得一个大的 JSON。 Unfortunately I haven't figured out how to get the code to finish all the queries before trying to combine them, thus retuning 'undefined' for all the results.不幸的是,在尝试组合它们之前,我还没有弄清楚如何让代码完成所有查询,从而为所有结果重新调整“未定义”。 Here is what I currently have:这是我目前拥有的:

var mysql = require('mysql');
var fs = require('fs');

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'pass',
    database: 'test_data'
});

connection.connect();

connection.query('SHOW TABLES;', function(err, results, fields)
{
    if(err) throw err;

    var name = fields[0].name;
    var database_json = get_table(results[0][name]);
    for (i = 1; i < results.length; i++)
    {
        var table_name = results[i][name];
        var table_json = get_table(table_name);
        database_json = database_table_json.concat(table_json);
    }
    fs.writeFile('test_data.json', JSON.stringify(database_json), function (err)
    {
        if (err) throw err;
    });
    connection.end();
});

function get_table(table_name)
{
    connection.query('select * from ' + table_name + ';', function(err, results, fields) {
        if(err) throw err;

        return results;
    });
}

This gets the table list and goes through all of it with no issue, and the information returned by the second query is correct if I just do a console.log(results) inside the query, but the for loop just keeps going before any query is completed and thus 'table_json' just ends up being 'undefined'.这将获取表列表并毫无问题地遍历所有内容,如果我只是在查询中执行 console.log(results) ,则第二个查询返回的信息是正确的,但是 for 循环只会在任何查询之前继续运行已完成,因此“table_json”最终只是“未定义”。 I really think this must be an easy solution (probably something with callbacks which I don't quite understand fully yet) but I keep stumbling.我真的认为这一定是一个简单的解决方案(可能是一些我还不太完全理解的回调),但我一直在磕磕绊绊。

Thanks for the help.谢谢您的帮助。

I'm guessing that this is for some sort of maintenance type function and not a piece that you need for your application.我猜这是用于某种维护类型的功能,而不是您的应用程序所需的部分。 You're probably safe to do this asynchronously.异步执行此操作可能是安全的。 This module is available here: https://github.com/caolan/async该模块可在此处获得: https : //github.com/caolan/async

You can also use Q promises, available here: https://github.com/kriskowal/q您还可以使用 Q 承诺,可在此处获得: https : //github.com/kriskowal/q

This answer: describes both approaches pretty well: Simplest way to wait some asynchronous tasks complete, in Javascript?这个答案:很好地描述了这两种方法: Simplest way to wait some asynchronous tasks complete, in Javascript?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM