Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module export for mysql connection

I've been playing with Node.js and i was writing some test applications with node-mysql. I am trying to write a module that automatically establishes connections with the database so that i don't have to write the code for connection again and again. Here is what i have written:

var mysql = require('mysql');

module.exports = function(mysql) {
  var client = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '12345'
  });
  return client;
}

But when i try to import this file into my another *.js file, i get an error:

~/work : $ node queryInfo.js
/Users/socomo22/work/queryInfo.js:3
client.connect();
       ^
TypeError: undefined is not a function
    at Object.<anonymous> (/Users/socomo22/work/queryInfo.js:3:8)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Since i'm new to Node.js i'm not sure what i'm doing wrong. Please help!

queryInfo.js // Code that requires above stated module

var client = require('./connectMysql');

client.query("USE node");

client.query("INSERT INTO test(content) VALUES(?)", ['the content'],
  function(err, info) {
    if(err)
      return handle_error(err);
    console.log(info.insertId);
});


client.query('UPDATE test SET content = ?', ['new content'], function(err, info) {
  if(err)
    return handle_error(err);
  console.log(info.insertId);
});

client.end();
like image 789
Rohan Kumar Avatar asked Jan 14 '16 11:01

Rohan Kumar


People also ask

How to export MySQL database from command line?

Command-line is an efficient yet slightly complex way to export the MySQL database. It is suitable for both small and large databases. It requires the users to write some custom codes to export data using the mysqldump. Hevo Data provides a hassle-free & fully managed solution using its No-code Data Pipelines.

How do I connect to a MySQL data source?

This topic shows you how to connect to an MySQL data source from the Choose a Data Source or Choose a Destination page of the SQL Server Import and Export Wizard. There are several data providers that you can use to connect to MySQL.

How do I export a table from access to MySQL?

To export a table of data from an Access database to MySQL, follow these instructions: With an Access database opened, the navigation plane on the right should display, among other things, all the tables in the database that are available for export (if that is not the case, adjust the navigation plane's display settings).

How to perform a MySQL data export using phpMyAdmin?

You can use the following steps to perform a MySQL data export using the phpMyAdmin tool: Log in to cPanel & select the phpMyAdmin option from the databases section. Select the database you want to export and click on the Export tab. In the export tab, you can select a particular column or an entire table to export.


1 Answers

You can use this:

Config.js

var mysql      = require('mysql');
var config;
config = {
    mysql_pool : mysql.createPool({
        host     : 'hede',
        user     : 'hede',
        password : 'hede',
        database : 'hede'
    })
};
module.exports = config;

When you use this js. You can use like that;

var mysqlConf = require('config.js').mysql_pool;

    mysqlConf.getConnection(function (err, connection) {
        connection.query('{YOUR QUERY}' ,[{FIELDS}], function (err, rows) {
            connection.release();   //---> don't forget the connection release.
        });
    });
like image 168
Ayberk Anıl Atsız Avatar answered Oct 12 '22 20:10

Ayberk Anıl Atsız