Using SQLite in Javascript with sql.js

Fun Fun Fun :)

  1. var sql = require('sql.js');
  2. // or sql = window.SQL if you are in a browser
  3.  
  4. // Create a database
  5. var db = new sql.Database();
  6. // NOTE: You can also use new sql.Database(data) where
  7. // data is an Uint8Array representing an SQLite database file
  8.  
  9. // Execute some sql
  10. sqlstr = "CREATE TABLE hello (a int, b char);";
  11. sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
  12. sqlstr += "INSERT INTO hello VALUES (1, 'world');"
  13. db.run(sqlstr); // Run the query without returning anything
  14.  
  15. var res = db.exec("SELECT * FROM hello");
  16. /*
  17. [
  18.         {columns:['a','b'], values:[[0,'hello'],[1,'world']]}
  19. ]
  20. */
  21.  
  22. // Prepare an sql statement
  23. var stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");
  24.  
  25. // Bind values to the parameters and fetch the results of the query
  26. var result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});
  27. console.log(result); // Will print {a:1, b:'world'}
  28.  
  29. // Bind other values
  30. stmt.bind([0, 'hello']);
  31. while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']
  32.  
  33. // You can also use javascript functions inside your SQL code
  34. // Create the js function you need
  35. function add(a, b) {return a+b;}
  36. // Specifies the SQL function's name, the number of it's arguments, and the js function to use
  37. db.create_function("add_js", add);
  38. // Run a query in which the function is used
  39. db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world'
  40.  
  41. // free the memory used by the statement
  42. stmt.free();
  43. // You can not use your statement anymore once it has been freed.
  44. // But not freeing your statements causes memory leaks. You don't want that.
  45.  
  46. // Export the database to an Uint8Array containing the SQLite database file
  47. var binaryArray = db.export();

sql.js @ Github

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

Particle Network Animations in Javascript

What are particle animations? Particle network animations in JavaScript typically involve creating visual representations of… Read More

3 days ago

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

1 month ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago