📄 capi3.test
字号:
sqlite3_column_count $STMT} 3check_header $STMT capi3-5.21 {a sum(b) max(c)} {VARINT {} {}}check_origin_header $STMT capi3-5.22 {main {} {}} {t1 {} {}} {a {} {}}do_test capi3-5.23 { sqlite3_finalize $STMT} SQLITE_OKdo_test capi3-5.30 { set sql "SELECT a AS x, sum(b) AS y, max(c) AS z FROM t1 AS m GROUP BY x" set STMT [sqlite3_prepare $DB $sql -1 TAIL] sqlite3_column_count $STMT} 3check_header $STMT capi3-5.31 {x y z} {VARINT {} {}}check_origin_header $STMT capi3-5.32 {main {} {}} {t1 {} {}} {a {} {}}do_test capi3-5.33 { sqlite3_finalize $STMT} SQLITE_OKset ::ENC [execsql {pragma encoding}]db closedo_test capi3-6.0 { sqlite3 db test.db set DB [sqlite3_connection_pointer db] sqlite3_key $DB xyzzy set sql {SELECT a FROM t1 order by rowid} set STMT [sqlite3_prepare $DB $sql -1 TAIL] expr 0} {0}do_test capi3-6.1 { db cache flush sqlite3_close $DB} {SQLITE_BUSY}do_test capi3-6.2 { sqlite3_step $STMT} {SQLITE_ROW}check_data $STMT capi3-6.3 {INTEGER} {1} {1.0} {1}do_test capi3-6.3 { sqlite3_finalize $STMT} {SQLITE_OK}do_test capi3-6.4-misuse { db cache flush sqlite3_close $DB} {SQLITE_OK}db close# This procedure sets the value of the file-format in file 'test.db'# to $newval. Also, the schema cookie is incremented.# proc set_file_format {newval} { hexio_write test.db 44 [hexio_render_int32 $newval] set schemacookie [hexio_get_int [hexio_read test.db 40 4]] incr schemacookie hexio_write test.db 40 [hexio_render_int32 $schemacookie] return {}}# This procedure returns the value of the file-format in file 'test.db'.# proc get_file_format {{fname test.db}} { return [hexio_get_int [hexio_read $fname 44 4]]}if {![sqlite3 -has-codec]} { # Test what happens when the library encounters a newer file format. do_test capi3-7.1 { set_file_format 5 } {} do_test capi3-7.2 { sqlite3 db test.db catchsql { SELECT * FROM sqlite_master; } } {1 {unsupported file format}} db close}if {![sqlite3 -has-codec]} { # Now test that the library correctly handles bogus entries in the # sqlite_master table (schema corruption). do_test capi3-8.1 { file delete -force test.db test.db-journal sqlite3 db test.db execsql { CREATE TABLE t1(a); } db close } {} do_test capi3-8.2 { sqlite3 db test.db execsql { PRAGMA writable_schema=ON; INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL); } db close } {} do_test capi3-8.3 { sqlite3 db test.db catchsql { SELECT * FROM sqlite_master; } } {1 {malformed database schema (?)}} do_test capi3-8.4 { # Build a 5-field row record. The first field is a string 'table', and # subsequent fields are all NULL. db close file delete -force test.db test.db-journal sqlite3 db test.db execsql { CREATE TABLE t1(a); PRAGMA writable_schema=ON; INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL); } db close } {}; do_test capi3-8.5 { sqlite3 db test.db catchsql { SELECT * FROM sqlite_master; } } {1 {malformed database schema (?)}} db close}file delete -force test.dbfile delete -force test.db-journal# Test the english language string equivalents for sqlite error codesset code2english [list \SQLITE_OK {not an error} \SQLITE_ERROR {SQL logic error or missing database} \SQLITE_PERM {access permission denied} \SQLITE_ABORT {callback requested query abort} \SQLITE_BUSY {database is locked} \SQLITE_LOCKED {database table is locked} \SQLITE_NOMEM {out of memory} \SQLITE_READONLY {attempt to write a readonly database} \SQLITE_INTERRUPT {interrupted} \SQLITE_IOERR {disk I/O error} \SQLITE_CORRUPT {database disk image is malformed} \SQLITE_FULL {database or disk is full} \SQLITE_CANTOPEN {unable to open database file} \SQLITE_EMPTY {table contains no data} \SQLITE_SCHEMA {database schema has changed} \SQLITE_CONSTRAINT {constraint failed} \SQLITE_MISMATCH {datatype mismatch} \SQLITE_MISUSE {library routine called out of sequence} \SQLITE_NOLFS {large file support is disabled} \SQLITE_AUTH {authorization denied} \SQLITE_FORMAT {auxiliary database format error} \SQLITE_RANGE {bind or column index out of range} \SQLITE_NOTADB {file is encrypted or is not a database} \unknownerror {unknown error} \]set test_number 1foreach {code english} $code2english { do_test capi3-9.$test_number "sqlite3_test_errstr $code" $english incr test_number}# Test the error message when a "real" out of memory occurs.ifcapable memdebug { do_test capi3-10-1 { sqlite3 db test.db set DB [sqlite3_connection_pointer db] sqlite3_memdebug_fail 1 catchsql { select * from sqlite_master; } } {1 {out of memory}} do_test capi3-10-2 { sqlite3_errmsg $::DB } {out of memory} ifcapable {utf16} { do_test capi3-10-3 { utf8 [sqlite3_errmsg16 $::DB] } {out of memory} } db close sqlite3_memdebug_fail -1 do_test capi3-10-4 { sqlite3 db test.db set DB [sqlite3_connection_pointer db] sqlite3_memdebug_fail 1 catchsql { select * from sqlite_master where rowid>5; } } {1 {out of memory}} do_test capi3-10-5 { sqlite3_errmsg $::DB } {out of memory} ifcapable {utf16} { do_test capi3-10-6 { utf8 [sqlite3_errmsg16 $::DB] } {out of memory} } db close sqlite3_memdebug_fail -1}# The following tests - capi3-11.* - test that a COMMIT or ROLLBACK# statement issued while there are still outstanding VMs that are part of# the transaction fails.sqlite3 db test.dbset DB [sqlite3_connection_pointer db]sqlite_register_test_function $DB funcdo_test capi3-11.1 { execsql { BEGIN; CREATE TABLE t1(a, b); INSERT INTO t1 VALUES(1, 'int'); INSERT INTO t1 VALUES(2, 'notatype'); }} {}do_test capi3-11.1.1 { sqlite3_get_autocommit $DB} 0do_test capi3-11.2 { set STMT [sqlite3_prepare $DB "SELECT func(b, a) FROM t1" -1 TAIL] sqlite3_step $STMT} {SQLITE_ROW}do_test capi3-11.3 { catchsql { COMMIT; }} {1 {cannot commit transaction - SQL statements in progress}}do_test capi3-11.3.1 { sqlite3_get_autocommit $DB} 0do_test capi3-11.4 { sqlite3_step $STMT} {SQLITE_ERROR}do_test capi3-11.5 { sqlite3_finalize $STMT} {SQLITE_ERROR}do_test capi3-11.6 { catchsql { SELECT * FROM t1; }} {0 {1 int 2 notatype}}do_test capi3-11.6.1 { sqlite3_get_autocommit $DB} 0do_test capi3-11.7 { catchsql { COMMIT; }} {0 {}}do_test capi3-11.7.1 { sqlite3_get_autocommit $DB} 1do_test capi3-11.8 { execsql { CREATE TABLE t2(a); INSERT INTO t2 VALUES(1); INSERT INTO t2 VALUES(2); BEGIN; INSERT INTO t2 VALUES(3); }} {}do_test capi3-11.8.1 { sqlite3_get_autocommit $DB} 0do_test capi3-11.9 { set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL] sqlite3_step $STMT} {SQLITE_ROW}do_test capi3-11.9.1 { sqlite3_get_autocommit $DB} 0do_test capi3-11.9.2 { catchsql { ROLLBACK; }} {1 {cannot rollback transaction - SQL statements in progress}}do_test capi3-11.9.3 { sqlite3_get_autocommit $DB} 0do_test capi3-11.10 { sqlite3_step $STMT} {SQLITE_ROW}do_test capi3-11.11 { sqlite3_step $STMT} {SQLITE_ROW}do_test capi3-11.12 { sqlite3_step $STMT} {SQLITE_DONE}do_test capi3-11.13 { sqlite3_finalize $STMT} {SQLITE_OK}do_test capi3-11.14 { execsql { SELECT a FROM t2; }} {1 2 3}do_test capi3-11.14.1 { sqlite3_get_autocommit $DB} 0do_test capi3-11.15 { catchsql { ROLLBACK; }} {0 {}}do_test capi3-11.15.1 { sqlite3_get_autocommit $DB} 1do_test capi3-11.16 { execsql { SELECT a FROM t2; }} {1 2}# Sanity check on the definition of 'outstanding VM'. This means any VM# that has had sqlite3_step() called more recently than sqlite3_finalize() or# sqlite3_reset(). So a VM that has just been prepared or reset does not# count as an active VM.do_test capi3-11.17 { execsql { BEGIN; }} {}do_test capi3-11.18 { set STMT [sqlite3_prepare $DB "SELECT a FROM t1" -1 TAIL] catchsql { COMMIT; }} {0 {}}do_test capi3-11.19 { sqlite3_step $STMT} {SQLITE_ROW}do_test capi3-11.20 { catchsql { BEGIN; COMMIT; }} {1 {cannot commit transaction - SQL statements in progress}}do_test capi3-11.20 { sqlite3_reset $STMT catchsql { COMMIT; }} {0 {}}do_test capi3-11.21 { sqlite3_finalize $STMT} {SQLITE_OK}# The following tests - capi3-12.* - check that its Ok to start a# transaction while other VMs are active, and that its Ok to execute# atomic updates in the same situation #do_test capi3-12.1 { set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL] sqlite3_step $STMT} {SQLITE_ROW}do_test capi3-12.2 { catchsql { INSERT INTO t1 VALUES(3, NULL); }} {0 {}}do_test capi3-12.3 { catchsql { INSERT INTO t2 VALUES(4); }} {0 {}}do_test capi3-12.4 { catchsql { BEGIN; INSERT INTO t1 VALUES(4, NULL); }} {0 {}}do_test capi3-12.5 { sqlite3_step $STMT} {SQLITE_ROW}do_test capi3-12.5.1 { sqlite3_step $STMT} {SQLITE_ROW}do_test capi3-12.6 { sqlite3_step $STMT} {SQLITE_DONE}do_test capi3-12.7 { sqlite3_finalize $STMT} {SQLITE_OK}do_test capi3-12.8 { execsql { COMMIT; SELECT a FROM t1; }} {1 2 3 4}# Test cases capi3-13.* test the sqlite3_clear_bindings() and # sqlite3_sleep APIs.#if {[llength [info commands sqlite3_clear_bindings]]>0} { do_test capi3-13.1 { execsql { DELETE FROM t1; } set STMT [sqlite3_prepare $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL] sqlite3_step $STMT } {SQLITE_DONE} do_test capi3-13.2 { sqlite3_reset $STMT sqlite3_bind_text $STMT 1 hello 5 sqlite3_bind_text $STMT 2 world 5 sqlite3_step $STMT } {SQLITE_DONE} do_test capi3-13.3 { sqlite3_reset $STMT sqlite3_clear_bindings $STMT sqlite3_step $STMT } {SQLITE_DONE} do_test capi3-13-4 { sqlite3_finalize $STMT execsql { SELECT * FROM t1; } } {{} {} hello world {} {}}}if {[llength [info commands sqlite3_sleep]]>0} { do_test capi3-13-5 { set ms [sqlite3_sleep 80] expr {$ms==80 || $ms==1000} } {1}}# Ticket #1219: Make sure binding APIs can handle a NULL pointer.#do_test capi3-14.1-misuse { set rc [catch {sqlite3_bind_text 0 1 hello 5} msg] lappend rc $msg} {1 SQLITE_MISUSE}# Ticket #1650: Honor the nBytes parameter to sqlite3_prepare.#do_test capi3-15.1 { set sql {SELECT * FROM t2} set nbytes [string length $sql] append sql { WHERE a==1} set STMT [sqlite3_prepare $DB $sql $nbytes TAIL] sqlite3_step $STMT sqlite3_column_int $STMT 0} {1}do_test capi3-15.2 { sqlite3_step $STMT sqlite3_column_int $STMT 0} {2}do_test capi3-15.3 { sqlite3_finalize $STMT} {SQLITE_OK}do_test capi3-15.4 { # 123456789 1234567 set sql {SELECT 1234567890} set STMT [sqlite3_prepare $DB $sql 8 TAIL] sqlite3_step $STMT set v1 [sqlite3_column_int $STMT 0] sqlite3_finalize $STMT set v1} {1}do_test capi3-15.5 { # 123456789 1234567 set sql {SELECT 1234567890} set STMT [sqlite3_prepare $DB $sql 9 TAIL] sqlite3_step $STMT set v1 [sqlite3_column_int $STMT 0] sqlite3_finalize $STMT set v1} {12}do_test capi3-15.6 { # 123456789 1234567 set sql {SELECT 1234567890} set STMT [sqlite3_prepare $DB $sql 12 TAIL] sqlite3_step $STMT set v1 [sqlite3_column_int $STMT 0] sqlite3_finalize $STMT set v1} {12345}do_test capi3-15.7 { # 123456789 1234567 set sql {SELECT 12.34567890} set STMT [sqlite3_prepare $DB $sql 12 TAIL] sqlite3_step $STMT set v1 [sqlite3_column_double $STMT 0] sqlite3_finalize $STMT set v1} {12.34}do_test capi3-15.8 { # 123456789 1234567 set sql {SELECT 12.34567890} set STMT [sqlite3_prepare $DB $sql 14 TAIL] sqlite3_step $STMT set v1 [sqlite3_column_double $STMT 0] sqlite3_finalize $STMT set v1} {12.3456}# Make sure code is always generated even if an IF EXISTS or # IF NOT EXISTS clause is present that the table does not or# does exists. That way we will always have a prepared statement# to expire when the schema changes.#do_test capi3-16.1 { set sql {DROP TABLE IF EXISTS t3} set STMT [sqlite3_prepare $DB $sql -1 TAIL] sqlite3_finalize $STMT expr {$STMT!=""}} {1}do_test capi3-16.2 { set sql {CREATE TABLE IF NOT EXISTS t1(x,y)} set STMT [sqlite3_prepare $DB $sql -1 TAIL] sqlite3_finalize $STMT expr {$STMT!=""}} {1}# But still we do not generate code if there is no SQL#do_test capi3-16.3 { set STMT [sqlite3_prepare $DB {} -1 TAIL] sqlite3_finalize $STMT expr {$STMT==""}} {1}do_test capi3-16.4 { set STMT [sqlite3_prepare $DB {;} -1 TAIL] sqlite3_finalize $STMT expr {$STMT==""}} {1}# Ticket #2426: Misuse of sqlite3_column_* by calling it after# a sqlite3_reset should be harmless.#do_test capi3-17.1 { set STMT [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL] sqlite3_step $STMT sqlite3_column_int $STMT 0} {1}do_test capi3-17.2 { sqlite3_reset $STMT sqlite3_column_int $STMT 0} {0}do_test capi3-17.3 { sqlite3_finalize $STMT} {SQLITE_OK}# Verify that sqlite3_step() fails with an SQLITE_SCHEMA error# when the statement is prepared with sqlite3_prepare() (not# sqlite3_prepare_v2()) and the schema has changed.#do_test capi3-18.1 { set STMT [sqlite3_prepare db {SELECT * FROM t2} -1 TAIL] sqlite3 db2 test.db db2 eval {CREATE TABLE t3(x)} db2 close sqlite3_step $STMT} {SQLITE_ERROR}do_test capi3-18.2 { sqlite3_reset $STMT sqlite3_errcode db} {SQLITE_SCHEMA}do_test capi3-18.3 { sqlite3_errmsg db} {database schema has changed}# The error persist on retry when sqlite3_prepare() has been used.do_test capi3-18.4 { sqlite3_step $STMT} {SQLITE_ERROR}do_test capi3-18.5 { sqlite3_reset $STMT sqlite3_errcode db} {SQLITE_SCHEMA}do_test capi3-18.6 { sqlite3_errmsg db} {database schema has changed}sqlite3_finalize $STMT# Ticket #3134. Prepare a statement with an nBytes parameter of 0.# Make sure this works correctly and does not reference memory out of# range.#do_test capi3-19.1 { sqlite3_prepare_tkt3134 db} {}# Tests of the interface when no VFS is registered.#if {![info exists tester_do_binarylog]} { db close vfs_unregister_all do_test capi3-20.1 { sqlite3_sleep 100 } {0} vfs_reregister_all}finish_test
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -