I have a feeling I have to try and retrieve things from my SQL with python and not JavaScript. I’ve googled a lot, and it seems JavaScript can’t handle database retrieval client side. I put my database, my html file, and my javascript file in the same folder, double click my html file to have my default browser run it.
I can get the message retrieved from my database, but I can’t get it to assign it to anything I can use.
My database is simply has a table named test, with the values:
id = 1 and message = “Animal crackers in my soup”
alerts only work in HTML, console.log in the console
I’m going to give up on this idea, but if anyone wants to take a crack at it:
// Get fields from the mySQL database:
const sqlite3 = require('sqlite3').verbose();
const path = require('path')
const dbPath = path.resolve(__dirname, 'GameScore.db')
const db = new sqlite3.Database(dbPath, (err) => {
if(err){
//alert(err);
console.log(err.message)
}
else{
//alert("connected");
console.log("Connected");
}
});
function getMessage() {
//alert("getMessage");
//var x = "None";
//alert(x)
db.serialize(() => {
db.each(`SELECT id as id, message as message FROM test;`, (err, row) => {
if (err) {
console.log(err.message);
//alert(err.message);
}
// No Error
// HOW DO I GET my row.message out of these call backs?
console.log(row.message);
return row.message;
});
// here my row.message is gone.
//console.log(row.message);
});
}
function MyFunction(){
//alert("myfunction")
message = getMessage();
//document.getElementById("MyMessage").innerHTML = message;
console.log(message); //'None'
};
MyFunction();
picture attached of result in console.