SQLite3 for Python

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.