Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

NodeJS MySQL डंप

लिखित रूप में कोड मेरे लिए फ़ाइल सहेजने के लिए भी नहीं मिला। ऐसा लगता है कि कुछ मुद्दे हैं। सुनिश्चित नहीं है कि यह वास्तविक कोड है या कॉपी पेस्ट में कुछ चीजें खो गई हैं। हालांकि, आपको जो मिला है उसके आधार पर:

एक बड़ी बात यह है कि आप कनेक्शन के साथ अपने कोड में डेटाबेस से कभी भी कनेक्ट नहीं होते हैं। कनेक्ट ()।

एक बार कनेक्ट होने के बाद आप जिस कोड को चलाना चाहते हैं, वह कनेक्शन के अंदर होना चाहिए। कनेक्ट () कॉलबैक। उदा.

connection.connect(function (err, empty) {
    if (err)
        throw new Error ('Panic');

    // if no error, we are off to the races...
}

हालांकि, भले ही आप कनेक्शन कॉलबैक प्राप्त करने के अंदर अपनी आखिरी पंक्तियों को लपेटने के लिए अपने कोड को तुरंत दोबारा प्रतिक्रिया दें, फिर भी आपको समस्याएं होंगी, क्योंकि आप विभिन्न SQL कॉल किए जाने से पहले कनेक्शन को नष्ट कर रहे हैं, इसलिए आप स्थानांतरित करना चाहेंगे किसी प्रकार के अंतिम कॉलबैक में कोड।

ऐसा करने के बाद भी, आपके पास अभी भी एक खाली फ़ाइल होगी, क्योंकि आप अपने 'शो टेबल' कॉलबैक से save_backup को कॉल कर रहे हैं, बजाय इसके कि आपने वास्तव में इसे आंतरिक कॉलबैक के माध्यम से पॉप्युलेट किया है, जहां आपको टेबल बनाएं स्टेटमेंट मिलता है और पॉप्युलेट करता है बैकअप संपत्ति।

यह आपके कोड का न्यूनतम पुनर्लेखन है जो वह करेगा जो आप करना चाहते हैं। ध्यान देने योग्य एक महत्वपूर्ण बात "काउंटर" है जो यह प्रबंधित करती है कि फ़ाइल को कब लिखना है और कनेक्शन को बंद करना है। यदि यह मेरा होता, तो मैं अन्य परिवर्तन करता, जिनमें शामिल हैं:

  • 'मैं' के बजाय 'स्वयं' का उपयोग करना
  • for (... in ...) सिंटैक्स के बजाय लूप के लिए संख्यात्मक का उपयोग करना
  • मेरे अपने कॉलबैक होने से (गलती, सामान) का नोड सम्मेलन गिर जाता है
  • एक अधिक महत्वपूर्ण परिवर्तन यह है कि मैं वादों का उपयोग करने के लिए इसे फिर से लिखूंगा, क्योंकि ऐसा करने से आपको गहरे नेस्टेड कॉलबैक के साथ निहित भ्रम के साथ कुछ दुख हो सकता है। मुझे व्यक्तिगत रूप से क्यू लाइब्रेरी पसंद है, लेकिन यहां कई विकल्प हैं।

आशा है कि इससे मदद मिली।

var mysql_backup = function(){
    this.backup = '';
    this.mysql = require('mysql');

    this.init = function(){
        this.connection = this.mysql.createConnection({
            user     : 'root',
            password : 'root',
            database : 'test'
        });

    };

    this.query = function(sql, callback) {
        this.connection.query(sql, function (error, results, fields) {
            if (error) {
                throw error;
            }
            if (results.length  > 0) {
                callback(results);
            }
        });
    };

    this.get_tables = function(callback){
        var counter = 0;
        var me = this;
        this.query('SHOW TABLES',
            function(tables) {
                for (table in tables){
                    counter++;
                    me.query(
                        'SHOW CREATE TABLE ' + tables[table].Tables_in_mvc,
                        function(r){
                            for (t in r) {
                                me.backup += "DROP TABLE " + r[t].Table + "\n\n";
                                me.backup += r[t]["Create Table"] + "\n\n";
                            }
                            counter--;
                            if (counter === 0){
                                me.save_backup();
                                me.connection.destroy();

                            }
                        }
                    )
                }
            });
    };

    this.save_backup = function(){
        var fs = require('fs');
        fs.writeFile("./backup_test.txt", this.backup, function(err) {
            if(err) {
                console.log(err);
            } else {
                console.log("The file was saved!");
            }
        });
    }

};

var db = new mysql_backup;
db.init();
db.connection.connect(function (err){
    if (err) console.log(err);
    db.get_tables(function(x){;});

});

अपडेट करें:यदि आप उत्सुक हैं, तो यहां वादों का उपयोग करते हुए एक भारी-भरकम कार्यान्वयन है। ध्यान दें कि क्यू वादा पुस्तकालय कार्यों की व्याख्या करने वाली टिप्पणियों के बिना, यह मूल संस्करण से कुछ हद तक छोटा है और अधिक व्यापक त्रुटि प्रबंधन भी प्रदान करता है।

var MysqlBackup = function(connectionInfo, filename){

    var Q = require('q');
    var self = this;
    this.backup = '';
    // my personal preference is to simply require() inline if I am only
    // going to use something a single time. I am certain some will find
    // this a terrible practice
    this.connection = require('mysql').createConnection(connectionInfo);

    function getTables(){
        //  return a promise from invoking the node-style 'query' method
        //  of self.connection with parameter 'SHOW TABLES'.
        return Q.ninvoke(self.connection,'query', 'SHOW TABLES');
    };

    function doTableEntries(theResults){

        // note that because promises only pass a single parameter around,
        // if the 'denodeify-ed' callback has more than two parameters (the
        // first being the err param), the parameters will be stuffed into
        // an array. In this case, the content of the 'fields' param of the
        // mysql callback is in theResults[1]

        var tables = theResults[0];
        // create an array of promises resulting from another Q.ninvoke()
        // query call, chained to .then(). Note that then() expects a function,
        // so recordEntry() in fact builds and returns a new one-off function
        // for actually recording the entry (see recordEntry() impl. below)

        var tableDefinitionGetters = [];
        for (var i = 0; i < tables.length ; i++){
            //  I noticed in your original code that your Tables_in_[] did not
            //  match your connection details ('mvc' vs 'test'), but the below
            //  should work and is a more generalized solution
            var tableName = tables[i]['Tables_in_'+connectionInfo.database];

            tableDefinitionGetters.push(Q.ninvoke(self.connection, 'query', 'SHOW CREATE TABLE ' + tableName)
                                        .then(recordEntry(tableName)) );
        }

        // now that you have an array of promises, you can use Q.allSettled
        // to return a promise which will be settled (resolved or rejected)
        // when all of the promises in the array are settled. Q.all is similar,
        // but its promise will be rejected (immediately) if any promise in the
        // array is rejected. I tend to use allSettled() in most cases.

        return Q.allSettled(tableDefinitionGetters);
    };

    function recordEntry (tableName){
        return function(createTableQryResult){
            self.backup += "DROP TABLE " + tableName + "\n\n";
            self.backup += createTableQryResult[0][0]["Create Table"] + "\n\n";
        };
    };

    function saveFile(){
        // Q.denodeify return a promise-enabled version of a node-style function
        // the below is probably excessively terse with its immediate invocation
        return (Q.denodeify(require('fs').writeFile))(filename, self.backup);
    }

    // with the above all done, now you can actually make the magic happen,
    // starting with the promise-return Q.ninvoke to connect to the DB
    // note that the successive .then()s will be executed iff (if and only
    // if) the preceding item resolves successfully, .catch() will get
    // executed in the event of any upstream error, and finally() will
    // get executed no matter what.

    Q.ninvoke(this.connection, 'connect')
    .then(getTables)
    .then(doTableEntries)
    .then(saveFile)
    .then( function() {console.log('Success'); } )
    .catch( function(err) {console.log('Something went awry', err); } )
    .finally( function() {self.connection.destroy(); } );
};

var myConnection = {
    host     : '127.0.0.1',
    user     : 'root',
    password : 'root',
    database : 'test'
};

// I have left this as constructor-based calling approach, but the
// constructor just does it all so I just ignore the return value

new MysqlBackup(myConnection,'./backup_test.txt');



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. 'MySql.Data.MySqlClient' ADO.NET प्रदाता के लिए कोई इकाई फ्रेमवर्क प्रदाता नहीं मिला

  2. SQL इंजेक्शन जो mysql_real_escape_string () के आसपास हो जाता है

  3. डुप्लीकेट कुंजी अपडेट पर डालने के समान

  4. एक MySQL स्टेटमेंट के अंदर PHP वेरिएबल को कैसे शामिल करें?

  5. QUARTER () उदाहरण – MySQL