examples nodejs code

Ciao Sorry... questo laptop esegue KVM e macchine virtuali linux in ram con solo 300Mb molto veloce con HelloMicroCAD per usare GUI database dbmemory.py con python devi istallare python-tk.

HP 255 G7 freedos... info config linux debian 10.2 by skole www.pi-ppo.org

creare usb boot tramite file.iso
# lsblk
# umount /dev/sdb1
# sudo dd if=inputfile.iso of=/dev/sdb bs=4M && sync

esempio sh...
# dd if=debian10.2.iso of=/dev/sdb bs=4M && sync

usare HP 255 G7 con internet tramite il cellulare ...
# ifconfig -a

# ifconfig usb0 or up && dhclient usb0 or /sbin/dhclient enp0s...

usare wifi... HP 255 G7 con debian 10.2
# iwconfig
# lspci -nnk | grep -i net -a3

Il wifi si risolve, se attivo disattiva momentaneamente il Secure boot nel bios/firmware,
il driver per RTL8821CE HP255 G7 :

# apt install bc module-assistant build-essential dkms
# m-a prepare

# git clone https://github.com/tomaspinho/rtl8821ce
# cd rtl8821ce
# echo $PATH
# export PATH=$PATH:/usr/sbin/:

# ./dkms-install.sh

controllo dopo riavvio...
# iwconfig
wlo1 = wireless interface da inserire in Wicd Network Manager

done...

//Questo codice nodejs permette di auto+scrivere usando metodologia PAL scripts javascript per
//creare tabelle database mysql che saranno usate da jade per scrivere pagine HTML su www.zucca.cloud.
//Usando la variabile nome tabella e le variabili nomi campi e tipo che sono letti dal file nometabella.pal
//che a sua volta recupera la lista dei campi dalla prima riga del file nometabella.csv.
//Micro PAL auto create mysqlscripts by skole www.pi-ppo.org
//example CreaScript... mysqlDBcreateTable"Name".js Fileds...file.pal by skole www.pi-ppo.org
//example mysql Table"Name" Fields.pal
//info se si vuole usare un solo script per molte tabelle è possibile creare un database PAL-all e una tabella pal-tables
//La riga successiva contiene tutte le variabili necessarie le costanti non variano mai in questo programma evitando errori.
//id;Codice int;Descrizione varchar(255);Prezzo decimal(8,2);Sconto decimal(5,2);IVA decimal(5,2);barcod int;Nome varchar(255)+

var fs = require('fs');
var argsCSV = process.argv.slice(2);

try {
stats = fs.statSync(argsCSV[0]);
console.log('File: ' + argsCSV[0] + ' exists.');
}
catch (e) {
console.log('File: ' + argsCSV[0] + ' does not exist.');
process.exit();
}

console.log('file CSV: ' + argsCSV[0]);
var filename = argsCSV[0];
var pos = filename.indexOf("."); // separa il punto e l'estensione dal nomefile
var tablename = filename.slice(0, pos); // assegna il nome del file alla tabella
var filelog = tablename + '.log';
console.log('tablename: ' + tablename);
var Fields = [];
var Titles = 150;

fs.readFile(argsCSV[0], 'utf8', function (err, data) {
var dataArray = data.split(/\r?\n|;/);
for(var i = 0; i < Titles;i++){
console.log('fields name: ' + dataArray[i]);
Fields[i] = dataArray[i];
if(dataArray[i] == "+") { Titles = i, CreaScriptMySQL(); }
}
});

