sqlite-async icon indicating copy to clipboard operation
sqlite-async copied to clipboard

Examples

Open foremind opened this issue 3 years ago • 1 comments

Would it be possible for you to provide some usage examples with slightly more details than the one in README? Thanks.

foremind avatar Oct 30 '22 17:10 foremind

  1. 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
});
  1. 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
});
  1. 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
});
  1. 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
  });
  1. 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
  });
  1. 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
  });
  1. 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
  });

jadsongmatos avatar Jan 05 '23 05:01 jadsongmatos