0% found this document useful (0 votes)
4 views3 pages

Python SQL Commands Guide

This guide provides an overview of using Python to interact with SQLite databases, covering essential commands such as setting up a connection, creating tables, inserting, fetching, updating, and deleting data. It also includes examples of using SQL with Pandas and error handling techniques. The document serves as a practical reference for executing SQL commands in Python.

Uploaded by

mathisaravanan23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python SQL Commands Guide

This guide provides an overview of using Python to interact with SQLite databases, covering essential commands such as setting up a connection, creating tables, inserting, fetching, updating, and deleting data. It also includes examples of using SQL with Pandas and error handling techniques. The document serves as a practical reference for executing SQL commands in Python.

Uploaded by

mathisaravanan23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python SQL Commands Guide

# 1. Introduction
Python allows interaction with SQL databases using libraries like:
- sqlite3 (for SQLite)
- [Link] (for MySQL)
- psycopg2 (for PostgreSQL)

This guide focuses on SQLite.

# 2. Setting Up the Database Connection


```python
import sqlite3
conn = [Link]("my_database.db")
cursor = [Link]()
```

# 3. Creating a Table
```python
[Link]('''
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER,
department TEXT
)
''')
[Link]()
```

# 4. Inserting Data
```python
[Link]("INSERT INTO employees (name, age, department) VALUES (?, ?, ?)",
("John Doe", 28, "IT"))
[Link]()
```

For multiple records:


```python
employees = [
("Alice Smith", 30, "HR"),
("Bob Johnson", 25, "Finance"),
("Charlie Brown", 35, "IT")
]

[Link]("INSERT INTO employees (name, age, department) VALUES (?, ?, ?)", employees)
[Link]()
```

# 5. Fetching Data
```python
[Link]("SELECT * FROM employees")
rows = [Link]()
for row in rows:
print(row)
```
Fetch a single row:
```python
[Link]("SELECT * FROM employees WHERE name = ?", ("John Doe",))
print([Link]())
```

# 6. Updating Data
```python
[Link]("UPDATE employees SET age = ? WHERE name = ?", (30, "John Doe"))
[Link]()
```

# 7. Deleting Data
```python
[Link]("DELETE FROM employees WHERE name = ?", ("John Doe",))
[Link]()
```

Delete all records:


```python
[Link]("DELETE FROM employees")
[Link]()
```

# 8. Closing the Connection


```python
[Link]()
```

# 9. Using SQL with Pandas


```python
import pandas as pd
conn = [Link]("my_database.db")
df = pd.read_sql_query("SELECT * FROM employees", conn)
print([Link]())
```

# 10. Error Handling


```python
try:
conn = [Link]("my_database.db")
cursor = [Link]()
[Link]("SELECT * FROM employees")
rows = [Link]()
for row in rows:
print(row)
except [Link] as e:
print("Error:", e)
finally:
[Link]()
```

You might also like