# Getting error: "cannot commit-no transaction is active"?

**URL:** <https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367>\
**Category:** Databases\
**Created:** [January 4, 2015, 4:01pm UTC](https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367 "2015-01-04T16:01:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rami\_Hassan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@Rami\_Hassan](https://forum.xojo.com/u/Rami_Hassan)\
**Post date:** [January 4, 2015, 4:01pm UTC](https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367/1 "2015-01-04T16:01:59Z")

</div>

It works at first (it appears to have saved according to a ListBox) but when I close the project (when testing) and reopen it, it has not saved (listbox shows previous value). Can anyone help please?  
It doesn’t give an error when the first line is uncommented however it still has the same result- not saving in the SQLite database.

` 'gDatabase.SQLExecute("begin") 
  Dim record As RecordSet=gDatabase.SQLSelect("SELECT * From Login")
  if record<>nil then
    record.MoveFirst
    while not record.EOF
      if record.Field("Coach").IntegerValue=0 then
        if InTeam(record.Field("RunnerID").IntegerValue) then
          if record.Field("Team").IntegerValue=0 then
            record.Edit
            record.Field("Team").IntegerValue=1
            record.Update
          end
        else
          if record.Field("Team").IntegerValue=1 then
            record.Edit
            record.Field("Team").IntegerValue=0
            record.Update
          end
          if record.Field("Team").IntegerValue=2 then
            record.Edit
            record.Field("Team").IntegerValue=3
            record.Update
          end 
        end
      end
      record.MoveNext
    wend
  end
  gDatabase.Commit
  if gDatabase.Error then
    MsgBox("ERROR: "+gDatabase.ErrorMessage)
  end`

Thank you.

---

<div class="post-metadata">

**Author:** ![Syed\_Hassan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@Syed\_Hassan](https://forum.xojo.com/u/Syed_Hassan)\
**Post date:** [January 4, 2015, 4:20pm UTC](https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367/2 "2015-01-04T16:20:29Z")

</div>

May be the “Commit” has to be executed before the “MoveNext” and after the “Update”?

---

<div class="post-metadata">

**Author:** ![Oliver\_Osswald](https://forum.xojo.com/user_avatar/forum.xojo.com/oliver_osswald/32/6972_2.png) [@Oliver\_Osswald](https://forum.xojo.com/u/Oliver_Osswald)\
**Post date:** [January 4, 2015, 4:29pm UTC](https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367/3 "2015-01-04T16:29:58Z")

</div>

SQLiteDatabase is automatically set to autocommit, unless you start a transaction.  
So either you uncomment your “begin” or you comment your commit.

[http://documentation.xojo.com/index.php/Database.Commit](http://documentation.xojo.com/index.php/Database.Commit)

[https://www.sqlite.org/lang\_transaction.html](https://www.sqlite.org/lang_transaction.html)

> **[SQLite - Transactions](http://www.tutorialspoint.com/sqlite/sqlite_transactions.htm)**
>
> SQLite - Transactions, A transaction is a unit of work that is performed against a database. Transactions are units or sequences of work accomplished in a logical order, whether in a

---

<div class="post-metadata">

**Author:** ![Rami\_Hassan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@Rami\_Hassan](https://forum.xojo.com/u/Rami_Hassan)\
**Post date:** [January 4, 2015, 4:37pm UTC](https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367/4 "2015-01-04T16:37:37Z")

</div>

I thought the database won’t save unless I commit it?

---

<div class="post-metadata">

**Author:** ![Oliver\_Osswald](https://forum.xojo.com/user_avatar/forum.xojo.com/oliver_osswald/32/6972_2.png) [@Oliver\_Osswald](https://forum.xojo.com/u/Oliver_Osswald)\
**Post date:** [January 4, 2015, 4:44pm UTC](https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367/5 "2015-01-04T16:44:33Z")

</div>

SQLiteDAtabase standard setting is to immediately execute each insert, update or delete and make permanent changes to the database.

There are cases where you can drastically speed up the execution by wrapping it into a a transaction with begin and commit.  
Once you begin a transaction you can rollback or commit it.

This is also useful when you have to execute several statements in a row which belong together: when all of them successful, then commit, otherwise rollback.

To answer your question : SQLitedatabase does not need a commit in order to save an insert, update or delete.

---

<div class="post-metadata">

**Author:** ![Oliver\_Osswald](https://forum.xojo.com/user_avatar/forum.xojo.com/oliver_osswald/32/6972_2.png) [@Oliver\_Osswald](https://forum.xojo.com/u/Oliver_Osswald)\
**Post date:** [January 4, 2015, 4:59pm UTC](https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367/6 "2015-01-04T16:59:19Z")

</div>

I would also do some error checking after each update and in case of an error, then rollback the transaction and exit the while loop. You can set a variable ErrorOccured to TRUE and skip the commit at the end …

---

<div class="post-metadata">

**Author:** ![Rami\_Hassan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@Rami\_Hassan](https://forum.xojo.com/u/Rami_Hassan)\
**Post date:** [January 4, 2015, 5:43pm UTC](https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367/7 "2015-01-04T17:43:56Z")

</div>

I removed the `gDatabase.Commit` but it still has the same result.

---

<div class="post-metadata">

**Author:** ![Rami\_Hassan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@Rami\_Hassan](https://forum.xojo.com/u/Rami_Hassan)\
**Post date:** [January 4, 2015, 5:50pm UTC](https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367/8 "2015-01-04T17:50:08Z")

</div>

It’s fixed now!

Thank you to everyone for their help!

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:33pm UTC](https://forum.xojo.com/t/getting-error-cannot-commit-no-transaction-is-active/21367/9 "2020-10-29T17:33:37Z")

</div>


