My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
ExecuteSQL  
SQL syntax and using the Execute SQL Tab of SQLite Manager.
Featured
Updated Jul 30, 2011 by mrinal.k...@gmail.com

Execute SQL Tab

Although SQLite Manager simplifies many database operations, there are still tasks that can only be accomplished by using SQL in the Execute SQL tab such as updating multiple rows at a time or using SELECT across multiple tables.

Once you have entered your sql statement(s) into the Enter SQL text box, you can click on the Run SQL button or press ctrl + F12 or ctrl ; (control semicolon) When multiple sql statements are to be run, each sql statement must be terminated by a semicolon.

Tip: You can shrink or expand the Enter SQL text box by clicking and dragging the horizontal line below the Run SQL button.

Copy or Save Query Results

Output for SELECT queries is displayed in the lower text box of the Execute SQL tab as table rows. A drop down menu to the right of the Run SQL button provides the option of displaying the query result as CSV (comma separated values) or saving the query result as a CSV text file. One or more rows from the query result can also be copied on to the clip board by highlighting the rows to be copied and right clicking to bring up the context menu. This menu provides the option of copying the rows as SQL statements or in CSV format. They can then be pasted into a text file or a spreadsheet program such as MS Excel.

Save Query By Name

SELECT Queries and other SQL statements can be stored in a SQLite database and rerun at a later time from the Execute SQL tab of SQLite Manager. To enable this functionality, go to the Tools menu in the Menu bar and check the Use Table for Extension Data item.

To save a SQL statement, click on the Save Query icon in the Execute SQL tab, which will prompt you to enter a query name.

To retrieve the SQL query, select it by name from the drop down menu below the Enter SQL text box. The text of the statement will be written to the Enter SQL text box where it can be modified if necessary before being run.

To delete a saved query statement from the menu run the following sql statement containing the query name in the Enter SQL text box.

DELETE FROM __sm_ext_mgmt WHERE type = "NamedQuery: query_name"

SQL Quick Reference

CREATE TABLE

CREATE TABLE table_name(fieldname_1 data_type, fieldname_2 data_type, fieldname_3 data_type);

Create Tables with Foreign Keys

One to Many

CREATE TABLE child_table_name (field_1 INTEGER PRIMARY KEY, field_2 TEXT, foreign_key_field INTEGER , FOREIGN KEY(foreign_key_field) REFERENCES parent_table_name(parent_key_field));

One to One relationship

CREATE TABLE child_table_name (field_1 INTEGER PRIMARY KEY, field_2 TEXT, foreign_key_field INTEGER UNIQUE , FOREIGN KEY(foreign_key_field) REFERENCES parent_table_name (parent_key_field));

FOREIGN KEY PHRASE with CASCADE

FOREIGN KEY(foreign_key_field) REFERENCES parent_table_name(parent_key_field) ON DELETE CASCADE ON UPDATE CASCADE );

CREATE TABLE from SQL Query Results

CREATE TABLE new_table_name AS SELECT select statement

INSERT Records to a TABLE

INSERT INTO table_name (field_1, field_2, field_3) VALUES ("value a", "value b",0.00 )

UPDATE

UPDATE table_name SET column_name= value WHERE criteria;

Select Queries

SELECT
DISTINCT Exclude duplicate records for fields selected.
CASE WHEN expression Conditional expression
THEN expression
ELSE expression END
FROMtable_name Multiple table names are separated by commas.
WHERE Row level filtering
expressionANDexpression
expression ORexpression
IN Comma delimited list enclosed in parenthesis
NOT IN Comma delimited list enclosed in parenthesis
BETWEEN Select records within the specified numeric range
NOT BETWEEN Select records outside of the specified numeric range
LIKE String with wildcard (%) enclosed in parenthesis
NOT LIKE String with wildcard (%) enclosed in parenthesis
GROUP BY
HAVING
ORDER BY Sorting of the output using a comma delimited list of column names
LIMIT Limit the number of rows returned

String Manipulation

FunctionExplanationSyntax
------------
LENGTH( )Returns the number of characters in the string length(fieldname or expression)
LTRIM( )Trims listed characters from the beginning of a string, if the only argument that is provided is a field name or expression then the function will trim only white space.LTRIM(fieldname or expression,' characters')
RTRIM( )Trims listed characters from the end of a string, if the only argument that is provided is a field name or expression then the function will trim only white space.RTRIM(fieldname or expression,'characters')
TRIM ( )Trims listed characters from both ends of a string, if the only argument that is provided is a field name or expression then the function will trim only white space.TRIM(fieldname or expression ,'characters')
QUOTE( )returns field enclosed in single quotesQUOTE(fieldname or expression)
SUBSTR( )Extracts part of a string.SUBSTR(field_name, start_location, substring_length )
REPLACE( )Searches column or field for string specified in the second argument and replaces it with the string in the third argument. REPLACE(field_name,'old_string','new_string')
||Concatenate strings string_one||string_two

Expressions

SymbolMeaningExample
*Multiplyfield_name * 0.05
+Addfield_name + 1
-Subtractfield_name - 0.5
/Dividefield_1 / field_2
=equal tofield_1 = field_2
==equal tofield_1 == 1
< less than field_b < 100
<=less than or equal tofield_a <= 99
>Greater thanfield_name > 2
>=Greater than or equal tofield_name >= 15.142
<>Not equal tofield_name <> 0
!=Not equal tofield_a != field_b

Aggregate Functions

FunctionExplanationSyntax
AVG( )Averages the value of the column or groupingAVG( fieldname or expression )
COUNT( )Returns the number of rows in the column or groupingCOUNT( fieldname or expression )
MAX( )Returns the highest value found in the column or grouping MAX( fieldname or expression )
MIN( )Returns the lowest value found in the column or grouping MIN( fieldname or expression )
SUM( )Total of the values in the column or grouping added togetherSUM( fieldname or expression )
TOTAL( )Specific to SQLITE SQL, always returns the floating point sum of the values a column or groupingTOTAL( fieldname or expression )


Sign in to add a comment
Powered by Google Project Hosting