jeudi 10 septembre 2015

How to use sqlite3 with node.js - what is smt?

I want to be able to use sqlite 3 with node.js

I am using the code from here and it works perfectly, However I do not really understand the code. For example:

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

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

  var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
  for (var i = 0; i < 10; i++) {
      stmt.run("Ipsum " + i);
  }
  stmt.finalize();

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

db.close();

  • First off I dont understand what stmt is or what it does neither with smt.finalize
  • What does the (?) do after VALUES
  • Why use all of this smt stuff, why not just do this: (It works I tested it.) db.run("CREATE TABLE user (id INTEGER PRIMARY KEY, name TEXT)"); db.run("INSERT INTO user VALUES (1,\"manu\")");

I hope all my questions can be answered with a link to a good documentation!

Thank you!

Aucun commentaire:

Enregistrer un commentaire