2

I try to insert data into a local database sqlite. Its possible. I can do it . But I have to create a new table every time. I try to do it without creating a new table...

I already insert the data into the table but I had to create a table every time.

     $("#ecris").click( function() 
        {          

              var fs = require("fs");         
              var sql = require("./js/sql.js"); 
              var db = new sql.Database();                   
              db.run("CREATE TABLE test (col1, col2);");

              db.run("INSERT INTO test (col1, col2) VALUES ('8','4564')"); 
;  
              var data = db.export();

              var buffer = new Buffer(data);
              fs.writeFileSync("bd/mybd.sqlite", buffer );
              db.close();   



        });

I just need the insert into the existing table... thanks a lot

1
  • insert does not work Commented Dec 31, 2018 at 17:02

2 Answers 2

0

Why creating & writing to same file everytime,

instead check if file already exists, then reopen your db file "bd/mybd.sqlite" and directly insert data to table, you can get syntax easily on net.

Update to read a database from the disk:

   var fs = require('fs');
   var SQL = require('sql.js');
   var filebuffer = fs.readFileSync('test.sqlite');

  // Load the db
  var db = new SQL.Database(filebuffer);

Ref: https://github.com/kripken/sql.js/blob/master/README.md

Sign up to request clarification or add additional context in comments.

2 Comments

its a good idea to get the syntax on the net thnaks a lot....realy usefull I don't ask a question when I have the solution...I dont know how to reopen the db file
I have just added code on how to reopen DB file, refer mentioned readme URL.
0

finally find thanks for your help !

$("#ecris").click( function() 
{          

     var fs =  require('fs');         
     var sql = require("./js/sql.js");      
     var filebuffer = fs.readFileSync("bd/mybd.sqlite"); 
     var db = new sql.Database(filebuffer);
      db.run("INSERT INTO test (col1, col2) VALUES ('gerald','coucou')");
     var data = db.export();
     var buffer = new Buffer(data);
     fs.writeFileSync("bd/mybd.sqlite", buffer );
     db.close();

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.