function CreaScriptMySQL(){
var date = new Date();
var lines = '';
lines = lines + "//example CreaScriptMySQL.js creato " + date + "\n";
lines = lines + "//mysqlDBcreateTable" + tablename + ".js file.csv by skole www.pi-ppo.org\n";
lines = lines + "\n";
lines = lines + "var fs = require('fs');\n";
lines = lines + "var argsCSV = process.argv.slice(2);\n";
lines = lines + "var mysql = require('mysql');\n";
lines = lines + "\n";
lines = lines + "try {\n";
lines = lines + " stats = fs.statSync(argsCSV[0]);\n";
lines = lines + " console.log('File: ' + argsCSV[0] + ' exists.');\n";
lines = lines + "}\n";
lines = lines + "catch (e) {\n";
lines = lines + " console.log('File: ' + argsCSV[0] + ' does not exist.');\n";
lines = lines + " process.exit();\n";
lines = lines + "}\n";
lines = lines + "\n";
lines = lines + "var con = mysql.createConnection({\n";
lines = lines + " host: 'localhost',\n";
lines = lines + " user: 'skole',\n";
lines = lines + " password: 'mysqlskole',\n";
lines = lines + " database: 'dbgiorgia'\n";
lines = lines + "});\n";
lines = lines + "\n";
lines = lines + "con.connect(function(err) {\n";
lines = lines + " if (err) throw err;\n";
lines = lines + " var filename = argsCSV[0];\n";
lines = lines + " var pos = filename.indexOf('.'); // separa il punto e l'estensione dal nomefile\n";
lines = lines + " var tablename = filename.slice(0, pos); // assegna il nome del file alla tabella\n";
lines = lines + " console.log(tablename);\n";
lines = lines + " console.log('Connected!');\n";
lines = lines + " var sql = 'CREATE TABLE " + tablename + " (id int NOT NULL AUTO_INCREMENT,";
// Fields...
for(var i = 1; i < Titles;i++){ lines = lines + " "+Fields[i]+","; }
// done...
lines = lines + " PRIMARY KEY ("+Fields[0]+"))';\n";
lines = lines + " console.log(sql);\n";
lines = lines + " con.query(sql, function (err, result) {\n";
lines = lines + " if (err) throw err;\n";
lines = lines + " console.log('Table " + tablename + " created...');\n";
lines = lines + " process.exit();\n";
lines = lines + " });\n";
lines = lines + "});\n";
// Fields...
for(var i = 0; i < Titles;i++){ lines = lines + "// table name: " + tablename + " field: " + Fields[i] + "\n"; }
// done...
fs.appendFile('mysqlDBcreateTable' + tablename + '.js', lines, function (err) {
if (err) throw err;
console.log('appendFile... mysqlDBcreateTable' + tablename + '.js');
});
}

//example CreaScriptMySQL.js creato Sat Jan 25 2020 13:44:22 GMT+0000 (GMT)
//mysqlDBcreateTableNewRecords.js file.csv by skole www.pi-ppo.org

var fs = require('fs');
var argsCSV = process.argv.slice(2);
var mysql = require('mysql');

try {
stats = fs.statSync(argsCSV[0]);
console.log('File: ' + argsCSV[0] + ' exists.');
}
catch (e) {
console.log('File: ' + argsCSV[0] + ' does not exist.');
process.exit();
}

var con = mysql.createConnection({
host: 'localhost',
user: 'skole',
password: 'mysqlskole',
database: 'dbgiorgia'
});

con.connect(function(err) {
if (err) throw err;
var filename = argsCSV[0];
var pos = filename.indexOf('.'); // separa il punto e l'estensione dal nomefile
var tablename = filename.slice(0, pos); // assegna il nome del file alla tabella
console.log(tablename);
console.log('Connected!');
var sql = 'CREATE TABLE NewRecords (id int NOT NULL AUTO_INCREMENT, Codice int, Descrizione varchar(255), Prezzo decimal(8,2), Sconto decimal(5,2), IVA decimal(5,2), barcod int, Nome varchar(255), PRIMARY KEY (id))';
console.log(sql);
con.query(sql, function (err, result) {
if (err) throw err;
console.log('Table NewRecords created...');
process.exit();
});
});
// table name: NewRecords field: id
// table name: NewRecords field: Codice int
// table name: NewRecords field: Descrizione varchar(255)
// table name: NewRecords field: Prezzo decimal(8,2)
// table name: NewRecords field: Sconto decimal(5,2)
// table name: NewRecords field: IVA decimal(5,2)
// table name: NewRecords field: barcod int
// table name: NewRecords field: Nome varchar(255)

example zucca.sh by skole www.pi-ppo.org

