CoffeeScript SQLite - CoffeeScript

What is CoffeeScript SQLite?

SQLite is a light-weight, schema-based relational database engine. it is a popular desire as embedded database software for local garage in net browsers.

unlike many other database management structures, SQLite isn't always a client–server database engine. For extra records read our SQLite academic

on this chapter you'll learn how to communicate with SQLite database the use of CoffeeScript.

installation

The SQLite3 database can be integrated with CoffeeScript the usage of node-sqlite3 module. This module works with Node.js v0.10.x, v0.12.x, v4.x, and v5.x. This module caters numerous features to communicate with SQLite3 using CoffeeScript, further to this it also provides an sincere query and parameter binding interface, and an query serialization API.

You can install the node-sqlite3 module using npm as shown below.


To use sqlite3 module, you must first create a connection object that represents the database and this object will help you in executing all the SQL statements.

Connecting to Database

in order to connect with SQLite database, first of all, create its package by invoking the require() function of the node-sqlite3 module and pass the string sqlite3 as a parameter to it. Then hook up with a database by way of passing the call to the database to sqlite3.Database() construct.

Following CoffeeScript code suggests how to connect to a current database. If the database does no longer exist, then it will be created with the given name test.db, opened and finally, the database object will be returned.

We also can deliver :memory: to create an anonymous in-reminiscence database and, an empty string to create anonymous disk-based totally database, in place of test.db. store the above code in a report with call create_db.coffee and execute it as shown below. If the database is correctly created, then it will produce the following message −

Creating a Table

You can create a table in SQLite database through CoffeeScript using the run() function. Pass the query to create a table to this function in String format.

The following CoffeeScript program will be used to create a table in previously test.db database −


The serialize() function units the database in serialized mode. on this mode whenever a callback encounters, it will be known as immediately. The queries in that callback are executed serially. quickly the function returns The database could be set to ordinary mode once more. After finishing the transaction we need to close the connection using near() function.

save the above code in a report with call create_table.coffee and execute it as proven beneath. this will create a desk named student in the database test.db showing the subsequent messages.


Inserting / creating data

you may insert data into SQLite database through CoffeeScript code by means of executing the insert statement. To achieve this we will use the prepare() function which prepares square statements.

It also accepts question with bind variables (?), values to those variables can be connected the usage of the run() feature. you could insert a couple of information the use of organized assertion, and after placing all the statistics, you need to finalize the prepared declaration the use of finalize() function.

the subsequent CoffeeScript program suggests how to insert facts in the table named pupil created in the previous instance.

Save the above code in a file with name insert_data.coffee and execute it as shown below. This will populate the table named STUDENT displaying the following messages.

Reading / Retrieving Data

You can get the data from an SQLite table using the each() function. This function accepts an optional callback function which will be called on each row.

The following CoffeeScript program shows how we can fetch and display records from the table named STUDENT created in the previous example

Save the above code in a file with name retrive_data.coffee and execute it as shown below. This retrieves all the records in the table named STUDENT and displays on the console as follows.

The following CoffeeScript code shows how we can use UPDATE statement to update any record and then fetch and display updated records in the table named STUDENT

Save the above code in a file with name update_data.coffee and execute it as shown below. This updates the city of the student named Ram and displays all the records in the table after update operation as follows.

Deleting Data

The following CoffeeScript code shows how we can use DELETE statement to delete any record and then fetch and display remaining records from the table named STUDENT.

Save the above code in a file with name delete_data.coffee and execute it as shown below. It deletes the record of the student named Ram and displays all the remaining in the table after delete operation as follows.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

CoffeeScript Topics