Skip to content

Executing SQL scripts

To run SQL against the selected database:

  1. Go to the SQL tab and type your SQL in the upper edit window — for example:

    SELECT Company, City, Country
    FROM customer
    WHERE Country = 'US'
    ORDER BY Company;
    
  2. Press F5, or choose SQL → Execute.

If the statement is a SELECT, its result set appears below — in the grid or as text, depending on your settings.

The SQL tab The SQL tab

Running several statements

A script can contain several statements separated by semicolons, and SQLite Expert runs them all in order. Only the result of the last statement is displayed (and only if it is a SELECT), so a script that changes data and then reads it back will show you the final result.

Running only part of a script

To run just one statement (or a few) out of a longer script, select the text you want and press F5. Only the highlighted SQL is executed.

When a statement fails

SQLite reports what is wrong with a statement, and usually also where. When it does, SQLite Expert puts the caret on the offending word in the editor and selects it, so you can go straight to it after closing the error message — useful when the message on its own is hard to place, such as near ".": syntax error in a script with a hundred dots in it.

The message gives the same position in words, as a line and column counted from the start of the failing statement — not from the start of the script — along with the line it is on:

near ".": syntax error

Error at line 12, column 26 of the statement:
    ...JOIN [main]..[orders] ON...

Not every error names a word. no such table: orders is about the statement as a whole, so there is nothing to point at and nothing is selected.

Note

The position comes from the SQLite library itself and needs version 3.38.0 or later. If you have selected an older library, the error message still appears but without the position, and nothing is highlighted.

Tip

Wrap a batch of changes in a transaction (BEGIN … COMMIT) so they all succeed or fail together — and so you can roll back if the result isn't what you expected.

See also: Explaining a query plan · Using parameters in SQL scripts · Cancelling the execution of a long-running SQL script · Code Completion