timestart=$(date +"%T")
echo "start time : $timestart"
echo create zucca web microsite by skole www.pi-ppo.org

echo drop mysql tables web
nodejs mysqlDBDropTable.js tableweb.csv
nodejs mysqlDBDropTable.js messaggi.csv

echo create mysql tables web
nodejs mysqlDBcreateTable.js tableweb.csv
nodejs mysqlDBcreateTable.js messaggi.csv

echo import records... tablewebs.csv
nodejs Zuccatableweb.js tableweb.csv
nodejs Zuccamessaggi.js messaggi.csv

echo create zucca done
echo "start time : $timestart"
timedone=$(date +"%T")
echo "done time : $timedone"

example updatezucca.sh by skole www.pi-ppo.org

timestart=$(date +"%T")
echo "start time : $timestart"
echo create zucca web microsite by skole www.pi-ppo.org

echo import records... tableweb.csv
nodejs Zuccatableweb.js updatetableweb.csv
nodejs Zuccamessaggi.js updatemessaggi.csv

echo create zucca done
echo "start time : $timestart"
timedone=$(date +"%T")
echo "done time : $timedone"


// examples mysql nodejs DROP table by skole www.pi-ppo.org
var fs = require('fs');
var argsCSV = process.argv.slice(2);
var mysql = require('mysql');

try {
stats = fs.statSync(argsCSV[0]);
console.log('File: ' + argsCSV[0] + ' exists.');
}
catch (e) {
console.log('File: ' + argsCSV[0] + ' does not exist.');
process.exit();
}

var con = mysql.createConnection({
host: "localhost",
user: "skole",
password: "mysqlskole",
database: "dbgiorgia"
});

con.connect(function(err) {
if (err) throw err;
var filename = argsCSV[0];
var pos = filename.indexOf("."); // separa il punto e l'estensione dal nomefile
var tablename = filename.slice(0, pos); // assegna il nome del file alla tabella
console.log(tablename);
var sql = "DROP TABLE "+tablename+"";
console.log(sql);
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table " + tablename + " deleted");
process.exit();
});
});


// examples mysql nodejs CREATE table by skole www.pi-ppo.org
var fs = require('fs');
var argsCSV = process.argv.slice(2);
var mysql = require('mysql');

try {
stats = fs.statSync(argsCSV[0]);
console.log('File: ' + argsCSV[0] + ' exists.');
}
catch (e) {
console.log('File: ' + argsCSV[0] + ' does not exist.');
process.exit();
}

var con = mysql.createConnection({
host: "localhost",
user: "skole",
password: "mysqlskole",
database: "dbgiorgia"
});

con.connect(function(err) {
if (err) throw err;
var filename = argsCSV[0];
var pos = filename.indexOf("."); // separa il punto e l'estensione dal nomefile
var tablename = filename.slice(0, pos); // assegna il nome del file alla tabella
console.log(tablename);
console.log(sql);
console.log("Connected!");
var sql = "CREATE TABLE "+tablename+" (id int NOT NULL AUTO_INCREMENT, campo2 VARCHAR(255), campo3 VARCHAR(255), campo4 VARCHAR(255), campo5 VARCHAR(255), campo6 VARCHAR(255), campo7 VARCHAR(255), campo8 VARCHAR(255), campo9 VARCHAR(255), campo10 VARCHAR(255), PRIMARY KEY (id))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table " + tablename + " created");
process.exit();
});
});

//Scusa Sorry... dimenticavo il data-entry per www.zucca.cloud questo script nodejs ti permette la gestione //della tabella da usare sul web... tramite le funzioni dbselect, dbinsert, dbupdate,dblog.

//Auto scritto da CreaScriptImportCSV.js 

//example Zucca creato Wed Jan 22 2020 07:56:58 GMT+0000 (GMT)
//CreaScriptImportCSV.js file.csv by skole www.pi-ppo.org
var fs = require('fs');
var mysql = require('mysql');
var argsCSV = process.argv.slice(2);
console.log(argsCSV[0]);
var con = mysql.createConnection({
host: 'localhost',
user: 'skole',
password: 'mysqlskole',
database: 'dbgiorgia',
connectTimeout: 1000000 // default 10000 ok 1000 records...
});

