- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
SQLite expressions
In this part of the SQLite tutorial, we will cover SQLite expressions.
Wikipedia defines an expression in a programming language as a combination of values, variables, operators, and functions that are interpreted (evaluated) according to the particular rules of precedence and of association for a particular programming language, which computes and then produces (returns, in a stateful environment) another value. The expression is said to evaluate to that value.
Literal values
A literal value is a constant of some kind. Literal values may be integers, floating point numbers, strings, BLOBs, or NULLs.
sqlite> SELECT 3, 'Wolf', 34.5; 3|Wolf|34.5
Here we return three literals: namely integer, string, and floating point constants.
sqlite> .nullvalue NULL sqlite> SELECT NULL; NULL
The .nullvalue
command tells SQLite to show NULL
values as NULL
. SQLite shows empty strings for NULL
values by default. The NULL
value is a literal too.
sqlite> SELECT x'345eda2348587aeb'; x'345eda2348587aeb' -------------------
BLOB
literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character.
Operators
Operators are used to build expressions. SQL operators are very similar to mathematical operators. SQLite supports unary and binary operators. Binary operators work with two operands, unary work with one. An operator may have one or two operands. An operand is one of the inputs (arguments) of an operator.
SQLite supports five broad categories of operators:
- Arithmetic operators
- Boolean operators
- Relational operators
- Bitwise operators
- Other operators
SQLite supports the following binary operators:
|| * / % + - << >> & | < <= > >= = == != <> IS IN LIKE GLOB BETWEEN AND OR
Operators are arranged according to the precedence. The ||
operator has the highest order of precedence, the OR
operator the lowest.
These are the unary prefix operators:
- + ~ NOT
The unary +
operator is a no-op. It does not do anything. The unary -
operator changes positive values to negative and vice versa.
sqlite> SELECT -(3-44); 41
The result is 41. The other two operators will be discussed later.
Arithmetic operators
Arithmetic operators understood by SQLite are multiplication, division, addition, subtraction, and modulo.
sqlite> SELECT 3*3/9; 1
These are the multiplication and division operators that we know from mathematics.
sqlite> SELECT 3 + 4 - 1 + 5; 11
We show the addition and subtraction operators.
sqlite> SELECT 11 % 3; 2
The %
operator is called the modulo operator. It finds the remainder of division of one number by another. The 11 % 3
, 11 modulo 3 is 2, because 3 goes into 11 three times with a remainder of 2.
Boolean operators
With boolean operators we perform logical operations. SQLite has three boolean operators: AND
, OR
, and NOT
. Boolean operators return true or false. In SQLite, 1 is true, 0 is false.
The AND
operator evaluates to true if both operands are true.
sqlite> SELECT 0 AND 0, 0 AND 1, 1 AND 0, 1 AND 1; 0|0|0|1
The first three operations evaluate to false, the last one to true.
sqlite> SELECT 3=3 AND 4=4; 1
Both operands are true, so the result is true (1).
The OR operator evaluates to true if at least one of the operands is true.
sqlite> SELECT 0 OR 0, 0 OR 1, 1 OR 0, 1 OR 1; 0|1|1|1
The first operation evaluates to false, other operations evaluate to true.
The NOT
operator is a negation operator. It makes true false and false true.
sqlite> SELECT NOT 1, NOT 0; 0|1 sqlite> SELECT NOT (3=3); 0
Relational operators
Relational operators are used to compare values.
Symbol | Meaning |
---|---|
< | strictly less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
= or == | equal to |
!= or <> | not equal to |
These operators always result in a boolean value.
sqlite> SELECT 3*3 == 9, 9 = 9; 1|1
Both =
and ==
are equality operators.
sqlite> SELECT 3 < 4, 3 <> 5, 4 >= 4, 5 != 5; 1|1|1|0
Usage of the relational operators is known from mathematics.
Bitwise operators
Decimal numbers are natural to humans. Binary numbers are native to computers. Binary, octal, decimal, or hexadecimal symbols are only notations of the same number. Bitwise operators work with bits of a binary number. We have binary logical operators and shift operators.
The bitwise and operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 only if both corresponding bits in the operands are 1.
00110 & 00011 = 00010
The first number is a binary notation of 6, the second is 3 and the result is 2.
sqlite> SELECT 6 & 3; 2 sqlite> SELECT 3 & 6; 2
The bitwise or operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 if either of the corresponding bits in the operands is 1.
00110 | 00011 = 00111
The result is 00110 or decimal 7.
sqlite> SELECT 6 | 3; 7
The bitwise shift operators shift bits to the right or left.
number << n : multiply number 2 to the nth power number >> n : divide number by 2 to the nth power
These operators are also called arithmetic shift.
00110 >> 00001 = 00011
We shift each of the bits of the number six to the right. It is equal to dividing the six by 2. The result is 00011 or decimal 3.
sqlite> SELECT 6 >> 1; 3
00110 << 00001 = 01100
We shift each of the bits of the number six to the left. It is equal to multiplying the number six by 2. The result is 01100
or decimal 12.
sqlite> SELECT 6 << 1; 12
The bitwise negation operator changes each 1 to 0 and 0 to 1. It is also called the tilda operator.
sqlite> SELECT ~7; -8 sqlite> SELECT ~-8; 7
The operator inverts all bits of a number, 7. One of the bits also determines whether the number is negative. If we negate all the bits one more time, we get number 7 again.
Other operators
Other SQLite operators include ||, IN, LIKE, GLOB, and BETWEEN.
sqlite> SELECT 'wolf' || 'hound'; wolfhound
The ||
operator is a string concatenation operator. It simply joins strings.
We can use the IN
operator in two cases.
sqlite> SELECT 'Tom' IN ('Tom', 'Frank', 'Jane'); 1
Here we check if the string value 'Tom' is in the list of names that follows the IN
operator. The return value is a boolean value.
For the following examples, we recapitulate what we have in the Cars
table.
sqlite> SELECT * FROM Cars; Id Name Price ---------- ---------- ---------- 1 Audi 52642 2 Mercedes 57127 3 Skoda 9000 4 Volvo 29000 5 Bentley 350000 6 Citroen 21000 7 Hummer 41400 8 Volkswagen 21600
In the second case, the IN
operator allows us to specify multiple values in a WHERE
clause.
sqlite> SELECT * FROM Cars WHERE Name IN ('Audi', 'Hummer'); Id Name Price ---------- ---------- ---------- 1 Audi 52642 7 Hummer 41400
From the Cars
table, we choose cars that are listed after the IN
operator.
The LIKE
operator is used in the WHERE
clause to search for a specified pattern in a column.
sqlite> SELECT * FROM Cars WHERE Name LIKE 'Vol%'; Id Name Price ---------- ---------- ---------- 4 Volvo 29000 8 Volkswagen 21600
Here we select cars whose names begin with 'Vol'. The percent sign (%) matches an arbitrary number of characters (including zero characters).
sqlite> SELECT * FROM Cars WHERE Name LIKE '____'; Id Name Price ---------- ---------- ---------- 1 Audi 52642
An underscore character (_) matches any single character. Here we select a car name that has exactly four characters; there are four underscores.
The GLOB
operator is similar to the LIKE
, but it uses the Unix file globbing syntax for its wildcards. Also, GLOB
is case sensitive, unlike LIKE
.
sqlite> SELECT * FROM Cars WHERE Name GLOB '*en'; Id Name Price ---------- ---------- ---------- 6 Citroen 21000 8 Volkswagen 21600
Here we have cars, whose names end with 'en' characters.
sqlite> SELECT * FROM Cars WHERE Name GLOB '????'; Id Name Price ---------- ---------- ---------- 1 Audi 52642
Here we again select a car name that has exactly four characters.
sqlite> SELECT * FROM Cars WHERE Name GLOB '*EN'; sqlite> SELECT * FROM Cars WHERE Name LIKE '%EN'; Id Name Price ---------- ---------- ---------- 6 Citroen 21000 8 Volkswagen 21600
These two statements demonstrate that LIKE
is case insensitive and GLOB
is case sensitive.
The BETWEEN
operator is equivalent to a pair of comparisons; a BETWEEN b AND c
is equivalent to a>=b AND a<=c
.
sqlite> SELECT * FROM Cars WHERE Price BETWEEN 20000 AND 55000; Id Name Price ---------- ---------- ---------- 1 Audi 52642 4 Volvo 29000 6 Citroen 21000 7 Hummer 41400 8 Volkswagen 21600
In this SQL statement, we have selected cars that cost between 20000 and 55000 units.
Precedence
The rules of operator precedence specify which operators are evaluated first. The precedence level is necessary to avoid ambiguity in expressions.
What is the outcome of the following expression, 28 or 40?
3 + 5 * 5
Like in mathematics, the multiplication operator has a higher precedence than addition operator. So the outcome is 28.
(3 + 5) * 5
To change the order of evaluation, we can use parentheses. Expressions inside parentheses are always evaluated first.
sqlite> SELECT 3+5*5, (3+5)*5; 28|40
The first expression evaluates to 28 because multiplication operator has a higher precedence than addition. In the second example, we have used parentheses to change the order of evaluation. So the second expressions evaluates to 40.
Here we put again the list of operators in SQLite.
unary + - ~ NOT || * / % + - << <> & | < <= > >= = == != <> IS IN LIKE GLOB BETWEEN AND OR
The operators on the same row have the same level of precedence. The precedence grows from bottom to top.
Associativity
Sometimes precedence is insufficient for determining the outcome of an expression. A second set of rules, called rules of associativity, determine the order of evaluation of operators with the same precedence level.
9 / 3 * 3
What is the outcome of this expression, 9 or 1? The multiplication, deletion and the modulo operator are left to right associated. So the expression is evaluated this way: (9 / 3) * 3
and the result is 9.
sqlite> SELECT 9 / 3 * 3; 9
The associativity rule is left to right.
sqlite> SELECT 0 AND 0 OR 1; 1
The associativity rule is again left to right. If it was right to left, the result would be 0.
Arithmetic, boolean, relational, and bitwise operators are all left to right associated.
In this part of the SQLite tutorial, we have covered the SQLite expressions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论