Python using the SQLite example

Source: Internet
Author: User
Tags mysql insert sqlite sqlite database



SQLite is an embedded database, and its database is a file. Python has built-in SQLite3, so using SQLite in Python does not require anything to be installed and used directly.



Operation relational database, first need to connect to the database, a database connection is called connection;



After connecting to the database, you need to open the cursor, called the cursor, execute the SQL statement through the cursor, and then get the execution result.



Python defines a set of API interfaces for manipulating databases, and any database to be connected to Python requires only a Python-compliant database driver.



Since SQLite's drivers are built into the Python standard library, we can directly manipulate the SQLite database.



Example code:


 
import sqlite3

conn = sqlite3.connect(‘test.db‘)

cursor = conn.cursor()

cursor.execute(‘select * from user‘)

values = cursor.fetchall() for value in values: print(value)

cursor.close()
conn.close()
    • The commit transaction is invoked after an insert operation is performedcommit();

    • The SQL placeholder for MySQL is%s,sqlite的占位符是?


Example:


Cursor.execute ('select * from user where id =%s', ('1',))  / /MySQL INSERT statement
Cursor.execute ('select * from user where id=? ', ('1',))     //SQLite INSERT statement





Python using the SQLite example


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.