sqlite-async
sqlite-async copied to clipboard
Examples
Would it be possible for you to provide some usage examples with slightly more details than the one in README? Thanks.
- Database.open() The Database.open() method is used to open an existing database or create a new one if it does not exist.
Example:
Database.open('mydb.db').then((db) => {
// database is now open
}).catch((err) => {
// handle error
});
- Database.on() The Database.on() method is used to register a callback function to be executed when a certain event occurs.
Example:
Database.open('mydb.db').then((db) => {
db.on('open', () => {
// database has been opened
});
}).catch((err) => {
// handle error
});
- Database.close() The Database.close() method is used to close an open database.
Example:
Database.open('mydb.db').then((db) => {
db.close().then(() => {
// database is now closed
}).catch((err) => {
// handle error
});
}).catch((err) => {
// handle error
});
- Database.run() The Database.run() method is used to execute an SQL statement and return the results.
Example:
db.run("SELECT * FROM users")
.then((result) => {
// SQL statement has been executed and results are in 'result'
})
.catch((err) => {
// handle error
});
- Database.get() The Database.get() method is used to execute an SQL statement and return a single row of results.
Example:
db.get("SELECT * FROM users WHERE id = ?", [1])
.then((row) => {
// SQL statement has been executed and row is in 'row'
})
.catch((err) => {
// handle error
});
- Database.all() The Database.all() method is used to execute an SQL statement and return an array of rows.
Example:
db.all("SELECT * FROM users")
.then((rows) => {
// SQL statement has been executed and rows are in 'rows'
})
.catch((err) => {
// handle error
});
- Database.each() The Database.each() method is used to execute an SQL statement and call a callback for each row.
Example:
db.each("SELECT * FROM users", (err, row) => {
// row contains the result of the query
})
.then((nrows) => {
// number of rows returned
})
.catch((err) => {
// handle error
});