Insert/Update/Delete
Any other database operation except for querying the DB is performed via the Exec()
function. This function can also intake parameters via an optional array of untyped values.
This command accepts a query in whichever SQL dialect your database speaks, and returns an object of type SqlResult
which is defined as follows:
ts
type SqlResult = {
LastInsertedId: any; // typically a number or a string
RowsAffected: number;
}
Example (inserting data into DB)
ts
{
var cli = new SqlCli();
cli.Driver("sqlite");
cli.ConnString("/var/sqlitedbs/mydb.sqlite");
if (cli.Connect()) {
var res = cli.Exec('INSERT INTO places (place, code) VALUES (?,?)', ['universe', 42]);
Log(res.LastInsertedId); // Output: 12345
Log(res.RowsAffected); // Output: 1
cli.Close();
}
}
Example (deleting data from DB)
ts
{
var cli = new SqlCli();
cli.Driver("sqlite");
cli.ConnString("/var/sqlitedbs/mydb.sqlite");
if (cli.Connect()) {
var res = cli.Exec('DELETE FROM places WHERE code=?', [42]);
Log(res.RowsAffected); // Output: 1
cli.Close();
}
}