- 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 functions
In this part of the SQLite tutorial, we will cover SQLite built-in functions. There are three types of functions in SQLite database: core, aggregate, and date & time functions.
We will cover some functions from each group of SQLite functions.
Core functions
In this group we have various functions. Some are numerical functions, some work with text. Others do some very specific things.
sqlite> SELECT sqlite_version() AS 'SQLite Version'; SQLite Version -------------- 3.7.15.1
The sqlite_version()
function returns the version of the SQLite library.
sqlite> SELECT random() AS Random; Random ------------------- 1056892254869386643
The random()
function returns a pseudo-random integer between -9223372036854775808 and +9223372036854775807.
sqlite> SELECT abs(11), abs(-5), abs(0), abs(NULL); abs(11) abs(-5) abs(0) abs(NULL) ------------------ ----------- ---------- ---------- 11 5 0 NULL
The abs()
function returns the absolute value of a numeric argument.
sqlite> SELECT max(Price), min(Price) FROM Cars; max(Price) min(Price) ---------- ---------- 350000 9000
In our example, the max()
and min()
functions return the most and the least expensive cars from the Cars
table.
sqlite> .width 18 sqlite> SELECT upper(Name) AS 'Names in capitals' FROM Friends; Names in capitals ------------------ JANE THOMAS FRANK ELISABETH MARY LUCY JACK
The upper()
function converts characters into upper-case letters.
sqlite> SELECT lower(Name) AS 'Names in lowercase' FROM Friends ...> WHERE Id IN (1, 2, 3); Names in lowercase ------------------ jane thomas frank
With the lower()
function we change the names of first three rows into lower-case letters.
sqlite> SELECT length('ZetCode'); length('ZetCode') ------------------ 7
The length()
function returns the length of a string.
sqlite> SELECT total_changes() AS 'Total changes'; Total changes ------------- 3
The total_changes()
function returns the number of row changes caused by INSERT
, UPDATE
, or DELETE
statements since the current database connection was opened. In the current database connection, we have done three INSERT
statements, so total changes is equal to three.
sqlite> .width 5 sqlite> SELECT sqlite_compileoption_used('SQLITE_DEFAULT_FOREIGN_KEYS') AS 'FK'; FK ----- 0
The sqlite_compileoption_used()
function returns a boolean value, depending on whether or not that option was used during the build. In our case we check if the FOREIGN KEY
constraint is enforced by default. The function returns 0, which means that the constraint is not enforced by default. We use the PRAGMA
statement to change it. (PRAGMA foreign_keys = 1;)
sqlite> SELECT typeof(12), typeof('ZetCode'), typeof(33.2), typeof(NULL), ...> typeof(x'345edb'); typeof(12) typeof('ZetCode') typeof(33.2) typeof(NULL) typeof(x'345edb') ------------ ------------------ ------------ ------------ ----------------- integer text real null blob
The typeof()
function returns the data type of the argument.
Aggregate functions
With aggregate functions, we get some statistical data. Aggregate functions that take a single argument can be preceded by the DISTINCT
keyword. In such cases, duplicate elements are filtered before being passed into the aggregate function.
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
Notice that there are no duplicate records.
sqlite> SELECT count(*) AS '# of cars' FROM Cars; # of cars ---------- 8
The count()
function returns the number of rows in the table—there are eight cars.
In the Orders
table, we do have duplicate records of customers.
sqlite> SELECT * FROM Orders; Id OrderPrice Customer ---------- ---------- ---------- 1 1200 Williamson 2 200 Robertson 3 40 Robertson 4 1640 Smith 5 100 Robertson 6 50 Williamson 7 150 Smith 8 250 Smith 9 840 Brown 10 440 Black 11 20 Brown
Logically, each customer can make multiple orders. How do we count the number of orders and how do we count the number of customers?
sqlite> SELECT count(Customer) AS '# of orders' FROM Orders; # of orders ----------- 11
This SQL statement returns the number of orders. To calculate the number of unique customers, we have to utilise the DISTINCT
clause.
sqlite> SELECT count(DISTINCT Customer) AS '# of customers' FROM Orders; # of customers -------------- 5
We have 5 customers in our Orders
table. They placed 11 orders.
Next we are going to demonstrate the difference between the count(*)
and count(ColumnName)
functions. The difference is the way they handle NULL
values.
sqlite> .nullvalue NULL
First, we change how sqlite3
shows NULL
values. By default, the NULL
value is shown as an empty string.
sqlite> CREATE TABLE TESTING(Id INTEGER); sqlite> INSERT INTO Testing VALUES (1), (2), (3), (NULL), (NULL);
Here we create table Testing
with 3 numerical and 2 NULL
values.
sqlite> SELECT last_insert_rowid(); 5
The last_insert_rowid()
function returns the Id of the last inserted row.
sqlite> SELECT count(*) AS '# of rows' FROM Testing; # of rows ---------- 5
The count(*)
returns the number of rows in the table. It takes NULL values into account.
sqlite> SELECT count(Id) AS '# of non NULL values' FROM Testing; # of non NULL values -------------------- 3
The count(Id)
counts only non NULL
values.
sqlite> SELECT avg(Price) AS 'Average price' FROM Cars; Average price ------------- 72721.125
The avg()
function returns the average value of all non NULL
records. In our example, we show the average price of the car in the Cars
table.
Finally, we mention the sum()
function. It sums all non NULL
values.
sqlite> SELECT sum(OrderPrice) AS Sum FROM Orders; Sum -------- 4930
Here we count how many orders our customers placed.
Date and time funcions
SQLite has functions for working with date and time. These functions take various time strings, modifiers, and formats.
sqlite> .header OFF sqlite> SELECT date('now'); 2014-11-17
The date()
function with the now
string returns the current date.
sqlite> SELECT datetime('now'); 2014-11-17 21:41:56
The datetime()
function returns the current date and time.
sqlite> SELECT time('now'); 21:42:35
The time()
function gives the current time.
sqlite> SELECT time(), time('now'); 21:42:59 21:42:59 sqlite> SELECT date(), date('now'); 2014-11-17 2014-11-17
The now
string can be omitted.
The first parameter of the date()
, time()
, and datetime()
functions is the time string. It can be followed by one or more modifiers.
sqlite> SELECT date('now', '2 months'); 2015-01-17
In this example, '2 months' is a modifier. It adds two months to the current date. So the function returns the date two months from today.
sqlite> SELECT date('now', '-55 days'); 2014-09-23
Negative modifiers can be also used. In this example, we extract 55 days from today.
sqlite> SELECT date('now', 'start of year'); 2014-01-01
Using the start of year
modifier, we get the date of the start of the year, e.g. January 1st.
sqlite> SELECT datetime('now', 'start of day'); 2014-11-17 00:00:00
With the help of the start of day
modifier, we get the beginning of the current day.
sqlite> SELECT date('now', 'weekday 6'); 2014-11-22
The weekday
modifier advances to the next date, where Sunday is 0, Monday 1, ..., Saturday 6. In this example, we get the date of the nearest Saturday.
The modifiers can be combined.
sqlite> SELECT date('now', 'start of year', '10 months', 'weekday 4'); 2014-11-06
This SQL statement returns the first Thursday of the November for the current year. In this example, we used three modifiers: start of year
, +x months
and weekday x
. The now
time string gives the current date. The start of year
shifts the date backwards to the beginning of the year. The 10 months
adds 10 months to the current month (January). Finally, the weekday 4
modifier advances the date forward to the first Thursday.
The strftime()
function returns the date and time formatted according to the format string specified as the first argument. The second parameter is the time string. It can be followed by one or more modifiers.
sqlite> SELECT strftime('%d-%m-%Y'); 17-11-2014
We can use the strftime()
function to return a date in a different format.
sqlite> SELECT 'Current day: ' || strftime('%d'); Current day: 17
This SQL statement returns the current day of the month. We used the strftime()
function.
sqlite> SELECT 'Days to XMas: ' || (strftime('%j', '2014-12-24') - ...> strftime('%j', 'now')); Days to XMas: 37
Here we have computed the number of days till Christmas. The %j
modifier gives the day of the year for the time string.
In this part of the SQLite tutorial, we worked with built-in SQLite functions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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