aa-sqlite v1.0.29
Async-Await-SQLITE
Purpose
SQLite is popular embedded database engine used in mobile devices and computers. Although powerfull and lightweight, its asynchronous nature can be a stumbling block for beginners.
AA-SQLite is a promise based SQLite wrapper that allows applications to interact with SQLite3 databases synchronously. When using AA-SQLite to query a database, application execution is paused until the result of the query has been resolved. AA-SQlite supports foreign key integration, and the following database operations:
- Select
- Delete
- Insert
- Update
IMPORTANT LINKS
Technologies Used
- SQLite
- Node.js with Async/Await
Setup
In order to use this package, you will need to create a SQLite3 project, by installing the following required technologies.
Required Technologies
Installation and Configuration
- Install all required technologies
- Optional Enable SQLite foreign key constraint:
- Create a new project in your editor of choice
- Install the AA-SQLite package
- npm i aa-sqlite --save
- See instructions on database creation.
Create a Sample Database
- Create a database.js file
- Require the sqlite3 package
const sqlite3 = require('sqlite3').verbose();
- Set the location of your database file.
const DBSOURCE = "./geo_db.sqlite";
- Open a database connection, create a table, and populate the table:
let db = new sqlite3.Database(DBSOURCE, (err) => { if (err) { console.error("DB Error", err); return; } else { console.log('Connected to the SQLite geo_db database.'); db.run(`CREATE TABLE geo_table( id INTEGER, latitude DECIMAL(5, 2), longitude DECIMAL(5, 2), city TEXT, state TEXT, PRIMARY KEY(id) )`, (err) => { if (err) { console.log("geo_table NOT created", err); } else { console.log("geo_table created"); var insert = 'INSERT INTO geo_table (lat, lon, city, state) VALUES (?, ?, ?, ?)'; db.run(insert, [33.75, 84.39, "Atlanta", "Georgia"]); db.run(insert, [34.05, 118.24, "Los Angeles", "California"]); console.log("geo_table populated"); } }); }//db created });
- Close the database connection
db.close((err) => { if (err) { console.error(err.message); } console.log('geo_db connection is closed.'); });
- Run the javascript file
- At a command prompt, change the directory to the location of the database.js file.
- Type: "node database.js" Enter
- Require the sqlite3 package
Query the Database with AA-SQLite
- Prerequisites:
- Set the location of your database file.
const DBSOURCE = "./database_name.sqlite";
- Require the AA-SQLite package
const aaSqlite = require("aa-sqlite");
- Set the location of your database file.
- Open the database connection:
- Syntax:
await aaSqlite.open(DBSOURCE: string, Enable_Foreign_Key: boolean)
- Syntax:
- Query the database:
- SELECT Query - Syntax1 : Used to return a single unique value, such as a key.
await aaSqlite.get(query: string, values: array);
- SELECT Query - Syntax2 : Used to return all matching records.
await aaSqlite.get_all(query: string, values: array);
- INSERT, UPDATE, or DELETE Query - Syntax : Used for queries that do not return a value.
await aaSqlite.push(query: string, values: array);
- Parameters:
- query: Any of the sqlite3 query strings mentioned above.
- values: Array containing the actual values to be inserted in an INSERT INTO VALUES clause. If the values are contained in the query, or the query does not require them, this parameter should be provided an empty array [].
- SELECT Query - Syntax1 : Used to return a single unique value, such as a key.
- Close the database:
- Syntax:
aaSqlite.close();
- Syntax:
Use
- This repo is available for public non-commercial use only.
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago