Sqlite is a C language library that provides a lightweight file database solution. She does not need server support and supports non-standard SQL statements.
Sqlite is a C language library that provides a lightweight file database solution. She does not need server support and supports non-standard SQL statements.
(Translated from python v2.7 document)
Sqlite is a C language library that provides a lightweight file database solution. She does not need server support and supports non-standard SQL statements.
Sqlite has been integrated into the python Standard Library since python2.5.
An example of using sqlite3:
Import sqlite3
Conn = sqlite3.connect ('example ')
# To create a memory database, conn = sqlite3.connect (': memory :')
# After the connection is created, create a cursor object and execute its execute () method to execute SQL statements.
C = conn. cursor ()
# Create a table
C.exe cute (''' create table table1
(Date text, trans text, symbol text,
Qty real, price real )''')
# Insert a row of data
C.exe cute (''' insert into table1
Values ('2017-01-05 ', 'buy', 'rhat', 2011, 100 )''')
# Save the changes
Conn. commit ()
# We can also close the cursor if we are done with it
C. close ()
Python variables are usually used for SQL operations. The python string cannot be used directly, because it faces the threat of SQL injection. Use DB-API parameters instead, use '?' where you want to use a string As a placeholder, and then provide a tuple value as the second parameter of the cursor execute () method (other database modules may use a different German placeholder, such as "% s" or ": 1 "). For example:
!! This is dangerous !!
# Never do this -- insecure!
Symbol = 'ibm'
C.exe cute ("... where symbol = '% S'" % symbol)
In this way, the requirements are met:
# Do this instead
T = (symbol ,)
C.exe cute ('select * from stocks where symbol =? ', T)
# Larger example
For t in [('2014-03-28 ', 'buy', 'ibm', 2006, 1000 ),
('2017-04-05 ', 'buy', 'msoft', 2006, 1000 ),
('2014-04-06 ', 'clerk', 'ibm', 2006, 500 ),
]:
C.exe cute ('insert into stocks values (?,?,?,?,?) ', T)