Sqlite3 Development Tutorials, Guides & Insights
Unlock 1+ expert-curated sqlite3 tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your sqlite3 skills on DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Tutorial
Connecting a Node.js Application to an SQLite Database Using sqlite3
// app.js
const sqlite3 = require('sqlite3').verbose();
// Connect to the SQLite database (creates the file if it doesn't exist)
const db = new sqlite3.Database('your_database_name.db', (err) => {
if (err) {
console.error('Error opening database:', err.message);
} else {
console.log('Connected to the SQLite database.');
}
});
// Serialize ensures that the queries are executed sequentially
db.serialize(() => {
// Create the "accounts" table
db.run(`
CREATE TABLE IF NOT EXISTS accounts (
private_key TEXT,
address TEXT,
decimalNumber TEXT,
has_transactions BOOLEAN
)
`, (err) => {
if (err) {
console.error('Error creating table:', err.message);
} else {
console.log('Table "accounts" created or already exists.');
}
});
// Insert data into the "accounts" table
const stmt = db.prepare('INSERT INTO accounts (private_key, address, decimalNumber, has_transactions) VALUES (?, ?, ?, ?)');
stmt.run('private_key_value', 'address_value', 'decimalNumber_value', 1, function(err) {
if (err) {
console.error('Error inserting data:', err.message);
} else {
console.log(`A row has been inserted with rowid ${this.lastID}`);
}
});
stmt.finalize();
// Retrieve data from the "accounts" table
db.each('SELECT * FROM accounts', (err, row) => {
if (err) {
console.error('Error retrieving data:', err.message);
} else {
console.log(`Private Key: ${row.private_key}`);
console.log(`Address: ${row.address}`);
console.log(`Decimal Number: ${row.decimalNumber}`);
console.log(`Has Transactions: ${row.has_transactions}`);
console.log('---------------------------');
}
});
});
// Close the database connection
db.close((err) => {
if (err) {
console.error('Error closing the database connection:', err.message);
} else {
console.log('Database connection closed.');
}
});When working with databases, especially those storing sensitive information like private keys, adhering to security best practices is essential.
Oct 24, 2024
Read More