Reputation: 35
Im making a discord bot and im using one package that uses sqlite3 and made a database.sqlite file. I want to start making other tables and using that database but i dont know how to connect to that database! Please reply if you can help me also i use this package discord-economy.
Upvotes: 1
Views: 3545
Reputation: 487
Not sure if I understand your question, but you can use sqlite3 package from NPM. Then, you would call your db file and check if db already exists. If not, you should create and run a new one. Otherwise you can just instantiate from your db file:
var sqlite3 = require('sqlite3').verbose();
var fs = require('fs');
var dbFile = './database.sqlite';
var dbExists = fs.existsSync(dbFile);
if (!dbExists) {
fs.openSync(dbFile, 'w');
}
var db = new sqlite3.Database(dbFile);
if (!dbExists) {
db.run('CREATE TABLE `your_table` (' +
'`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,' +
'`name` TEXT,' +
'`email` TEXT,');
}
// You can insert some data here in order to test
// var statement = db.prepare('INSERT INTO `your_table` (`name`, `email`) ' +
'VALUES (?, ?)');
// statement.run('Your name', '[email protected]');
// statement.finalize();
db.close();
Upvotes: 4