con.connect(function(err) {
if (err) throw err;
//console.log('Connected!');
});

var done = 0;
var Nsqlrec = 0;
var CSVrecErr = 0;
var records = 0;
var filelog = 'tableweb.log';

try {
stats = fs.statSync(argsCSV[0]);
console.log('File: ' + argsCSV[0] + ' exists.');
}
catch (e) {
console.log('File: ' + argsCSV[0] + ' does not exist.');
process.exit();
}

fs.readFile(argsCSV[0], 'utf8', function (err, data) {
var dataArray = data.split(/\r?\n|;/);

var NrloopNfields = 10; // total loop fields csv file...
var NrloopRecords = 100; // max records for loop...

var Nfields = 0;
var title = [0,1,2,3,4,5,6,7,8,9] // start pos titles dataArray
var field = [title[0],title[1],title[2],title[3],title[4],title[5],title[6],title[7],title[8],title[9]]; // start pos fields dataArray

var sqlData = '';
var Data0 = ''; var Data1 = ''; var Data2 = ''; var Data3 = ''; var Data4 = ''; var Data5 = ''; var Data6 = ''; var Data7 = ''; var Data8 = ''; var Data9 = '';
for(var ii = 0; ii < (NrloopRecords*NrloopNfields) ;ii++){
for(var i = 0; i < dataArray.length;i++){
if(i == ii) {
if(i == 0) Nfields = 1;
if(i > field[0]) { field[0] = field[0] + NrloopNfields, Nfields = Nfields + 1, records = records + 1; }
if(i > field[1]) field[1] = field[1] + NrloopNfields, Nfields = Nfields + 1;
if(i > field[2]) field[2] = field[2] + NrloopNfields, Nfields = Nfields + 1;
if(i > field[3]) field[3] = field[3] + NrloopNfields, Nfields = Nfields + 1;
if(i > field[4]) field[4] = field[4] + NrloopNfields, Nfields = Nfields + 1;
if(i > field[5]) field[5] = field[5] + NrloopNfields, Nfields = Nfields + 1;
if(i > field[6]) field[6] = field[6] + NrloopNfields, Nfields = Nfields + 1;
if(i > field[7]) field[7] = field[7] + NrloopNfields, Nfields = Nfields + 1;
if(i > field[8]) field[8] = field[8] + NrloopNfields, Nfields = Nfields + 1;
if(i > field[9]) field[9] = field[9] + NrloopNfields, Nfields = Nfields + 1;

if(i == field[0]) Data0=dataArray[i], console.log(dataArray[title[0]]+': ' + dataArray[i]);
if(i == field[1]) Data1=dataArray[i], console.log(dataArray[title[1]]+': ' + dataArray[i]);
if(i == field[2]) Data2=dataArray[i], console.log(dataArray[title[2]]+': ' + dataArray[i]);
if(i == field[3]) Data3=dataArray[i], console.log(dataArray[title[3]]+': ' + dataArray[i]);
if(i == field[4]) Data4=dataArray[i], console.log(dataArray[title[4]]+': ' + dataArray[i]);
if(i == field[5]) Data5=dataArray[i], console.log(dataArray[title[5]]+': ' + dataArray[i]);
if(i == field[6]) Data6=dataArray[i], console.log(dataArray[title[6]]+': ' + dataArray[i]);
if(i == field[7]) Data7=dataArray[i], console.log(dataArray[title[7]]+': ' + dataArray[i]);
if(i == field[8]) Data8=dataArray[i], console.log(dataArray[title[8]]+': ' + dataArray[i]);
if(i == field[9]) Data9=dataArray[i], console.log(dataArray[title[9]]+': ' + dataArray[i]);

if(i == field[9] & i > 9) { // i 0...9 field title not in sqlData
//console.log('for id ' + Data0 + ' rows file cvs ' + records);
sqlData = Data0 + Data1 + Data2 + Data3 + Data4 + Data5 + Data6 + Data7 + Data8 + Data9; //array
var npos = sqlData.search('pincopalla')
//console.log('npos ' + npos); //npos numero posizione inizio valore da ricercare nella stringa -1 non trovato
if(npos != -1) { dblog(filelog, 'Err.: ' + sqlData + '\n');//test values sqlData
sqlData = 'err', CSVrecErr = CSVrecErr +1;
} //end test values sqlData
if(sqlData != 'err') { //test values sqlData
dbselect(Data0, Data1, Data2, Data3, Data4, Data5, Data6, Data7, Data8, Data9);
};
};
};
};
};
console.log('Records extract: '+ records + ' fields: ' + Nfields + ' chars estratti: ' + i);
});

function dbselect(Data0, Data1, Data2, Data3, Data4, Data5, Data6, Data7, Data8, Data9){
// console.log('SELECT tableweb ' + Data0);
var sql = "SELECT * FROM tableweb where id = '"+Data0+"'";
con.query(sql, function (err, result) {
if(err) throw err;
if(result != '') {
// console.log('SELECT... tableweb ' + result[0].id + ' ' + result[0].iduser);
dbupdate(result[0].id, Data1, Data2, Data3, Data4, Data5, Data6, Data7, Data8, Data9);
} else { dbinsert(Data0, Data1, Data2, Data3, Data4, Data5, Data6, Data7, Data8, Data9) };
});
return done;
};

function dbupdate(Data0, Data1, Data2, Data3, Data4, Data5, Data6, Data7, Data8, Data9){
//console.log('UPDATE tableweb ' + Data0);
var logtext = '';
var sql = "UPDATE tableweb SET campo2 = '"+Data1+"', campo3 = '"+Data2+"', campo4 = '"+Data3+"', campo5 = '"+Data4+"', campo6 = '"+Data5+"', campo7 = '"+Data6+"', campo8 = '"+Data7+"', campo9 = '"+Data8+"', campo10 = '"+Data9+"' where id = '"+Data0+"'";
con.query(sql, function (err, result) {
if(err) throw err;
if(result != '') {
Nsqlrec = Nsqlrec + 1;
logtext = sql + '\n';
//console.log(logtext);
dblog(filelog, logtext);
//console.log('UPDATE tableweb ' + Nsqlrec + ' record updeted CSVrecErr: ' + CSVrecErr);
} else { dblog('dbsqlErr.log', 'ID: ' + id + ';' + campo2 + '\n') };
});

return done;
};

function dbinsert(Data0, Data1, Data2, Data3, Data4, Data5, Data6, Data7, Data8, Data9){
//console.log('INSERT tableweb ' + Data1);
var logtext = '';
var sql = "INSERT INTO tableweb (campo2, campo3, campo4, campo5, campo6, campo7, campo8, campo9, campo10) VALUES ('"+Data1+"', '"+Data2+"', '"+Data3+"', '"+Data4+"', '"+Data5+"', '"+Data6+"', '"+Data7+"', '"+Data8+"', '"+Data9+"')";
con.query(sql, function (err, result) {
if(err) throw err;
if(result != '') {
Nsqlrec = Nsqlrec + 1;
logtext = sql + '\n';
//console.log(logtext);
dblog(filelog, logtext);
//console.log('INSERT tableweb ' + Nsqlrec + ' record insert ' + records + ' CSVrecErr: ' + CSVrecErr);
} else { dblog('dbsqlErr.log ', + Data1 + '\n') };
});

return done;
};

function dblog(filelog, dbdatalog){
var date = new Date();
var current_hour = date.getHours();
var logline = date + current_hour + '-' + dbdatalog;
fs.appendFile(filelog, logline, function (err) {
if (err) throw err;
//console.log('log! ' + filelog + ' datalog ' + logline);
if(Nsqlrec == (records - 1 - CSVrecErr)) { fs.renameSync(argsCSV[0], 'done-'+argsCSV[0]), process.exit(); }
});
return done;
};

CONTACT US

 

  Tel: .....

FOLLOW US

PROFILE

Profile ...

© Copyright 2016-2020 All Rights Reserved.