Is it possible to turn off implicit transactions using the PostgreSQLDatabase plugin? I can’t create a database in code without this.
is it what you’re looking for ?
When
autocommit
is on (the sane default in psql), you can “turn it off” by starting a transaction manually withBEGIN
.
BEGIN
initiates a transaction block, that is, all statements after aBEGIN
command will be executed in a single transaction until an explicitCOMMIT
orROLLBACK
is given. By default (withoutBEGIN
), PostgreSQL executes transactions in “autocommit” mode, that is, each statement is executed in its own transaction and a commit is implicitly performed at the end of the statement (if execution was successful, otherwise a rollback is done).
You can make it explicit using pure Xojo as
BeginTransaction() , CommitTransaction(), RollbackTransaction()
Try
DB.BeginTransaction
DB.ExecuteSQL("CREATE TABLE AddressBook name VARCHAR, email VARCHAR")
DB.CommitTransaction
Catch error As DatabaseException
MessageBox("Error: " + error.Message)
DB.RollbackTransaction
End Try
Thanks for the replies, however Postgres will not allow creating a database within a transaction. The error message I get is “ERROR: CREATE DATABASE cannot run inside a transaction block”.
I have a workaround. I was creating the database & setting permissions in a single executeSQL statement, so 4 SQL commands. By executing the create database command by itself I don’t get the error & I can then execute the other 3 commands as a group.