SQLite BEGIN TRANSACTION创建提交事务 http://www.uedsc.com/sqlite-begin-transaction.html

本文介绍了SQLite数据库中的事务管理机制,包括如何开始、提交和回滚事务,以及不同类型的事务(延迟、立即和排他)的区别。此外,还详细解释了在遇到特定错误时事务的处理方式。

http://www.uedsc.com/sqlite-begin-transaction.html


除非在一个事务中,否则是不能对数据库进行更改的。 如果当前不在事务中, 任何改变数据库的命令(基本上,任何除SELECT之外的 SQL 语句)都会自动开始一个事务。 自动开始的事务在命令执行完成后会自动提交。

sql-statement ::= BEGIN [ DEFERRED | IMMEDIATE | EXCLUSIVE ] [TRANSACTION [name]]
sql-statement ::= END [TRANSACTION [name]]
sql-statement ::= COMMIT [TRANSACTION [name]]
sql-statement ::= ROLLBACK [TRANSACTION [name]]

可以通过使用 BEGIN 命令手工开始一个事务。这种事务将会持续到下一个 COMMIT 或 ROLLBACK 命令为止。但如果数据库关闭,或者发生错误并且 ROLLBACK 冲突解决算法已指定的情况下,事务也会回滚。 有关冲突解决算法引起的回滚详见 ON CONFLICT

END TRANSACTION 是 COMMIT 的一个别名。

当前,可选的事务名(transaction name)是被忽略的。 SQLite当前也不支持嵌套的事务。 但是,未来的版本也可能增加对嵌套事务的支持,事务名也可能很重要。 我们建议为防止引起未来的兼容问题,在你的应用程序中暂时不要使用事务名。

事务可以延迟执行(deferred),立即执行(immediate)或排它执行(exclusive)。 缺省的事务行为是延迟地。延迟意味着直到数据库第一次被访问之前不需要加锁。

如此,对于一个延迟的事务,BEGIN命令什么也不做。直到第一个读或写操作之前, 不需要加锁。对数据库的第一个读操作会产生一个共享锁(SHARED), 第一个写操作会加一个保持锁(RESERVED)。由于对锁的需要是延迟的, 直到确实需要为止,在当前进程执行BEGIN之后, 有可能其它的进程或线程会启动一个另外的事务并写到数据库。

如果事务是立即的,那么,执行BEGIN命令将立即在所有数据库上获得一个RESERVED锁, 而不用等待数据库被访问到。在BEGIN IMMEDIATE之后, 可以保证没有其它进程下写入数据库中执行BEGIN IMMEDIATE或BEGIN EXCLUSIVE。 但其它进程仍可以从数据库中读数据。

一个排它的事务将在所有数据库上获得 EXCLUSIVE 锁,在该事务结束之前,没有任何其它线程或进程可以读、写数据库。

对于 SHARED, RESERVED, 和 EXCLUSIVE 的描述 单独在这里列出。

COMMIT 命令在所有未决的 SQL 命令完成之前实际上不会真正提交。 也就是说,如果事务中有两个或多个 SELECT 语句,并且执行了一个 COMMIT, 那么在 SELECT 语句完成之前不会发生提交。

试图执行 COMMIT 可能会导致返回 SQLITE_BUSY 代码。 它表示另外一个线程或进程在数据库上有一个读锁,从而阻止了数据库的更新。 当 COMMIT 在这种情况下失败时,事务仍然是活动的, 如果读者有机会清除,那么事务可以重新执行 COMMIT 。

在事务中对错误作出响应

在事务中,如果发生了特定的错误,事务可能会也可能不会自动回滚。 引起这些行为的错误包括:

  • SQLITE_FULL: 数据库或磁盘满
  • SQLITE_IOERR: 磁盘 I/O 出错
  • SQLITE_BUSY: 数据库被其它进程使用
  • SQLITE_NOMEM: 内存不够
  • SQLITE_INTERRUPT: 操作被用户中断

