Failed Experiment

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.

1 Like

Yes, I think you can’t retrieve a database from client side with pure javascript. You need something in the middle (a framework) that comunicates with the server. Javascript alone can only alter things inside the actual brower window. This is by design for security reasons.
With a pure Nodejs program you should be able to retrieve data from a database.
But I’m no expert in this, don’t take my word for granted.

1 Like

Uhhh I think there’s a database API for the client side, but it’s a MONSTER to understand.

2 Likes