Creating 1 SQLite database
File-New database, the pop-up dialog box, fill in DatabaseFile and DatabaseAlias information, click on the OK button to create a database
2 Create a table
Right-click the database name -new Table, pop-up dialog box, fill in the name of the table in the Table name, go to the stage to add fields
3 Add field
Click the Add button in the bottom left of the table, the pop-up dialog box, fill in the Name, Type, the Size, click OK, then finish adding a field. You can continue to add additional fields.
3 SQLite data types
Different from other relational databases, SQLite data is dynamic type, main types include: NULL, INTEGER, REAL, TEXT, BLOB
4 SQLite operation
4.1 Queries
Select * from 表名
4.2 Insert Data
insert into table (column 1, column 2, ...) values (value 1, value 2, ...);
Note: The parentheses following the table name, if all the columns to be assigned, can be omitted to fill in
When the insert operation SQLite, for integer, its value may be quoted, such as other characters, you need to add the date
For SQLite database, insert statements need to add a semicolon
4.3 modify the data
set update table column name = value where conditions
4.4 to delete data
the Delete from table where conditions
5 python operation SQLite database
5.1 operating module sqlite3
5.2 Key Objects
5.2.1 Connection object
Is defined: conn = sqlite3.connect ( 'database file')
NOTE: For CRUD operations, to commit connection object, in the format:
conn.commit()
After the completion of database operations, close the connection to the object, in the format:
conn.close()
5.2.2 Cursor:
Is defined: c = conn.cursor ()
Cursor object with execute () method, in the format:
result = c.execute ( 'SQL statement', [parameters]), wherein the parameter is optional, if the SQL select statement is a statement, as a result of the cursor object, may be traversed, a data type of tuples.
Note: For CRUD operations, to remember the connection object commit operation
Cursor object also with fetchall (), fetchone (), fetchmany (num) method such methods typically used with the query, the query results in a list read, wherein the or fetchall () to get all of the data query, either fetchone () Article 1 acquired data to the query, fetchmany (num) pieces of data are acquired num queried using the format:
lst=c.fetchall() lst=c.fetchone() lst=c.fetchmany(num)
5.3 SQLite SQL statement splicing
Recommended use parameterized query methods, to avoid SQL injection, using parameterized queries need to use placeholders, and there?:
5.3.1? Placeholder
name=(‘Tom’,) sql=”select * from 表名 where Name=?” c.fetchone(sql,name)
5.3.2: placeholder
sql=”insert into 表名 values(:name,:id,:birthday)”
c.execute(sql,{‘name’=’Tom’,’id’=13,birthday’=’1984-01-16’})
Note: different database types may be different
Note: strongly not recommended for use in python string splicing, such as:
name=’Tom’ sql=”select * from 表名 where Name={}”.format(name)