Created
November 4, 2016 07:44
-
-
Save lucnap/e3ba7113291c17c1d1a314af3d7e4a19 to your computer and use it in GitHub Desktop.
nodejs sqlite mysql create table select
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sqlite3 = require("sqlite3").verbose(); | |
var db = new sqlite3.Database('database/pagine.sqlite3'); | |
var async = require("async"); | |
var deasync = require("deasync"); | |
var mysql = require('mysql'); | |
var connection = mysql.createConnection({ | |
host : 'localhost', | |
user : 'root', | |
password : '123456', | |
database : 'test3' | |
}); | |
connection.connect(); | |
var query; | |
var getSqliteTables = function(db, callback) { | |
query = "select * from sqlite_master where type = 'table'"; | |
db.all(query, function(err, rows) { | |
if (err) callback(err); | |
callback(rows); // rows[x].name | |
}); | |
}; | |
var getTableInfo = function(db, table, callback) { | |
query = "PRAGMA table_info(" + table + ")"; | |
db.all(query, function(err, rows) { | |
if (err) callback(err); | |
callback(rows); | |
}); | |
}; | |
var rowdef = ""; | |
var tabledef = ""; | |
var i = 0; | |
var createTables = function(callback) { | |
getSqliteTables(db, function(tables) { | |
tables.forEach(function(table) { | |
getTableInfo(db, table.name, function(cols) { | |
i = 0; | |
tabledef = "CREATE TABLE IF NOT EXISTS " + table.name + " ("; | |
cols.forEach(function(col) { | |
i++; | |
rowdef = col.name; | |
rowdef = rowdef + ' ' + col.type; | |
if (col.notnull != 0) { | |
rowdef = rowdef + ' NOT NULL'; | |
} | |
rowdef = rowdef + ' DEFAULT ' + col.dflt_value; | |
if (col.pk != 0) { | |
rowdef = rowdef + ' PRIMARY KEY'; | |
} | |
if (i !== cols.length) { | |
rowdef = rowdef + ','; | |
} | |
tabledef = tabledef + rowdef; | |
}); | |
tabledef = tabledef + ')'; | |
connection.query(tabledef, function(err, rows, fields) { | |
if (err) console.log(err); | |
//console.log(rows); | |
}); | |
}); | |
}); | |
callback(null, true); | |
}); | |
}; | |
var y = 0; | |
var travasoDati = function(callback) { | |
getSqliteTables(db, function(tables) { | |
i = 0; | |
var fine = false; | |
tables.forEach(function(table) { | |
query = 'select * from ' + table.name; | |
db.run(query, function(err, rows) { | |
console.log(err); | |
console.log(rows); | |
}); | |
/* | |
i++; | |
query = 'select * from ' + table.name + ''; | |
db.each(query, function(err, row) { | |
rows.forEach(function(row) { | |
console.log(row); | |
fine = false; | |
query = connection.query('INSERT INTO ' + table.name + ' SET ?', row, function(err, result) { | |
if (err) { | |
console.log(err); | |
} else { | |
y++; | |
console.log(y + ' ' + table.name); | |
} | |
fine = true; | |
}); | |
}); | |
while(fine === false) { | |
deasync.runLoopOnce(); | |
} | |
*/ | |
/**** | |
(function(i) { | |
var y = 0; | |
async.waterfall( [ | |
function(callback) { | |
query = 'select * from ' + table.name + ' limit 1000'; | |
db.all(query, function(err, rows) { | |
if (err) console.log(err); | |
callback(null, rows); | |
}); | |
}, | |
function(callback) { | |
query = 'select * from ' + table.name + ''; | |
db.all(query, function(err, rows) { | |
rows.forEach(function(row) { | |
console.log(row); | |
query = connection.query('INSERT INTO ' + table.name + ' SET ?', row, function(err, result) { | |
if (err) { | |
console.log(err); | |
} else { | |
y++; | |
console.log(y + ' ' + table.name); | |
} | |
}); | |
}); | |
}); | |
callback(null, " OK " + table.name); | |
}, | |
function(data, callback) { | |
// console.log(table.name); | |
callback(null, data); | |
} | |
], function(err, result) { | |
if (err) console.log(err); | |
console.log('result = ' + result); | |
} | |
); | |
})(i); | |
*****/ | |
}); | |
}); | |
callback(null, "FINITO"); | |
}; | |
travasoDati(function(err, res) { | |
console.log(res); | |
}); | |
//async.series([createTables(callback)); | |
/* | |
setInterval(function() { | |
query = "Select * from details limit 1, 100"; | |
db.each(query, function(err, row) { | |
if (err) console.log( "errore: " + err ); | |
console.log(row.id); | |
for ( var col in row) { | |
console.log(col + ' ' + row[col]); | |
} | |
}); | |
}, 3000); | |
*/ | |
/* | |
connection.query('SELECT * from audiobooks limit 2', function(err, rows, fields) { | |
if (err) throw err; | |
//console.log(rows[0]["title"]); | |
rows.forEach(function(row) { | |
// console.log(row["title"]); | |
}); | |
// console.log(JSON.parse( JSON.stringify(rows) )); | |
// console.log(fields); | |
}); | |
*/ | |
/* | |
var post = {name: "Ciaooooo"}; | |
var query = connection.query('INSERT INTO access SET ?', post, function(err, result) { | |
console.log(result); | |
console.log(result.affectedRows); | |
console.log(result.insertId); | |
}); | |
console.log(query.sql); | |
*/ | |
// process.stdin.resume();//so the program will not close instantly | |
function exitHandler(options, err) { | |
if (options.cleanup) { | |
console.log('cleaning: db.close() connection.end()'); | |
db.close(); | |
connection.end(); | |
} | |
if (err) console.log(err.stack); | |
if (options.exit) process.exit(); | |
} | |
//do something when app is closing | |
process.on('exit', exitHandler.bind(null,{cleanup:true})); | |
//catches ctrl+c event | |
process.on('SIGINT', exitHandler.bind(null, {exit:true})); | |
//catches uncaught exceptions | |
process.on('uncaughtException', exitHandler.bind(null, {exit:true})); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment