PYTHON Tutorial
sqlite3.connect('database.db')
.connection.cursor()
.execute()
method to execute SQL commands. For example, cursor.execute('SELECT * FROM table_name')
.fetchall()
method. For insert, update, and delete operations, use the cursor's rowcount
property to get the number of rows affected.connection.close()
.import sqlite3
# Establish connection
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
# Execute query
cursor.execute('SELECT * FROM table_name')
results = cursor.fetchall()
# Insert data
cursor.execute('INSERT INTO table_name VALUES (1, "John Doe")')
# Update data
cursor.execute('UPDATE table_name SET name="Jane Doe" WHERE id=1')
# Delete data
cursor.execute('DELETE FROM table_name WHERE id=2')
# Commit changes
connection.commit()
# Close connection
connection.close()