Sqlite Studio Manual
Sqlite Studio Manual
Version 2.0.6
Contents
1) Installation and running 1.1) Supported platforms 1.2) Installing from sources 1.3) Installing from binaries 2) Basics 2.1) Interface introduction 2.1.a) First start 2.1.b) Main window 2.1.c) Editing data 2.2) Managing databases 2.3) Editing tables structure 2.4) Exporting data 2.5) SQL editor 2.5.a) Writing and executing SQL query 2.5.b) Executing SQL directly from file 2.6) Indexes, triggers, views 2.7) Configuration dialog 2.7.a) Miscellaneous tab 3) Advanced topics 3.1) Built-in and custom SQL functions 3.1.a) Built-in SQLite functions 3.1.b) Built-in SQLiteStudio functions 3.1.c) Custom SQL functions dialog 3.2) Populating tables 3.3) Converting SQLite database version 3.4) Schema importing 4) Useful utilities and features 4.1) Create similar table 4.2) Select duplicated rows for new unique index 4.3) User-friendly bugs reporting 4.4) Create view from query 4.5) Good to know 4.5.a) Modifying table schema with lots of data in it 4.5.b) Triggers support for SQLite2 and SQLite3 older than 3.6.19 5) Plugins 5.1) Populating plugins 5.1.a) Built-in plugins description 5.1.b) Writing populating plugin 5.2) SQL formatting plugins 5.2.a) Built-in plugins description 5.2.b) Writing formatting plugin 5.3) Exporting plugins 5.3.a) Built-in plugins description 5.3.b) Writing exporting plugin 2 4 4 5 6 7 7 7 7 8 10 10 11 12 12 13 14 15 15 17 17 17 17 18 20 21 22 23 23 24 25 26 27 27 27 28 29 29 30 32 32 32 33 33 34
Linux Linux Windows 9x/Me Windows NT/XP/Vista/7 Windows NT/XP/Vista/7 Solaris Solaris Solaris FreeBSD FreeBSD OpenBSD NetBSD MacOS X MacOS X MacOS AIX QNX HP-UX
ix86 (32bit) ix86_64 (64bit) ix86 (32bit) ix86 (32bit) ix86_64 (64bit) ix86 (32bit) SPARC (32bit) SPARC (64bit) ix86 (32bit) ix86_64 (64bit) all all ix86_64 (64bit) PPC PPC IBM POWER all all
[**] [*] [*] [*]
AmigaOS PPC [*] - That OS is not supported, but some older versions (1.0.x) of SQLiteStudio might work.
[**] - Binaries are 32bit, but Windows 64bit supports 32bit binaries just fine.
Application has been tested to work on this platform. Application wasn't tested on this platform, but it should work. Application wasn't tested on this platform and it's hard to say if it works. This platform is unsupported and application won't work on it.
If you have some operating system/platform not mentioned in the table above, you can always try to run SQLiteStudio from sources. All you need to collect some dependencies mentioned in chapter 1.2. Be aware, that it might not support your platform even running from sources.
On Linux/Unix/MacOSX Delete ".sqlitestudio" directory in home directory of each user that run application at least once.
Linux/Unix/MacOSX Installation for Unix systems is pretty much like for Windows systems. The only difference is that you might need to set execution permission for downloaded file with command: chmod +x sqlitestudio-<VERSION>.bin Change "<VERSION>" to fit the file name you've just downloaded, of course. For Unix-like systems it is good idea to rename file to "sqlitestudio" and move it to directory included in environment variable PATH, so it can be run from console by writting simply: sqlitestudio If you want to uninstall application - just delete the file. That's all. If you want to also delete configuration files, you have to delete ".sqlitestudio" directory in home directory of each user that run application at least once.
2) Basics
Here's few informations for complete newbies: SQLiteStudio is NOT the same as SQLite. SQLite is database, SQLiteStudio is application to manage such databases. SQLite is relational database with SQL support. If you don't know how to use/manage SQLite databases, read about relational databases and SQL first. SQLite database has no client-server model. Here databases are files, so you cannot connect to some remote host on some strange port, log in and manage database. You need to select local file which is (or is going to be) database.
Tasks order on Taskbar can be arranged manually by dragging and dropping tasks. Active tasks are saved on application exit and restored after next start.
There are two main types of tasks, which are represented as MDI windows: Table window, SQL editor window.
2.1.c) Editing data The data can be edited in either Table Window, or in SQL editor window. Table Window provides data editing with Grid View or Form View, while SQL editor allows same ways as Table Window and additionally the generic way by SQL queries.
the database. They are in pending state and wait for Commit or Rollback to be pressed in a toolbar above. You can tell if there are any pending commits by state of Commit/Rollback buttons, or by checking if there are any cells marked with blue border in Grid View. Pending commits can be disabled in Configuration window in Miscellaneous tab (see 2.7.a), so any changes will be sent to database immediately. If Commit fails because of constraint violation or some similar reason, then the proper message is displayed and cells/rows that could not be committed are marker with red border. The Commit/Rollback buttons in Form View commits or rolls back only single, current row, while same buttons in Grid View applies to all pending operations (deletes, updates or inserts). You can always commit/rollback only selected cells from Grid View use adequate entry from context menu of Grid View (right mouse click). Table window data view provides also filter entry in toolbar. You can type any token (a word, a sentence, a number, etc) and press Enter key or Apply data filter toolbar button to filter table data view to display only rows containing given token. To disable data filtering use Clear data filter or manually delete contents of filter entry and apply it.
Known issue:
Editing SQL query results from multiple SELECT statements joined with compound operator (see SQLite manual) won't work correctly. This is because of limited support of handling results in SQLite extension for Tcl and at this moment SQLite maintainers don't plan to fix it anytime soon.
11
Since SQLiteStudio supports SQLite in version 2 and 3, you can pick (while creating new database, using not existing file) which version of database you'd like to create. If you see only SQLite3 in dropdown list, then it's probably because your binary distribution doesn't support SQLite2 (see 1.1), or you're runnign source distribution and you don't have sqlite extension for Tcl in version 2. If SQLiteStudio supports both versions of SQLite on your computer, then you can convert database from one version to another. Use Convert database from databases tree context menu (right mouse click), but it has its consequences (see 3.3).
While being in Table Structure dialog, you can add/edit/delete columns by buttons on the right, or by pressing keyboard keys Insert/Space/Delete. Note, that for editing or deleting column you have to select which column you mean (if you prefer using keyboard, use tab key and arrows to navigate through the dialog). You can also open Column Edition dialog by double-clicking on it. Columns order can be reorganized by drag&drop. 12
13
Syntax highlighting works using colors and fonts defined in Configuration window. Table names in query that actually exists in currently chosen database are marked with individual color. You can quickly open such tables (Table window) by pressing Control key (the names will go underline) and clicking on the name. Error checking is invoked each time after users stops typing for a short while. If any error is detected, then it's marked with color defined in Configuration window. Even error mark starts at some position and ends at the end of statement, the actual error occurs at the beginning of mark. The rest is just not understandable by SQLiteStudio, until the error is fixed. Note, that error checking is based on syntax definition included in SQLiteStudio sources, so it might contain some wrong definitions, or can go out of date. In that case SQLiteStudio might mark error even query is valid. In that case you can ignore it and execute query anyway. Syntax completion is a small helper window that pops up on demand (Control-Space shortcut, or other configured), or after user typed dot character and waited a short while. The window contains list of proposed values that would fit at current caret position. They are sorted in order, that values that user might want the most are placed at the beginning. 14
Each entry in list contains icon on the left to identify type of proposed value: column table index trigger view database SQL function keyword -
Syntax completion is based on syntax definition included in SQLiteStudio, just like error checking does, so it might get out of date. This is why you should always update SQLiteStudio to newest version. Transparent database attaching lets user to type queries that uses multiple databases without worrying about attaching any of them they will be attached automatically and transparently before query is executed and will be detached right after query execution has finished. To reference to other database in query, use its name as displayed in databases tree on the left. Code formatting organizes code so it's more human-readable. It works using plugins engine (see 5.2.a), so the results of this operation depends on chosen formatting plugin (in Configuration) and options defined for the plugin.
2.5.b) Executing SQL directly from file If you have some huge SQL file with lots of SQL statements, it might slowdown SQL Editor window significantly. In that case use Executing from file feature. There are two ways to execute SQL from file: Open SQL Editor window and click on Execute SQL from file from toolbar, Right-click on open database in databases tree and pick Execute SQL from file.
If SQL was executed sucessfly you'll be informed about it. If there was an error, the error message will appear with detailed information.
15
View dialog contains only database to create it in and body of the view. The body field supports SQL code completion feature.
16
2.7.a) Miscellaneous tab Following options on Miscellaneous tab might need explanation: Auto-commit edition in data grid view if this is enabled, then pending commits (described in 2.1.c) are disabled and any changes to data are committed immediately, just like it used to be in SQLiteStudio 1.x. Restore session after next start recreates tasks (and their MDI windows) on next start that was open at last application exit. Show line numbers in SQL editor if disabled, then line number/ROWID column in Grid View will be hidden. Show SQLite system tables and indexes in databases tree if enabled, then system tables (such as sqlite_master) and indexes (such as autoincrement index in SQLite2) will be displayed in database tree on the left side of window. Sort custom SQL functions list by name enables or disables sorting by name for list of SQL functions in Custom SQL functions dialog (see 3.1.a). 17
Ask if clipboard contents should be cut this defines behavior of application when user tries to paste some contents to Grid View, especially contents copied from other Grid View or spreadsheet application (such as MS Excel or OpenOffice Calc), since they will most likely contain data for multiple rows and/or columns. If this option is disabled, then pasting 2columns/2-rows data into last column in last row of Grid View will paste just first value from clipboard data, ignoring rest of data. If the option is enabled, then SQLiteStudio will ask user first, if he want's to skip rest of data if not, then no data will be pasted at all. Results per page in Table Window and Editor Results defines number of rows at single page of Grid View. The less, the faster Grid View will behave, but more pages to switch across for huge amount of results. History entries limit defines how many queries will be remembered in history, so user can go back to them and reexecute. The less, the faster new SQL Editor windows will open, but user loses amount of stored queries. Web browser this option is available only for Unix platforms, excluding MacOSX (Windows handles default web browser by itself, same as MacOSX). It defines which web browser should be used. It's exact command to be executed. Target URLs will be passed as single argument to the command.
18
3) Advanced topics
3.1) Built-in and custom SQL functions
SQL functions are the one used in SQL query expression, like: SELECT max(id) FROM table1; Here max() is the SQL function. There are many SQL functions built-in SQLite. Additionally SQLiteStudio implements some functions by itself and finally, provides dialog window to write custom SQL functions defined by user.
3.1.a) Built-in SQLite functions There is a wide list of SQL functions built-in SQLite. Full list with description is available in SQLite documentation here, here and here. 3.1.b) Built-in SQLiteStudio functions SQLiteStudio provides additional functions that can be used in SQL queries, but they are available only when managing database from SQLiteStudio, they won't be available in other application that connects to the database, unless the other application also implements such functions. There are following additional functions: tcl('Tcl code') - evaluates given Tcl code in separated Tcl interpreter and returns result of evaluation. sqlfile('file') - reads contents of given file and executes it as sequence of 0 or more SQL statements on current database connection. It returns 1 on success and 0 on error. base64_encode(arg) encodes given argument with Base64 algorithm and returns it. base64_decode(arg) decodes argument from Base64 algorityhm and returns it. md5(arg) calculates MD5 digest from given argument and returns it. md4(arg) calculates MD4 digest from given argument and returns it. sha1(arg) calculates SHA1 digest from given argument and returns it. sha256(arg) calculates SHA256 digest from given argument and returns it. crc16(arg) calculates checksum value using CRC-16 algorithm and returns it. crc32(arg) calculates checksum value using CRC-32 algorithm and returns it. uuencode_encode(arg) encodes given argument with Uuencoding algorithm and 19
returns it. uuencode_decode(arg) decodes given argument from Uuencoding algorithm and returns it. yencode_encode(arg) encodes given argument with yEnc algorithm and returns it. yencode_decode(arg) decodes given argument from yEnc algorithm and returns it. file('file') reads contents of given file and returns them. If file doesn't exists or cannot be read, then empty string is returned.
3.1.c) Custom SQL functions dialog SQLiteStudio provides support for writing user custom SQL functions on the fly. They work very much like built-in functions, except their body is defined by user in Custom SQL Functions dialog (available from toolbar). Remember, that custom functions won't work in other applications connecting to the database, unless they (applications) implement same functions by them-self. Body and arguments: Currently the body can be implemented either in SQL or in Tcl. All parameters passed to function call will replace any occurrences of placeholders $0, $1, $2, and so on. Results: Results are always returned as single cell (single column, single row). In case of SQL function, multiple results will be concatenated into single value using whitespace as separator. NULL result will be transformed into empty string. Examples: Example function implemented in SQL, lets call it getProductId and assume that it takes one argument, a product name. The body is: SELECT productId FROM products WHERE productName = '$0'; Other example. This time function is implemented in Tcl. Lets call it random and takes two arguments minimum and maximum values expected. The body is: set min $0 20
21
22
23
Just like in case of converting database, the importing requires SQLite2 support in your SQLiteStudio installation if you want to import from SQLite2 database.
24
25
26
27
28
4.5.b) Triggers support for SQLite2 and SQLite3 older than 3.6.19 SQLiteStudio 1.1.x emulated foreign keys using triggers. This has been removed in 2.0, because of 2 reasons: 1. It caused many problems. Didn't work like it was expected to. 2. SQLite 3.6.19 introduced full support for foreign keys and it becomes standard now. If you would like to implement foreign key for SQLite2, then you can do it manually with triggers. SQLiteStudio won't delete them during modifications on tables it always recreates any triggers for table that was modified.
29
5) Plugins
New plugins (a single tcl file with plugin class implementation) can be placed in CFG_DIR/plugins/ directory. The CFG_DIR directory depends on operating system and it's described in installation from sources. There's also command line option --plugins which you can use to specify additional plugins directory on demand. Writing plugins requires Tcl programing language knowledge.
30
Also minimal and maximal lenght of generated values is configurable. RANDOM NUMBER Fills column with random number generated in defined range. Also constant prefix and suffix to the generated value is possible. SEQUENCE Fills column with numbers taken in order from sequence. User can choose value to start from. Also constant prefix and suffix to the generated value is possible.
31
5.1.b) Writing populating plugin To write populating plugin the new class have to inherit from class PopulatingPlugin. Methods that you need to implement are: getName {} - Has to return symbolic name of table population engine. Must be unique for all populating plugins. It's good idea for it to be uppercase name. configurable {} - Has to return boolean value defining if implemented populating engine supports configuration. If true, then methods createConfigUI and applyConfig implementations should not be empty. createConfigUI {path} - The "path" argument is Tk widget path to frame where whole configuration of plugin should be placed. Implementation should create configuration widget in frame pointed by given "path". Can do nothing if configurable returns false. applyConfig {path} - Tk widget path to frame where configuration is placed. Implementation should extract necessary information from configuration widget and store it in local variables, so they can be used later, while populating by method nextValue. Configuration widget in path will be destroyed just after this method call is completed. Can do nothing if configurable returns false. nextValue {} - Implementation should generate next value to fill table cell and return it. The method called in a loop, for each populated table row. Example populating plugin:
class ConstantPopulatePlugin { inherit PopulatingPlugin constructor {} {} public { variable checkState these variables are used to cache configuration variable checkStateOrig proc getName {} proc configurable {} method createConfigUI {path} method applyConfig {path} method nextValue {} } }
body ConstantPopulatePlugin::constructor {} { set checkState(const) "" array set checkStateOrig [array get checkState] } body ConstantPopulatePlugin::getName {} { return "CONSTANT" - this name will appear in populating dialog, has to be unique }
32
body ConstantPopulatePlugin::configurable {} { return true - this is how we say that the plugin is configurable }
This method gets "path" and just build configuration GUI in it:
body ConstantPopulatePlugin::createConfigUI {path} { array set checkState [array get checkStateOrig] pack [ttk::frame $path.start] -side top -fill x -pady 2 ttk::label $path.start.l -text [mc {Value to use:}] -justify left ttk::entry $path.start.e -textvariable [scope checkState](const) pack $path.start.l -side top -fill x -expand 1 pack $path.start.e -side top -fill x -expand 1 focus -force $path.start.e } Since configuration entry with constant value is bind with checkState array, we can just read from this
body ConstantPopulatePlugin::applyConfig {path} { array set checkStateOrig [array get checkState] } body ConstantPopulatePlugin::nextValue {} { return $checkStateOrig(const) - this plugin always gives same, configured value }
33
5.2.a) Built-in plugins description There are 2 built-in formatting plugins: BASIC This is very simple formatting plugin with only few configuration options. It supports keyword uppercasing and few other minor features. It doesn't depend on SQL parser built in SQLiteStudio, so if for some reason SQL parser fails for some of your queries, then this formatter will work anyway. ENTERPRISE It's very advanced plugin. It lets user to decide where would he like to have white-spaces, line breaks, how to indent code, and more... It has enhanced configuration window with live preview of options currently selected. It depends on SQL parser built in SQLiteStudio this is why it always know where and how to format code, but it's more vulnerable for SQL parser syntax incompatibilities which actually should not happened very often. 5.2.b) Writing formatting plugin Class to inherit to implement SQL formatting plugin is SqlFormattingPlugin. Methods that you need to implement are: getName {} - Has to return symbolic name of table population engine. Must be unique for all populating plugins. It's good idea for it to be uppercase name. configurable {} - Has to return boolean value defining if implemented populating engine supports configuration. If true, then methods createConfigUI and applyConfig implementations should not be empty. createConfigUI {path} - The "path" argument is Tk widget path to frame where whole configuration of plugin should be placed. Implementation should create configuration widget in frame pointed by given "path". Can do nothing if configurable returns false. applyConfig {path} - Tk widget path to frame where configuration is placed. Implementation should extract necessary informations from configuration widget and store it in local variables, so they can be used later, while populating by method nextValue. Configuration widget in path will be destroyed just after this method call is completed. Can do 34 BASIC ENTERPRISE
nothing if configurable returns false. formatSql {tokenizedQuery originalQuery {db ""}} - this is the core method. It takes SQL query to format and should return formatted query. OriginalQuery is SQL query in original, untouched form. TokenizedQuery is query processed by lexer and it's a list of 4-element tokens. Tokens are described more precisely below. The optional argument db is contextual database that formatting was called for. You can check if it's not empty and apply some extra formatting option when you know what database it is. Tokens passed to formatSql have 4 elements: type, value, begin index and end index. They represent query split in parts with type for each part and character index for begin and end of the token. Possible token types are: KEYWORD, OPERATOR, STRING, INTEGER, FLOAT, PAR_LEFT, PAR_RIGHT, OTHER, COMMENT.
5.3.a) Built-in plugins description There are 5 built-in exporting plugins: CSV It supports tables data and query results only. Stores output in standard CSV format. Header row can be 35 CSV PLAIN HTML XML SQL
enabled in plugin configuration. PLAIN It's plain text output plugin. It exports data in similar way as seen in Plain text tab in SQL Editor window. Just like CSV plugin, it supports only tables data and query results. Width of columns can be configured. HTML Exports data as HTML document. Supports configuration of header, row order number and column data type. XML Exports data as XML document. The XSD for the output XML can be generated as configuration option. SQL Exports database as DDLs, table data as set of INSERT statements. Query results are also exported as set of INSERT statements.
5.3.b) Writing exporting plugin Class to inherit to implement exporting plugin is ExportPlugin. Methods that you need to implement are: getName {} - Has to return symbolic name of table population engine. Must be unique for all populating plugins. It's good idea for it to be uppercase name. configurable {} - Has to return boolean value defining if implemented populating engine supports configuration. If true, then methods createConfigUI and applyConfig implementations should not be empty. createConfigUI {path} - The "path" argument is Tk widget path to frame where whole configuration of plugin should be placed. Implementation should create configuration widget in frame pointed by given "path". Can do nothing if configurable returns false. applyConfig {path} - Tk widget path to frame where configuration is placed. Implementation should extract necessary information from configuration widget and store it in local variables, so they can be used later, while populating by method nextValue. Configuration widget in path will be destroyed just after this method call is completed. Can do nothing if configurable returns false. validateConfig {context} - It's called just before any export* method is called. It allows to validate if all configured parameters (if any) are valid for exporting in given context. If there is any invalid parameter, then this method should call error command - it will be 36
caught and displayed in error dialog. Context is DATABASE, TABLE, or QUERY. Last one is for exporting SQL query results. exportResults {columns rows} - This method gets SQL query results (from SQL editor window) and should convert them to format that is implemented by the plugin, then return them. columns is a list of columns in table. Each element in list is pair of column name (which includes 'table_name.' prefix) and data type (VARCHAR, DATE, INTEGER, TEXT, etc). rows is a table data as list of rows. Each row is list of values for each column and each value is pair of the value as Tcl sees it and second is boolean indicating if it's <code>null</code> (<code>true</code>) or it's not <code>null</code>. exportTable {name columns rows ddl} - This method gets table columns and data and should convert them to format that is implemented by the plugin, then returned. name is a name of the table to export. columns is a list of columns in table. Each element in list is sublist of: column name (without 'table_name.' prefix), data type (VARCHAR, DATE, INTEGER, TEXT, etc), primary key boolean (true if column is primary key), not null boolean (true if column is not null) and default value of column. Default value of column is pair of the value and isNull boolean. rows is a table data as list of rows. Each row is list of values for each column and each value is pair of the value as Tcl sees it and second is boolean indicating if it's null (true) or it's not null. ddl is a full DDL of table. exportIndex {name table columns unique ddl} - This method gets index specification and should convert it to format that is implemented by the plugin, then returned. name is a name of the index to export. table is a name of the table that index is created for. columns are columns of the table that index is created for. Each column is sublist of 3 elements: column name, collation, sorting order. Collation can be NOCASE, BINARY, some custom one, or empty. Sorting order can be ASC, DESC or empty. unique is a boolean value indicating if index is of UNIQUE type. ddl is a full DDL of index. exportTrigger {name table when event condition code ddl} - This method gets trigger specification and should convert it to format that is implemented by the plugin, then returned. name is a name of the trigger to export.
37
table is a name of the table or view that trigger is invoked by. when is BEFORE, AFTER, or INSTEAD OF. event DELETE, INSERT, UPDATE, or UPDATE OF columns-list. condition is a condition for WHEN statement. code is a body of the trigger. ddl is a full DDL of the trigger. exportView {name code ddl} - This method gets view specification and should convert it to format that is implemented by the plugin, then returned. name is a name of the view to export. code is a body of the view. ddl is a full DDL of view.
38