Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SQL & Node.JS test program
- * Joseph Burger
- *
- * Notes:
- * According to documentation @ "https://github.com/felixge/node-mysql#terminating-connections",
- * I should call pool.end(). However if I do, the rest of the program doesn't run, and the program
- * also hangs on the command line - unacceptable.
- *
- * After hours, I arrived at this solution: rather than call connection.release() as the documentation
- * recommended, I call destroy (and not end() either). Additionally I don't call connection.release().
- *
- * It's probably a bad answer to the problem but at least it works.
- *
- * A bigger question is how the heck do I get results back from these asynchronous callbacks?
- * Sure, I can call functions from within callbacks. But that requires me to plan my programming
- * around that. Ugh.
- *
- */
- var mysql = require('mysql');
- var pool = mysql.createPool({
- host : 'localhost',
- user : '*******',
- password : '*******',
- database : 'users',
- });
- var fns = ["lina", "lanna", "cheryl", "heinrick", "bob", "larry", "william", "alice", "stewey", "jiang"],
- lns = ["anderson", "bojangles", "williamson", "polerson", "dottson", "bjarnes", "gandertronf"];
- var post = { username: fns[Math.floor(Math.random() * fns.length)],
- password: lns[Math.floor(Math.random() * lns.length)] }
- function complex()
- {
- pool.getConnection(function(err, connection)
- {
- console.log("Inserting: " + post.username + "\t" + post.password);
- connection.query('insert into `user_info` SET ?', post,
- function(err, result) {
- if (err) console.log (err);
- connection.destroy();
- getNum(); // +=+=+=+=+=
- console.log("\t\t-!-!- Exiting complex()");
- });
- });
- }
- function getNum()
- {
- pool.getConnection(function(err, connection){
- connection.query('select * from `user_info`', function (error, rows, fields) {
- if (error) throw error;
- ident = rows[Math.floor(Math.random() * rows.length)].ID;
- console.log("Number of rows:\t" + rows.length);
- console.log("ID to be deleted:\t" + ident);
- connection.destroy();
- del(ident); // +=+=+=+=+=
- console.log("\t\t-!-!- Exiting getNum()");
- });
- });
- }
- function del(ident)
- {
- post = ['user_info', 'id', ident];
- pool.getConnection(function(err, connection)
- {
- var query = connection.query('DELETE FROM ?? WHERE ?? = ?', post, function(err, result)
- {
- if (err) throw err;
- console.log('deleted ' + result.affectedRows + ' rows');
- connection.destroy();
- console.log("\t\t-!-!- Exiting del()");
- });
- console.log(query.sql);
- });
- }
- complex(); // +=+=+=+=+=
- console.log("Heya bud! It's over!");
- //pool.end();
- // Output:
- /*
- Heya bud! It's over!
- Inserting: lina polerson
- -!-!- Exiting complex()
- Number of rows: 72
- ID to be deleted: 2
- -!-!- Exiting getNum()
- DELETE FROM `user_info` WHERE `id` = 2
- deleted 1 rows
- -!-!- Exiting del()
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement