jeudi 10 septembre 2015

How to insert SQL code from my non node.js JS file?

I have node.js server. For the directory/setup look here.

I am using sqlite3.

I want to be able to insert sqlite commands from my index.js, NOT my server.js.

For example I want a user to be able to enter their name in a form, and then that name should be stored in a database.

Whats the best way to do this?

I have a node js file.

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');

db.serialize(function() {
  db.run("CREATE TABLE lorem (info TEXT, name TEXT)");

  var stmt = db.prepare("INSERT INTO lorem VALUES (?, ?)");
      for(var i = 0; i<3; i++)stmt.run("Ipsum", " Bob");
  stmt.finalize();

  db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
      console.log(row.id + ": " + row.info );
  });
  db.each("SELECT rowid AS id, name FROM lorem", function(err, row) {
      console.log(row.id + ": " + row.name );
  });
});

db.close();

I want to be able to Insert data like that, however from my indiex.js file linked to my html file.

Aucun commentaire:

Enregistrer un commentaire