Python's sqlite3 library learning

Source: Internet
Author: User

#-*-Coding:utf-8-*-

# import SQLite driver:
>>> Import Sqlite3
# Connect to the SQLite database
# Database file is test.db
# If the file does not exist, it is automatically created in the current directory:
>>> conn = sqlite3.connect (' test.db ')
# Create a cursor:
>>> cursor = conn.cursor ()
# Execute an SQL statement to create the user table:
>>> cursor.execute (' CREATE table user (ID varchar (primary key, name varchar (20)) ')
<sqlite3. Cursor Object at 0x10f8aa260>
# Continue executing an SQL statement and insert a record:
>>> Cursor.execute (' INSERT into user (ID, name) VALUES (\ ' 1\ ', \ ' michael\ ') ')
<sqlite3. Cursor Object at 0x10f8aa260>
# Gets the number of rows inserted through ROWCOUNT:
>>> Cursor.rowcount
1
# Close cursor:
>>> Cursor.close ()
# COMMIT TRANSACTION:
>>> Conn.commit ()
# Close Connection:
>>> Conn.close ()


Query Record:
>>> conn = sqlite3.connect (' test.db ')
>>> cursor = conn.cursor ()
# Execute the query statement:
>>> cursor.execute (' select * from user where id=? ', (' 1 ',))
<sqlite3. Cursor Object at 0x10f8aa340>
# Get query result set:
>>> values = Cursor.fetchall ()
>>> values
[(' 1 ', ' Michael ')]
>>> Cursor.close ()
>>> Conn.close ()

Python's sqlite3 library learning

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.