{"id":212,"date":"2014-07-31T23:59:28","date_gmt":"2014-07-31T18:29:28","guid":{"rendered":"http:\/\/codeforgeek.com\/?p=212"},"modified":"2023-12-22T19:02:31","modified_gmt":"2023-12-22T13:32:31","slug":"node-sqlite-tutorial","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/node-sqlite-tutorial\/","title":{"rendered":"NodeJS SQLite Tutorial"},"content":{"rendered":"\n<p>SQLite is a self-contained database engine that requires no server to run (for <a title=\"PHP and MySQL Connectivity\" href=\"https:\/\/codeforgeek.com\/php-mysql-connectivity\/\" target=\"_blank\" rel=\"noopener\">MySQL<\/a>, Oracle we require a Database Server). SQLite is most popular in developing mobile apps and it is considered as most widely deployed database engine in the world.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a title=\"Download code\" href=\"https:\/\/github.com\/shaikh-shahid\/node-sqlite\" target=\"_blank\" rel=\"noopener\">Download code from Github<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">NodeJS SQLite Tutorial<\/h2>\n\n\n\n<p>In this tutorial, I am going to explain how to use SQLite with NodeJS. Let&#8217;s create a project named &#8220;<strong>node-sqlite<\/strong>&#8221; and in that create a package file as &#8220;package.json&#8221;. Paste the following code in it.<br><code lang=\"javascript\"><br>\n{<br>\n\"name\": \"node-sqlite\",<br>\n\"version\": \"1.0.1\",<br>\n\"dependencies\": {<br>\n\"sqlite3\": \"~2.2.3\"<br>\n}<br>\n}<br>\n<\/code><\/p>\n\n\n\n<p>Install the project by typing &#8220;<strong>npm install<\/strong>&#8221; in the terminal or command prompt. It will install all dependencies and make your project ready to work.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" width=\"1112\" height=\"355\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/Screenshot-from-2014-07-31-135331-e1406797314704.png\" alt=\"NPM INSTALL\" class=\"wp-image-213\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/Screenshot-from-2014-07-31-135331-e1406797314704.png 1112w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/Screenshot-from-2014-07-31-135331-e1406797314704-768x245.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/Screenshot-from-2014-07-31-135331-e1406797314704-300x96.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/Screenshot-from-2014-07-31-135331-e1406797314704-1024x327.png 1024w\" sizes=\"(max-width: 1112px) 100vw, 1112px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">SQLite Database Schema Setup<\/h2>\n\n\n\n<p>SQLite is a transactional database engine, which means you can run DDL and DML queries of SQL. DDL queries deal with the creation of database, tables, view etc. whereas DML queries are responsible for <a href=\"https:\/\/en.wikipedia.org\/wiki\/Create,_read,_update_and_delete\" target=\"_blank\" rel=\"noopener\"><strong>CRUD<\/strong><\/a> operation (insert, delete, update, etc.). Here is the code to create an SQLite database and performing SQL operations.<br><code lang=\"javascript\"><br>\nvar sqlite3 = require('sqlite3').verbose();<br>\nvar db = new sqlite3.Database('mydb.db');<br>\nvar check;<br>\ndb.serialize(function() {<\/code><\/p>\n\n\n\n<p><code lang=\"javascript\"><br>\n<\/code><\/p>\n\n\n\n<p><code lang=\"javascript\">  db.run(\"CREATE TABLE if not exists user_info (info TEXT)\");<br>\nvar stmt = db.prepare(\"INSERT INTO user_info VALUES (?)\");<br>\nfor (var i = 0; i &lt; 10; i++) {<br>\nstmt.run(\"Ipsum \" + i);<br>\n}<br>\nstmt.finalize();<\/code><\/p>\n\n\n\n<p><code lang=\"javascript\"><code lang=\"javascript\"><\/code><\/code><\/p>\n\n\n\n<p>db.each(&#8220;SELECT rowid AS id, info FROM user_info&#8221;, function(err, row) {<br>console.log(row.id + &#8220;: &#8221; + row.info);<br>});<br>});<\/p>\n\n\n\n<p><code lang=\"javascript\"><br>\n<\/code><\/p>\n\n\n\n<p><code lang=\"javascript\">db.close();<br>\n<\/code><\/p>\n\n\n\n<p>To run and analyse the code, type <strong>node filename.js<\/strong> (in my case <strong>app.js<\/strong>) and see whether it creates a &#8220;<strong>mydb.sql<\/strong>&#8221; file in your folder or not. This code will create a table in the database and add some numbers and displays them in the console.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" width=\"728\" height=\"233\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/Screenshot-from-2014-07-31-135408-e1406797472151.png\" alt=\"SQLite output\" class=\"wp-image-215\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/Screenshot-from-2014-07-31-135408-e1406797472151.png 728w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/Screenshot-from-2014-07-31-135408-e1406797472151-300x96.png 300w\" sizes=\"(max-width: 728px) 100vw, 728px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">SQLite Queries using NodeJS<\/h2>\n\n\n\n<p>To perform the <a href=\"https:\/\/sqldocs.org\/sqlite\/sqlite-select-statement\/\" target=\"_blank\" rel=\"noopener\">SELECT query<\/a> you should use the <strong>.all<\/strong> function and for other operations such as DELETE, UPDATE, INSERT you should use the <strong>.run<\/strong> function. Here have a look at simple code.<br><code lang=\"javascript\"><br>\n\/\/Load modules<br>\nvar sqlite3         =       require('sqlite3').verbose();<br>\nvar db              =       new sqlite3.Database('.\/database_name.db');<\/code><\/p>\n\n\n\n<p><code lang=\"javascript\"><code lang=\"javascript\"><\/code><\/code><\/p>\n\n\n\n<p>\/\/Perform SELECT Operation<br>db.all(&#8220;SELECT * from blah blah blah where this=&#8221;+that,function(err,rows){<br>\/\/rows contain values while errors, well you can figure out.<br>});<\/p>\n\n\n\n<p><code lang=\"javascript\"><code lang=\"javascript\"><\/code><\/code><\/p>\n\n\n\n<p>\/\/Perform INSERT operation.<br>db.run(&#8220;INSERT into table_name(col1,col2,col3) VALUES (val1,val2,val3)&#8221;);<\/p>\n\n\n\n<p><code lang=\"javascript\"><code lang=\"javascript\"><\/code><\/code><\/p>\n\n\n\n<p>\/\/Perform DELETE operation<br>db.run(&#8220;DELETE * from table_name where condition&#8221;);<\/p>\n\n\n\n<p><code lang=\"javascript\"><br>\n<\/code><\/p>\n\n\n\n<p><code lang=\"javascript\">\/\/Perform UPDATE operation<br>\ndb.run(\"UPDATE table_name where condition\");<br>\n<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Further reading:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/sqldocs.org\/sqlite\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQLite3 official documentation page.<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQLite is a self-contained database engine that requires no server to run (for MySQL, Oracle we require a Database Server). SQLite is most popular in developing mobile apps and it is considered as most widely deployed database engine in the world. Download code from Github. NodeJS SQLite Tutorial In this tutorial, I am going to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":218,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[14,18],"tags":[],"class_list":["post-212","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodejs","category-tutorial"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner21.png",680,400,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner21-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner21-300x176.png",300,176,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner21.png",680,400,false],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner21.png",680,400,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner21.png",680,400,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner21.png",680,400,false]},"uagb_author_info":{"display_name":"Shahid","author_link":"https:\/\/codeforgeek.com\/author\/shahid\/"},"uagb_comment_info":0,"uagb_excerpt":"SQLite is a self-contained database engine that requires no server to run (for MySQL, Oracle we require a Database Server). SQLite is most popular in developing mobile apps and it is considered as most widely deployed database engine in the world. Download code from Github. NodeJS SQLite Tutorial In this tutorial, I am going to&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/218"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}