In sqlite we can do BEGIN TRANSACTION and then COMMIT after any update or insert operation to ensure atomic feature of databases. Can I achieve same using sqlite C APIs ?
Eg :
*BEGIN TRANSACTION;
UPDATE accounts
SET balance = balance - 1000
WHERE account_no = 100;
UPDATE accounts
SET balance = balance + 1000
WHERE account_no = 200;
INSERT INTO account_changes(account_no,flag,amount,changed_at)
VALUES(100,'-',1000,datetime('now'));
INSERT INTO account_changes(account_no,flag,amount,changed_at)
VALUES(200,'+',1000,datetime('now'));
COMMIT;*
I want to do above in C/C++ program using C/C++ APIs of sqlite
You can use below c API to execute any properly formed sql statement.
int sqlite3_exec(
sqlite3*, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
For example, to begin the transaction
sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, NULL);
To commit the transaction
sqlite3_exec(db, "END TRANSACTION;", NULL, NULL, NULL);
Where db is your sql connection.
Note::
END TRANSACTIONis just an alias forCOMMIT.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With