SQLite Made Easy: A Quick and Simple Introduction for Beginners Using Node.js
I love sqlite. It’s a very light database that uses the SQL language and lives in 1 local file. Super easy to manage. Wanna have a backup? Just copy your .db file and there you have it.
It’s light. A fast and efficient database.
That’s why in this tutorial I’ll show you how I set it up for my web development projects. Follow along. It won’t take more than 5 minutes.
As always, setup your project with node.js and install sqlite like so:
yarn add sqlite3 sqlite
You’ll need both, sqlite and sqlite3.
Now create a start function that initialises the database and creates a table like so:
const path = require('path')
const sqlite3 = require('sqlite3')
const { open } = require('sqlite')
let db = null
const start = async () => {
try {
db = await open({
filename: path.join(__dirname, 'database.db'),
driver: sqlite3.Database,
})
await db.exec('CREATE TABLE IF NOT EXISTS users (name, colour)')
} catch (e) {
return console.log(‘Error’, e)
}
}
start()
You can call the database however you want. As long as it ends in .db
.