Nodejs and SQLite how create multiple tables? - javascript

I have question about creating multiple tables in one DB.
I try to run for 2 tables, but seems after the first db.run(CREATE TABLE , second db.run(CREATE TABLE does not create a second table in the DB, here is the code i used:
//database.js
var sqlite3 = require('sqlite3').verbose();
var md5 = require('md5');
const users = require("./users");
const usersStatistic = require("./users_statistic");
const DBSOURCE = "db.sqlite";
let db = new sqlite3.Database(DBSOURCE, (err) => {
if (err) {
// Cannot open database
console.error(err.message)
throw err
}else{
console.log('Connected to the SQlite database.');
db.run(`CREATE TABLE user (
id integer,
first_name text,
last_name text,
email text UNIQUE,
gender text,
ip_address text,
CONSTRAINT email_unique UNIQUE (email)
)`,(err) => {
if (err) {
// Table already created
}else{
// Table just created, creating some rows
let props = '';
for (let i = 0; i < Object.keys(users[0]).length; i++) {
if(i === Object.keys(users[0]).length - 1) {
props += `${Object.keys(users[0])[i]}`
}else {
props += `${Object.keys(users[0])[i]}, `
}
}
const insert = `INSERT INTO user (${props}) VALUES (?,?,?,?,?,?)`;
for (let i = 0; i < users.length; i++) {
const user = users[i];
db.run(insert,
Object.values(user)
// user['id'],
// user['first_name'],
// user['last_name'],
// user['email'],
// user['gender'],
// user['ip_address']
)
}
// db.run(insert, [users[0]['first_name'], users[0]['last_name'], "admin#example.com"])
// db.run(insert, [users[1]['first_name'], users[2]['last_name'],"user#example.com"])
}
});
//Start second table
db.run(`CREATE TABLE user_statistic (
user_id integer,
date text,
page_views integer,
clicks integer,
)`,(err) => {
if (err) {
// Table already created
}else{
// Table just created, creating some rows
let props = '';
for (let i = 0; i < Object.keys(usersStatistic[0]).length; i++) {
if(i === Object.keys(usersStatistic[0]).length - 1) {
props += `${Object.keys(usersStatistic[0])[i]}`
}else {
props += `${Object.keys(usersStatistic[0])[i]}, `
}
}
const insert = `INSERT INTO user (${props}) VALUES (?,?,?,?)`;
console.log(insert)
for (let i = 0; i < usersStatistic.length; i++) {
const userStatistic = usersStatistic[i];
db.run(insert,
Object.values(userStatistic)
)
}
}
});
}
});
module.exports = db;

Hey there it has been a while but no one seems to reply.
I think you could try to use
db.serialize(()=>{
db.run([your 1st query])
db.run([your another query])
db.run([and another query])
})
db.close()

Related

Firebase .orderByChild().on runs more and more times every time it is called

I'm creating a guest list program that stores the guest list in Firebase RTDB and when I check people in and out my function runs several times more than it is supposed to. I've sent alerts to the console so I know how many times it has run. I have separate functions for both check in and check out operations so it may be that I am calling my db too many times?
//-------------------- Check In and Check In Helper Functions -------------------------
//Helper Function to Grab current List index
function printArray() {
var ref = database.ref('guestList')
ref.on('value', readData, errData);
}
function readData(data){
guestList=[];
var scores = data.val();
var keys = Object.keys(scores)
for (var i=0; i < keys.length; i++){
var k = keys[i]
var name = scores[k].name;
var inside = scores[k].Inside;
var timeIn = scores[k].TimeIn;
var timeOut = scores[k].TimeOut;
guestList[i] = {
name: name,
Inside: inside,
TimeIn: timeIn,
TimeOut: timeOut,
}
}
checkIn(guestList);
}
function errData(err){
console.log('Error!');
console.log(err);
}
//Helper Function to set text box to selected name
function checkInn(name){
console.log(name)
document.getElementById('checkIn').value = name;
}
//Check in
function checkIn(list) {
//Grabs current guest to be added or deleted from form text box
var name = document.getElementById('checkIn').value;
//Checks to see if user is in list of guests and isn't in the list of guest in the party
var guestsRef = firebase.database().ref("guestList/");
guestsRef.orderByChild("name").on("child_added", function(data) {
if (name == data.val().name) {
objIndex = list.findIndex((obj => obj.name == name));
guestsRef = firebase.database().ref("guestList/" + objIndex)
guestsRef.update({
Inside: "Yes",
TimeIn: getTime(),
})
guestsRef.off();
document.getElementById('checkIn').value = "";
alerts(name, true)
}
})
}
//------------------------- Check Out ------------------------------------------------------------
//Helper Function to Grab current List index
function printArrayy() {
var ref = database.ref('guestList')
ref.on('value', readOutData, errData);
}
function readOutData(data){
guestList=[];
var scores = data.val();
var keys = Object.keys(scores)
for (var i=0; i < keys.length; i++){
var k = keys[i]
var name = scores[k].name;
var inside = scores[k].Inside;
var timeIn = scores[k].TimeIn;
var timeOut = scores[k].TimeOut;
guestList[i] = {
name: name,
Inside: inside,
TimeIn: timeIn,
TimeOut: timeOut,
}
}
checkOut(guestList);
}
//Helper Function to set text box to selected name
function checkOutt(name){
console.log(name);
document.getElementById('checkOut').value = name;
}
//Check Out
function checkOut(list) {
//Grabs current guest to be added or deleted from form text box
var name = document.getElementById('checkOut').value;
//Checks to see if user is in list of guests and isn't in the list of guest in the party
var guestsRef = firebase.database().ref("guestList/");
guestsRef.orderByChild("name").on("child_added", function(data) {
if (name == data.val().name) {
objIndex = list.findIndex((obj => obj.name == name));
guestsRef = firebase.database().ref("guestList/" + objIndex)
guestsRef.update({
Inside: "No",
TimeOut: getTime(),
})
document.getElementById('checkOut').value = "";
guestsRef.off();
alerts(name, false)
}
})
}
//Placeholder to alert user when a succesful check in or check out function runs
function alerts(name, Boolean){
if(Boolean){
console.log(name + " has been checked in!")
}
else{
console.log(name + " has been checked out!")
}
}
Here is the screenshot of my output. Thanks in advance!
Edit: Forgot to mention and apologize for my excessive use of helper functions! My HTML form calls printArrayy() and printArray first for each function!
have you tried once instep on, i mean:
ref.once('value', readOutData, errData); }

Whitelisting a channel for a command

I am in the progress of making a discord bot. This is one of the commands that auto-does itself.
What it is supposed to do it's if someone type .team buy 1, save the data that comes out of another bot.
I would like to whitelist this command to 2 specific channels, identified by their channel id, and just ignore the message if it is not in the 2 channels.
How can I edit the code to do it?
const fs = require("fs");
module.exports.run = (client, message) => {
if ([509042284793430032, 501784044649054231].includes(message.channel.id)) return;
try {
//check if it's a different message //like when a user enters "team buy 234"
if (message.embeds[0].description.indexOf("❓") === 0) return;
//retrieve the team data
var teamData = JSON.parse(fs.readFileSync(client.config.dataFolder + "/teamUpgrades.json", "utf8"));
//get the current purchases data from the message
var arr = message.embeds[0].description.split("\n");
//loop and save the data in "items" object
for (var i = 0; i < arr.length; i++) {
if (arr[i] == "") continue;
if (arr[i].indexOf("Unlocks") > -1) continue; //skip locked items
var opt = arr[i].split("|"); //item's info
var name = opt[0].trim();
if (name.indexOf("**") > -1)
name = name.substring(name.indexOf("**") + 2, name.length - 2).trim(); //bold
else
name = name.split(" ")[1]; //not bold
var price = opt[1].trim();
price = price.substring(3, price.length - 1);
price = parseInt(price.split(",").join(""));
var count = opt[2].trim();
count = parseInt(count.substring(1, count.length - 2).split(",").join(""));
var eps = opt[3].trim();
eps = parseFloat(eps.split(" ")[0].substring(1));
//if the item doesn't exist, create it
if (!teamData.items[name]) teamData.items[name] = {};
teamData.items[name].price = price;
teamData.items[name].eps = eps;
teamData.items[name].count = count;
}
//the best item to buy, let's give it a very high number first
var minItem = {
name: "",
min: Number.MAX_SAFE_INTEGER
};
for (var name in teamData.items) {
//The average price/eps
var average = Number(teamData.items[name].price) / Number(teamData.items[name].eps);
//if the current item is less than the minimum item, replace it.
if (average < minItem.min) {
minItem.name = name;
minItem.min = average;
}
}
//write the current data into the json file
fs.writeFileSync(client.config.dataFolder + "/teamUpgrades.json", JSON.stringify(teamData));
message.channel.send(minItem.name);
} catch (err) {
console.log(err);
}
}
You can check if message.channel.id is equal to one of your IDs and if not, ignore it.
module.exports.run = (client, message) => {
if (['ID 1 here', 'ID 2 here'].includes(message.channel.id)) return;
};

Synchronous execution of mongoose queries

im pretty new to node.js/mongoose and i run into some difficulties getting my code to work as intended. Basically i have an Array holding some id's in a sorted order and i want to check against my Reservations if they hold those id's on a certain condition (same reservation date, time slot, etc.)
I cant seem to get my logic to work that basically should take following approach: Run through the table array from index 0 to index length-1, for each index there should be a findOne({]) query, if no reservation exists with that table id, a reservation shall be placed and exit the loop, if a reservation exists go to the next index and repeat until the array is iterated through from 0...n (the specific order the array is sorted in is important here, it should always prefer the least matching). If no table id is "free" to make a new reservation it should send a response message that every table is reserved already. Anyone that could help me out on my problem? Would appreciate any hints!
app.post('/api/reservations', (req, res) => {
const resDate = new Date(req.body.reservationDate).toISOString();
const queryDate = new Date(req.body.reservationDate);
const tableDict = req.body.tableDict;
const slot = req.body.timeSlot;
const seats = req.body.seats;
const restaurant = req.body.restaurant;
const customer = req.body.customer;
const comment = req.body.comment;
function gteQueryDate(date) {
return date.toISOString();
}
function lteQueryDate(date) {
date.setDate(date.getDate() + 1);
return date.toISOString();
}
const gteDate = gteQueryDate(queryDate);
const lteDate = lteQueryDate(queryDate);
const sortedTables = [];
for (i = 0; i < tableDict[slot].length - 1; i++) {
if (tableDict[slot][i] !== null && tableDict[slot][i].seats >= seats) {
sortedTables.push([tableDict[slot][i]._id, tableDict[slot][i].seats]);
}
}
sortedTables.sort(function(a, b) {
return a[1] - b[1];
});
var createReservation = function(tableId) {
console.log("called function with id : " + tableId);
Reservation.findOne({
'restaurant': restaurant,
'timeSlot.slot': slot,
'timeSlot.table': tableId,
'reservationDate': {
'$gte': gteDate,
'$lte': lteDate
}
})
.then(reservation => {
if (!reservation) {
console.log("can create reservation with id: " + tableId);
var timeSlot = [{
'slot': slot,
'table': tableId
}];
const newReservation = new Reservation({
seats: seats,
comment: comment,
timeSlot: timeSlot,
customer: customer,
restaurant: restaurant,
reservationDate: resDate
});
newReservation.save()
.then(result => {
res.send(result);
})
.catch(err => {
console.log(err);
});
} else {
console.log("reservation with id: " + tableId + " exists already.");
}
})
.catch(err => {
console.log(err);
});
}
for (j = 0; j < sortedTables.length; j++) {
var tableId = sortedTables[j][0];
createReservation(tableId);
}
});

Node / MySQL - code: 'ER_PARSE_ERROR', when trying to insert ~800 records

I am working on small idea to collect errors from pages and to store them in DB and then use graph API to display information visually.
There is 8 sites and on each of them there is 100 entries - so 800 transactions per time.
I loop through each site and then sub-loop through table of errors and collect them.
I got it working if I make insert query on each of those sub-loops for all 800 entries but I am getting some sort of memory leak from so many transactions and after few minutes - Node breaks due to memory exceeding.
So I tried queuing all 800 entries into Array of Arrays and then performing multi-insert at the end of every iteration but I am getting ER_PARSE_ERROR.
var tabletojson = require('tabletojson');
var mysql = require("mysql");
var striptag = require("striptags");
var fs = require("fs");
var path = require('path');
var startCollector;
var iterations = 0;
var insertions = 0;
var duplicated = 0;
var datas = [];
var clients = ["ClientA", "ClientB", "ClientC", "ClientD", "ClientE", "ClientF", "ClientG", "ClientH"];
var appDir = path.dirname(require.main.filename);
var errorList = ["err1", "err2", "err3", "err4", "err5", "err6"];
var con = mysql.createPool({
host: "localhost",
user: "User",
password: "Password",
database: "errors"
});
function CollectErrors() {
startCollector = new Date();
for(var a = 0; a < clients.length; a++) {
(function(a) {
tabletojson.convertUrl("http://example.com" + clients[a] + "/page.php?limit=100", { stripHtmlFromCells: false }, function(response) {
var rs = response[0];
for(var l = rs.length-1; l > -1; l--) {
var newDate = formatDate(striptag(rs[l]["Date"]), striptag(rs[l]["Time"]));
var user = getUser(striptag(rs[l]["User"]));
var msg = striptag(rs[l]["Error"]);
var splitError = rs[l]["Error"].split("<a href=\"");
var link = getUrl(splitError[1]);
var id = getId(link);
var type = getType(striptag(splitError[0]));
var temp = [newDate, link, type, user, clients[a], id, msg];
datas.push(temp);
}
});
})(a);
}
con.getConnection(function(err, connection) {
connection.query("INSERT IGNORE INTO entries (time, url, type, author, client, uid, message) VALUES ?", [datas], function(err, rows) {
console.log(err);
});
connection.release();
datas = [];
});
setTimeout(CollectErrors, 10000);
}
function formatDate(date, time) {
var newdate = date.split("/").reverse().join("-");
var newtime = time+":00";
return newdate + " " + newtime;
}
function getUrl(uri) {
return "http://example.com/"+uri.split("\">Details")[0];
}
function getId(url) {
return decodeURIComponent((new RegExp('[?|&]' + "id" + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}
function getType(error) {
for(var a = 0; a < errorList.length; a++) {
if(error.indexOf(errorList[a]) !== -1) {
return errorList[a];
}
}
return "Other";
}
function getUser(user) {
if(user == "" || user == " " || user == null) {
return "System";
}
return user;
}
CollectErrors();
I've tried mysql.createConnection too but that also gave me same issue.
I've been stuck for past 12 hours and I can't see what's wrong, I've even tried populating Datas table with just strings but got same error.
I've changed your code to use ES6 and correct modules features.
Useful links: correct pooling with mysql, correct insert query, async/await, IIFE, enhanced object
const tabletojson = require('tabletojson'),
mysql = require("mysql"),
striptag = require("striptags"),
fs = require("fs"),
path = require('path');
const startCollector,
iterations = 0,
insertions = 0,
duplicated = 0;
let datas = [];
const clients = ["ClientA", "ClientB", "ClientC", "ClientD", "ClientE", "ClientF", "ClientG", "ClientH"];
const appDir = path.dirname(require.main.filename);
const errorList = ["err1", "err2", "err3", "err4", "err5", "err6"];
const con = mysql.createPool({
host: "localhost",
user: "User",
password: "Password",
database: "errors"
});
// We'll use async/await from ES6
const collectErrors = async() => {
// Up to here I've only changed syntax to ES6
let startCollector = new Date();
// We'll try to iterate through each client. And we use here for..of syntax to allow us using await
for (let client of clients) {
// Please, check that client value return correct data. If not, change for..of to your for..each and client variable to clients[a]
const tbj = await tabletojson.convertUrl("http://example.com" + client + "/page.php?limit=100", {
stripHtmlFromCells: false
});
const result = tgj[0];
for (rs of result) {
// I can't check this part, but I hope your example was with correct values.
let newDate = formatDate(striptag(rs[l]["Date"]), striptag(rs[l]["Time"]));
let user = getUser(striptag(rs[l]["User"]));
let link = getUrl(splitError[1]);
let msg = striptag(rs[l]["Error"]);
let id = getId(link);
let splitError = rs[l]["Error"].split("<a href=\"");
let getType = getType(striptag(splitError[0]));
// ES6 enhanced object syntax
datas.push({
newDate,
user,
msg,
id,
splitError,
link,
getType,
temp: [newDate, link, type, user, client, id, msg]
});
}
}
// OK, here we have fulfilled datas array. And we want to save it.
con.getConnection((err, connection) => {
// Please, notice, here I've changed your insert query to prepared statement.
connection.query("INSERT IGNORE INTO entries SET ?", datas, (err, rows) => {
console.log(err);
connection.release();
datas = [];
});
});
// I don't see why do you need timeout here, so I've left it commented.
// setTimeout(CollectErrors, 10000);
};
// Here your other methods go....
// And to call your async function we'll use IIFE
(async() => {
await collectErrors();
})();
Probably there may be errors with mysql insert, but that's not for sure. If occurred, please write in comments and I'll help you with that.

Retrieving Data from existing database using webSQL?

i'm trying to retrieve the data from the database for web application using webSQL ,but i'm unable to get the data from database. I'm very new to this. I tried like this
var DB_NAME = "database";
var DB_VERSION = "";
var DB_TITLE = "";
var DB_BYTES = 50 * 1024 * 1024;
var db = openDatabase(DB_NAME, DB_VERSION, DB_TITLE, DB_BYTES);
//Retrieve Rows from Table
db.transaction(
function(tx) {
tx.executeSql("SELECT * FROM Data;",
[],
function (tx, results) {
var len = results.rows.length, i;
for (i = 0; i < len; i++) {
alert(results.rows.item(i).text);
}
});
});
Thanks in Advance.
This is how i have done.. and this is working for me.
// global variables
var db;
var shortName = 'Books';
var version = '1.0';
var displayName = 'BooksDB';
var maxSize = 200000;//65535;
function ListDBValues() {
if (!window.openDatabase) {
alert('Databases are not supported in this browser.');
return;
}
// this line tries to open the database base locally on the device if it does not exist, it will create it and return a database object stored in variable db
db = openDatabase(shortName, version, displayName,maxSize);
// this line clears out any content in the #lbUsers element on the page so that the next few lines will show updated content and not just keep repeating lines
$('#lbUsers').html('');
// this next section will select all the content from the User table and then go through it row by row appending the UserId FirstName LastName to the #lbUsers element on the page
db.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM books;', [], function(transaction, result) { if (result != null && result.rows != null) { for (var i = 0; i < result.rows.length; i++) { var row = result.rows.item(i); $('#lbUsers').append('<br>' + row.book_title + '. ' + row.book_isbn+ ' ' + row.book_price); } } },errorHandler); },errorHandler,nullHandler);
return;
alert('in list end');
}
// this is called when a successful transaction happens
function successCallBack() {
alert("DEBUGGING: success");
}
function nullHandler(){
alert('null handler');
};

Categories