📜  Python SQLite——游标对象(1)

📅  最后修改于: 2023-12-03 15:19:01.150000             🧑  作者: Mango

Python SQLite - Cursor Object

Python SQLite module provides a Cursor object that is used to interact with SQLite database.

The Cursor object allows you to execute SQL statements on a specific database connection and retrieve data from the result set.

Creating a Cursor Object

Before you can execute SQL statements on an SQLite database, you need to create a connection object to that database. Once you have a connection object, you can create a Cursor object that is used to execute SQL statements and retrieve data.

Here's how you can create a Cursor object:

# Import sqlite module
import sqlite3

# Connect to the database
conn = sqlite3.connect('example.db')

# Create a cursor object
cursor = conn.cursor()

In the above example, we first import the sqlite3 module. Next, we create a connection object to the example.db database using the connect() method. Finally, we create a cursor object using the cursor() method of the connection object.

Executing SQL Statements

Once you have a Cursor object, you can execute SQL statements on the database. The execute() method of the cursor object is used to execute SQL statements.

Here's an example of executing an SQL statement to create a table:

# Execute the SQL statement to create a table
cursor.execute('''CREATE TABLE stocks
             (date text, trans text, symbol text, qty real, price real)''')

In the above example, we execute an SQL statement to create a table named stocks. The table has five columns, date, trans, symbol, qty, and price.

Retrieving Data

Once you have executed an SQL statement, you can retrieve data from the result set using the fetchone() or fetchall() methods of the cursor object.

Here's an example of retrieving data from the stocks table:

# Execute the SQL statement to retrieve data from the stocks table
cursor.execute("SELECT * FROM stocks")

# Retrieve all rows from the result set
rows = cursor.fetchall()

# Print the rows
for row in rows:
    print(row)

In the above example, we execute an SQL statement to retrieve all rows from the stocks table using the SELECT statement. We then retrieve all the rows from the result set using the fetchall() method and print each row.

Committing and Rolling Back Changes

After you have executed SQL statements on a database, you need to commit the changes or rollback if necessary. The commit() method is used to save changes to the database while the rollback() method is used to undo any changes made.

Here's an example of committing changes to the database:

# Execute the SQL statement to insert data into the stocks table
cursor.execute("INSERT INTO stocks VALUES ('2022-07-01','BUY','AAPL',100,538)")

# Commit the changes
conn.commit()

In the above example, we execute an SQL statement to insert a row into the stocks table. We then commit the changes using the commit() method of the connection object.

Closing the Database Connection

After you have finished using the database, you should close the connection to the database using the close() method of the connection object.

Here's an example of closing the connection to the database:

# Close the connection to the database
conn.close()

In the above example, we close the connection to the example.db database using the close() method of the connection object.

That's a brief introduction to the Cursor object in Python SQLite. The Cursor object is a powerful tool for interacting with SQLite databases and retrieving data from the result set.