对于这所有的错误,SQLite 试图undo它正在执行的这条语句, 并对当前事务中已经执行的那些语句保持原样(保持它们对数据库所做的改变)。 但是,根据正在执行的这条语句和错误发生的点,可能需要SQLite 回滚并取消该事务。 应用程序可以通过 sqlite3_get_autocommit() C 语言接口获得是何种原因导致SQLite 执行何种动作。

建议在程序中使用明确的 ROLLBACK 命令来响应上面列出的各种错误。 如果事务被错误响应自动回滚了,执行 ROLLBACK 失败关引发一个错误, 但没什么害处。

未来版本的 SQLite 可能会扩展这一可能引起自动回滚的错误列表, 也可能改变这些错误的响应。特别的, 我们在SQLite未来的版本中可能会选择简化这一接口- 对上面可能引发的错误直接强制无条件的回滚。


你是一个unity专家 这个问题如何解决数据库路径 URI=file:/storage/emulated/0/Android/data/com.cmgames.CoinCars/files/chatdata.db 13007-21 07:30:10.553 18694 18874 D nativeloader: Load /data/app/~~sTWOKcK788AlzkZ6vsXqEg==/com.cmgames.CoinCars-Fel0V_2Qo_-P7BgnH0L53A==/lib/arm64/libsqlite3.so using class loader ns clns-4 (caller=/data/app/~~sTWOKcK788AlzkZ6vsXqEg==/com.cmgames.CoinCars-Fel0V_2Qo_-P7BgnH0L53A==/base.apk!classes3.dex): ok 13107-21 07:30:10.595 18694 18874 I Unity : result 1 13207-21 07:30:12.093 18694 18874 E Unity : Got the fucking exception :c# exception:SQLite error 13307-21 07:30:12.093 18694 18874 E Unity : near "ari": syntax error,stack: at Mono.Data.Sqlite.SQLite3.Prepare (Mono.Data.Sqlite.SqliteConnection cnn, System.String strSql, Mono.Data.Sqlite.SqliteStatement previous, System.UInt32 timeoutMS, System.String& strRemain) [0x00000] in <00000000000000000000000000000000>:0 13407-21 07:30:12.093 18694 18874 E Unity : at Mono.Data.Sqlite.SqliteCommand.BuildNextCommand () [0x00000] in <00000000000000000000000000000000>:0 13507-21 07:30:12.093 18694 18874 E Unity : at Mono.Data.Sqlite.SqliteDataReader.NextResult () [0x00000] in <00000000000000000000000000000000>:0 13607-21 07:30:12.093 18694 18874 E Unity : at Mono.Data.Sqlite.SqliteCommand.ExecuteReader (System.Data.CommandBehavior behavior) [0x00000] in <00000000000000000000000000000000>:0 13707-21 07:30:12.093 18694 18874 E Unity : at Mono.Data.Sqlite.SqliteCommand.ExecuteNonQuery () [0x00000] in <00000000000000000000000000000000>:0 13807-21 07:30:12.093 18694 18874 E Unity : at CustomSqlite.LChatSqlChatData.SetChatData (CustomSqlite.LChatSqlDataBase+SqlChatData chatData) [0x00000] in <00000000000000000000000000000000>:0 13907-21 07:30:12.093 18694 18874 E Unity : at CustomSqlite.LChatSqliteManager.SetChatData (System.String jsonData) [0x00000] i
07-22
(base) C:\Users\yhh>conda env list # conda environments: # pythonnew C:\Users\yhh\.conda\envs\pythonnew base * D:\anaconda4 D:\labelimg D:\yolov5 (base) C:\Users\yhh>d: (base) D:\>conda create -n paddle python=3.8 Collecting package metadata (current_repodata.json): | DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 - DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/main/noarch/current_repodata.json HTTP/1.1" 304 0 / DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/msys2/win-64/current_repodata.json HTTP/1.1" 304 0 \ DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/msys2/noarch/current_repodata.json HTTP/1.1" 304 0 | DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/r/noarch/current_repodata.json HTTP/1.1" 304 0 - DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/r/win-64/current_repodata.json HTTP/1.1" 304 0 DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/main/win-64/current_repodata.json HTTP/1.1" 304 0 done Solving environment: unsuccessful attempt using repodata from current_repodata.json, retrying with next repodata source. Collecting package metadata (repodata.json): - DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 / DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/main/noarch/repodata.json HTTP/1.1" 304 0 / DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/r/win-64/repodata.json HTTP/1.1" 304 0 / DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/r/noarch/repodata.json HTTP/1.1" 304 0 \ DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/main/win-64/repodata.json HTTP/1.1" 304 0 \ DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/msys2/win-64/repodata.json HTTP/1.1" 304 0 | DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/msys2/noarch/repodata.json HTTP/1.1" 304 0 done Solving environment: done ==> WARNING: A newer version of conda exists. <== current version: 23.7.2 latest version: 25.5.1 Please update conda by running $ conda update -n base -c defaults conda Or to minimize the number of packages updated during conda update use conda install conda=25.5.1 ## Package Plan ## environment location: C:\Users\yhh\.conda\envs\paddle added / updated specs: - python=3.8 The following NEW packages will be INSTALLED: ca-certificates pkgs/main/win-64::ca-certificates-2025.2.25-haa95532_0 libffi pkgs/main/win-64::libffi-3.4.4-hd77b12b_1 openssl pkgs/main/win-64::openssl-3.0.16-h3f729d1_0 pip pkgs/main/win-64::pip-24.2-py38haa95532_0 python pkgs/main/win-64::python-3.8.20-h8205438_0 setuptools pkgs/main/win-64::setuptools-75.1.0-py38haa95532_0 sqlite pkgs/main/win-64::sqlite-3.45.3-h2bbff1b_0 vc pkgs/main/win-64::vc-14.42-haa95532_5 vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.42.34433-hbfb602d_5 wheel pkgs/main/win-64::wheel-0.44.0-py38haa95532_0 Proceed ([y]/n)? y Downloading and Extracting Packages Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate paddle # # To deactivate an active environment, use # # $ conda deactivate (base) D:\>conda env list # conda environments: # paddle C:\Users\yhh\.conda\envs\paddle pythonnew C:\Users\yhh\.conda\envs\pythonnew base * D:\anaconda4 D:\labelimg D:\yolov5 (base) D:\>conda install paddlepaddle-gpu==2.3.2 cudatoolkit=10.1 --channel https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/ Collecting package metadata (current_repodata.json): - DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): mirrors.tuna.tsinghua.edu.cn:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): mirrors.tuna.tsinghua.edu.cn:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 | DEBUG:urllib3.connectionpool:https://mirrors.tuna.tsinghua.edu.cn:443 "GET /anaconda/cloud/Paddle/win-64/current_repodata.json HTTP/1.1" 404 None DEBUG:urllib3.connectionpool:https://mirrors.tuna.tsinghua.edu.cn:443 "GET /anaconda/cloud/Paddle/noarch/current_repodata.json HTTP/1.1" 404 None DEBUG:urllib3.connectionpool:https://mirrors.tuna.tsinghua.edu.cn:443 "GET /anaconda/cloud/Paddle/win-64/repodata.json HTTP/1.1" 304 0 DEBUG:urllib3.connectionpool:https://mirrors.tuna.tsinghua.edu.cn:443 "GET /anaconda/cloud/Paddle/noarch/repodata.json HTTP/1.1" 304 0 \ DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/msys2/noarch/current_repodata.json HTTP/1.1" 304 0 DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/r/noarch/current_repodata.json HTTP/1.1" 304 0 | DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/main/noarch/current_repodata.json HTTP/1.1" 304 0 DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/main/win-64/current_repodata.json HTTP/1.1" 304 0 \ DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/r/win-64/current_repodata.json HTTP/1.1" 304 0 | DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/msys2/win-64/current_repodata.json HTTP/1.1" 304 0 done Solving environment: unsuccessful initial attempt using frozen solve. Retrying with flexible solve. Solving environment: unsuccessful attempt using repodata from current_repodata.json, retrying with next repodata source. Collecting package metadata (repodata.json): | DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): mirrors.tuna.tsinghua.edu.cn:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): mirrors.tuna.tsinghua.edu.cn:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443 / DEBUG:urllib3.connectionpool:https://mirrors.tuna.tsinghua.edu.cn:443 "GET /anaconda/cloud/Paddle/win-64/repodata.json HTTP/1.1" 304 0 DEBUG:urllib3.connectionpool:https://mirrors.tuna.tsinghua.edu.cn:443 "GET /anaconda/cloud/Paddle/noarch/repodata.json HTTP/1.1" 304 0 / DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/r/noarch/repodata.json HTTP/1.1" 304 0 - DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/main/noarch/repodata.json HTTP/1.1" 304 0 DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/main/win-64/repodata.json HTTP/1.1" 304 0 \ DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/msys2/win-64/repodata.json HTTP/1.1" 304 0 / DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/r/win-64/repodata.json HTTP/1.1" 304 0 DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/msys2/noarch/repodata.json HTTP/1.1" 304 0 done Solving environment: unsuccessful initial attempt using frozen solve. Retrying with flexible solve. Solving environment: \ Found conflicts! Looking for incompatible packages. This can take several minutes. Press CTRL-C to abort. failed UnsatisfiableError: The following specifications were found to be incompatible with the existing python installation in your environment: Specifications: - paddlepaddle-gpu==2.3.2 -> python[version='>=3.6,<3.7|>=3.7,<3.8|>=3.8,<3.9|>=3.9,<3.10'] Your python: python=3.11 If python is on the left-most side of the chain, that's the version you've asked for. When python appears to the right, that indicates that the thing on the left is somehow not available for the python version you are constrained to. Note that conda will not change your python version to a different minor version unless you explicitly specify that. The following specifications were found to be incompatible with each other: Output in format: Requested package -> Available versions Package cudatoolkit conflicts for: paddlepaddle-gpu==2.3.2 -> cudatoolkit[version='>=10.1,<10.2|>=10.2,<10.3|>=11.2,<11.3|>=11.6,<11.7'] paddlepaddle-gpu==2.3.2 -> cudnn[version='>=8.2,<8.3'] -> cudatoolkit[version='>=10.0,<10.1|>=11.0,<11.4|>=9.2,<9.3|>=9.0,<9.1'] Package vc conflicts for: python=3.11 -> vc[version='>=14.1,<15.0a0|>=14.2,<15.0a0'] python=3.11 -> bzip2[version='>=1.0.8,<2.0a0'] -> vc=9 Package vs2015_runtime conflicts for: cudatoolkit=10.1 -> vc[version='>=14.1,<15.0a0'] -> vs2015_runtime[version='>=14.15.26706|>=14.27.29016|>=14.29.30133|>=14.40.33807|>=14.42.34433|>=14.16.27012'] paddlepaddle-gpu==2.3.2 -> numpy[version='>=1.13'] -> vs2015_runtime[version='>=14.16.27012,<15.0a0|>=14.27.29016,<15.0a0|>=14.29.30133,<15.0a0'] python=3.11 -> vs2015_runtime[version='>=14.16.27012,<15.0a0|>=14.29.30133,<15.0a0'] cudatoolkit=10.1 -> vs2015_runtime[version='>=14.16.27012,<15.0a0'] python=3.11 -> tk[version='>=8.6.14,<8.7.0a0'] -> vs2015_runtime[version='>=14.15.26706|>=14.27.29016|>=14.27.29016,<15.0a0|>=14.42.34433|>=14.40.33807|>=14.29.30133|>=14.16.27012'] (base) D:\>
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值