Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQLite PRAGMA

SQLite PRAGMA

SQLite's PRAGMA PRAGMA command is a special command that can be used to control various environment variables and status flags within the SQLite environment. A PRAGMA value can be read or set as needed.

Syntax

To query the current PRAGMA value, simply provide the name of the pragma:



PRAGMA pragma_name;

To set a new value for a PRAGMA, the syntax is as follows:



PRAGMA pragma_name = value;

Set the mode, which can be a name or an equivalent integer, but the returned value will always be an integer.

auto_vacuum Pragma

auto_vacuum The Pragma gets or sets the auto-vacuum mode. The syntax is as follows:



PRAGMA [database.]auto_vacuum;

PRAGMA [database.]auto_vacuum = mode;

Wheremode can be any one of the following:

Pragma value Description
0 or NONE Disable Auto-vacuum. This is the default mode, meaning the database file size will not shrink unless the VACUUM command is manually used.
1 or FULL Enable Auto-vacuum, fully automatic. In this mode, the database file is allowed to shrink as data is removed from the database.
2 or INCREMENTAL Enable Auto-vacuum, but it must be manually activated. In this mode, referenced data is maintained and free pages are placed only in the free list. These pages can be used at any time incremental_vacuum pragma to overwrite.

cache_size Pragma

cache_size The Pragma gets or temporarily sets the maximum size of the page cache in memory. The syntax is as follows:



PRAGMA [database.]cache_size;

PRAGMA [database.]cache_size = pages;

pages value represents the number of pages in the cache. The default size of the built-in page cache is 2,000 pages, and the minimum size is 10 pages.

case_sensitive_like Pragma

case_sensitive_like The Pragma controls the case sensitivity of the built-in LIKE expression. By default, this Pragma is false, which means the built-in LIKE operator ignores letter case. The syntax is as follows:



PRAGMA case_sensitive_like = [true|false];

Currently there is no way to query the current state of this Pragma.

count_changes Pragma

count_changes The Pragma gets or sets the return values of data manipulation statements such as INSERT, UPDATE, and DELETE. The syntax is as follows:



PRAGMA count_changes;

PRAGMA count_changes = [true|false];

By default, this Pragma is false and these statements return nothing. If set to true, each of the mentioned statements will return a single-row, single-column table consisting of a single integer value representing the number of rows affected by the operation.

database_list Pragma

database_list The Pragma is used to list all database connections. The syntax is as follows:



PRAGMA database_list;

This Pragma will return a single-row, three-column table, giving, whenever a database is opened or attached, the sequence number in the database, its name, and the associated file.

encoding Pragma

encoding The Pragma controls how strings are encoded and stored in the database file. The syntax is as follows:



PRAGMA encoding;

PRAGMA encoding = format;

format value can be one of UTF-8, UTF-16le, or UTF-16be.

freelist_count Pragma

freelist_count The Pragma returns an integer indicating the number of database pages currently marked as free and available. The syntax is as follows:



PRAGMA [database.]freelist_count;

index_info Pragma

index_info The Pragma returns information about database indexes. The syntax is as follows:



PRAGMA [database.]index_info( index_name );

The result set will display one row for each column in the given column sequence in an index, the column index within the table, and the column name.

index_list Pragma

index_list The Pragma lists all indexes associated with a table. The syntax is as follows:



PRAGMA [database.]index_list( table_name );

The result set displays one row for each index, giving the column sequence, index name, and a flag indicating whether the index is unique.

journal_mode Pragma

journal_mode Pragma gets or sets the journal mode that controls how the log file is stored and handled. The syntax is as follows::



PRAGMA journal_mode;

PRAGMA journal_mode = mode;

PRAGMA database.journal_mode;

PRAGMA database.journal_mode = mode;

Five journal modes are supported:

Pragma value Description
DELETE Default mode. In this mode, the log file is deleted at the end of a transaction.
TRUNCATE The log file is truncated to zero bytes in length.
PERSIST The log file is left in place, but the header is rewritten to indicate that the log is no longer valid.
MEMORY Journal records are kept in memory rather than on disk.
OFF No journal records are kept.

