Download presentation
Presentation is loading. Please wait.
1
Cosc 5/4730 Sqlite primer
2
Table Creation Syntax CREATE TABLE database_name.table_name( column1 datatype PRIMARY KEY, column2 datatype, column3 datatype, columnN datatype, );
3
Table Creation Example
CREATE TABLE COMPANY( ID INT PRIMARY KEY AUTO INCREMENT, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS TEXT, SALARY REAL ); This the db fills in for you. NOT null means you have to Enter a value While these can be left blank
4
Data Types Type Description NULL The value is a NULL value. INTEGER
The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value. REAL The value is a floating point value, stored as an 8-byte IEEE floating point number. TEXT The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE) BLOB The value is a blob of data, stored exactly as it was input.
5
Delete table Syntax: Example
DROP TABLE table_name; Example DROP TABLE company; A note, keywords don’t have to capitalized, it just to show them.
6
Insert Syntax: Example:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)] VALUES (value1, value2, value3,...valueN); Example: INSERT INTO COMPANY (NAME,AGE,ADDRESS, SALARY) VALUES ('Paul', 32, 'California', );
7
Insert android Long insert(String TABLE_NAME, String nullColumanHAack, ContentValues values); Where ContentValues is hash with column as a key and the value is the data. Likely nullColumnHack will be null.
8
Select (query) Basic version which will return all rows.
SELECT column1, column2....columnN FROM table_name; //only return columns listed. SELECT * FROM table_name; //all columns So SELECT * FROM company; Will list all the rows in the company table. A note, you can lists the tables in the database with SELECT tbl_name FROM sqlite_master WHERE type = 'table';
9
Select (query) 2 Where cause.
SELECT * FROM table_Name WHERE [condition]; SELECT * FROM company WHERE age >=25; Operators ==, = (same as ==), !=, <> ( same as !=), <, >, >=, <=, !>, !< Operators +, -, *, / % (modulus) can be used as well Logical operator can be used as well. SELECT * FROM company WHERE age >=25 AND salary <= 50000; SELECT * FROM COMPANY WHERE NAME LIKE ‘Ji%'; SELECT * FROM COMPANY WHERE AGE IS NOT NULL;
10
Logical operators Operator Description AND
The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause. BETWEEN The BETWEEN operator is used to search for values that are within a set of values, given the minimum value and the maximum value. EXISTS The EXISTS operator is used to search for the presence of a row in a specified table that meets certain criteria. IN The IN operator is used to compare a value to a list of literal values that have been specified. NOT IN The negation of IN operator which is used to compare a value to a list of literal values that have been specified. LIKE The LIKE operator is used to compare a value to similar values using wildcard operators. GLOB The GLOB operator is used to compare a value to similar values using wildcard operators. Also, GLOB is case sensitive, unlike LIKE. NOT The NOT operator reverses the meaning of the logical operator with which it is used. Eg. NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is negate operator. OR The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause. IS NULL The NULL operator is used to compare a value with a NULL value. IS The IS operator work like = IS NOT The IS operator work like != || Adds two different strings and make new one. UNIQUE The UNIQUE operator searches every row of a specified table for uniqueness (no duplicates).
11
Query android Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) GroupBy, having, and OrderBy are shown later in the slides When in doubt on a query, use Cursor rawQuery (String sql, String[] selectionArgs) With selectionArgs as null.
12
UPDATE Syntax UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition]; Example UPDATE COMPANY SET addres = 'Texas' WHERE id = 6; A Note, if you leave off the where cause, it will update every row.
13
UPDATE Android int update(String table, ContentValues values, String whereClause, String[] whereArgs)
14
Delete Syntax DELETE FROM table_name WHERE [condition]; Example DELETE FROM company WHERE id = 7; A Note, if you leave off the where cause, it will delete every row.
15
Delete Android int delete(String table, String whereClause, String[] whereArgs)
16
Limit cause The SQLite LIMIT clause is used to limit the data amount returned by the SELECT statement. Syntax SELECT column1, column2, columnN FROM table_name LIMIT [no of rows] SELECT column1, column2, columnN FROM table_name LIMIT [no of rows] OFFSET [row num] Examples SELECT * FROM COMPANY LIMIT 6; //only 6 rows SELECT * FROM COMPANY LIMIT 3 OFFSET 2; //start at row 3 and return 3 rows. (ie row 3 to 5)
17
ORDER BY cause Syntax: example
SELECT * … [ ORDER BY column1, column2, …] [ASC | DESC] Where ASC is ascending, DESC is descending default is ascending example SELECT * FROM company ORDER BY salary; SELECT * FROM COMPANY ORDER BY name, salary DESC; Descending order by name and then by salary
18
GROUP BY cause clause is used in collaboration with the SELECT statement to arrange identical data into groups. Note the ORDER BY must follow the GROUP BY Syntax: SELECT column-list FROM table_name WHERE [ conditions ] GROUP BY column1, column2....columnN ORDER BY column1, column2....columnN
19
execSQL execSQL(String sql, Object[] bindArgs)
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
20
Reference http://www.tutorialspoint.com/sqlite/sqlite_syntax.htm
And lot more information then provided here.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.