Python Common libraries

Source: Internet
Author: User

Sqlite3
    • Create a Connection object to represent a db with the following code:

Import Sqlite3

conn = Sqlite3.connect (' example.db ') #如果要在RAM中创建一个db, DB name used: Memory:


When more than one connection accesses the db at the same time and there is a process that modifies the DB, the DB is always locked until transaction is committed, and the common parameters of Connect () are:

    • The timeout parameter specifies how long the connection waits and then throws an exception, which defaults to 5 seconds.
    • Isolation_level: Set How to turn on Tranction, open transaction when a DML statement is encountered by default, submit when NON-DML statement is encountered transaction
    • Cached_statements: The number of statements that this connection can hold in the cache, which defaults to 100
    • Type Uri:boolean, specifying the db with a URI, you can specify the option of the connection, such as opening the DB as read-only
      db = Sqlite3.connect (' File:path/to/database?mode=ro ', uri=true)
  • then create a cursor object to execute the SQL command when executing the query, pay attention to the security of the SQL statement,

    C=conn.cursor ()
    # never do this--insecure!

    Symbol = ' Rhat '

    C.execute ("SELECT * from stocks WHERE symbol = '%s '"% symbol)

    ?

    # do this instead, note that there is only one element in a tuple followed by a comma
    #sql语句中的替换符为?

    t = (' Rhat ',)

    C.execute (' SELECT * from stocks WHERE symbol=? ', T)

    Print (C.fetchone ())

    ?

    # Larger example that inserts many records at a time

    purchases = [(' 2006-03-28 ', ' BUY ', ' IBM ', 1000, 45.00),

    (' 2006-04-05 ', ' BUY ', ' MSFT ', 1000, 72.00),

    (' 2006-04-06 ', ' SELL ', ' IBM ', 500, 53.00),

    ]

    C.executemany (' INSERT into stocks VALUES (?,?,?,?,?) ', purchases)

  • If you finish executing the query statement using execute, you can use the following methods to get the data:
    • Use the cursor as a iterator to get one row of row data at a time using the For statement
    • Use Fetchone () to fetch one row of row data at a time, and finally return none
    • Use Fetchall () to get the list of row, no data to return none



Python Common libraries

Related Article

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.