max_page_count Pragma

max_page_count Pragma gets or sets the maximum number of pages allowed for the database. The syntax is as follows:



PRAGMA [database.]max_page_count;

PRAGMA [database.]max_page_count = max_page;

The default value is 1,073,741,823, which is a gigabyte of pages, that is, with the default 1 KB page size, a megabyte of growth in the database.

page_count Pragma

page_count Pragma returns the number of pages in the current database. The syntax is as follows:



PRAGMA [database.]page_count;

The size of the database file should be page_count * page_size.

page_size Pragma

page_size Pragma gets or sets the page size of the database. The syntax is as follows:



PRAGMA [database.]page_size;

PRAGMA [database.]page_size = bytes;

By default, the allowed sizes are 512, 1024, 2048, 4096, 8192, 16384, and 32768 bytes. The only way to change the page size of an existing database is to set the page size and then immediately VACUUM the database.

parser_trace Pragma

parser_trace Pragma controls the printing of debug status as it parses SQL commands. The syntax is as follows:



PRAGMA parser_trace = [true|false];

By default it is set to false, but when set to true it is enabled, causing the SQL parser to print its status as it parses SQL commands.

recursive_triggers Pragma

recursive_triggers Pragma gets or sets the recursive triggers feature. If recursive triggers are not enabled, a trigger action will not fire another trigger. The syntax is as follows:



PRAGMA recursive_triggers;

PRAGMA recursive_triggers = [true|false];

schema_version Pragma

schema_version Pragma gets or sets the schema version value stored in the database header. The syntax is as follows:



PRAGMA [database.]schema_version;

PRAGMA [database.]schema_version = number;

This is a 32-bit signed integer value used to track schema changes. This value is incremented each time a schema-changing command is executed (such as CREATE... or DROP...).

secure_delete Pragma

secure_delete Pragma controls how content is deleted from the database. The syntax is as follows:



PRAGMA secure_delete;

PRAGMA secure_delete = [true|false];

PRAGMA database.secure_delete;

PRAGMA database.secure_delete = [true|false];

The default value of the secure-delete flag is usually off, but this can be changed with the SQLITE_SECURE_DELETE build option.

sql_trace Pragma

sql_trace Pragma is used to dump SQL trace results to the screen. The syntax is as follows:



PRAGMA sql_trace;

PRAGMA sql_trace = [true|false];

SQLite must be compiled with the SQLITE_DEBUG directive to reference this Pragma.

synchronous Pragma

synchronous Pragma gets or sets the current disk synchronization mode, which controls how aggressively SQLite writes data to physical storage. The syntax is as follows:



PRAGMA [database.]synchronous;

PRAGMA [database.]synchronous = mode;

SQLite supports the following synchronization modes:

Pragma value Description
0 or OFF No synchronization.
1 or NORMAL Synchronize after each sequence of critical disk operations.
2 or FULL Synchronize after every critical disk operation.

temp_store Pragma

temp_store Pragma gets or sets the storage mode used by temporary database files. The syntax is as follows:



PRAGMA temp_store;

PRAGMA temp_store = mode;

SQLite supports the following storage modes:

Pragma value Description
0 or DEFAULT Use the mode set at compile time by default. Usually FILE.
1 or FILE Use file-based storage.
2 or MEMORY Use memory-based storage.

temp_store_directory Pragma

temp_store_directory Pragma gets or sets the location used for temporary database files. The syntax is as follows:



PRAGMA temp_store_directory;

PRAGMA temp_store_directory = 'directory_path';

user_version Pragma

user_version Pragma gets or sets the user-defined version value stored in the database header. The syntax is as follows:



PRAGMA [database.]user_version;

PRAGMA [database.]user_version = number;

This is a 32-bit signed integer value that can be set by developers for version-tracking purposes.

writable_schema Pragma

writable_schema Pragma gets or sets whether system tables can be modified. The syntax is as follows:



PRAGMA writable_schema;

PRAGMA writable_schema = [true|false];

If this Pragma is set, tables beginning with sqlite_ can be created and modified, including the sqlite_master table. Use this Pragma with caution, as it can corrupt the entire database.