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

Python Details PDF

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)
24 views3 pages

Python Details PDF

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
You are on page 1/ 3

In Python, you can interact with SQL databases using various libraries.

One of the most commonly


used libraries for this purpose is `sqlite3`, which is a built-in library in Python and provides a simple
way to work with SQLite databases. Addi onally, there are third-party libraries like `SQLAlchemy` and
`pymysql` for interac ng with different types of SQL databases such as MySQL, PostgreSQL, Oracle,
etc.

Here's a brief explana on of how you can use `sqlite3` to interact with an SQLite database in Python:

1. **Connec ng to a Database:**

To connect to an SQLite database, you use the `connect()` func on from the `sqlite3` module,
specifying the path to the database file. If the file does not exist, it will be created.

```python

import sqlite3

# Connect to the database (creates a new database if not exists)

connec on = sqlite3.connect('example.db')

```

2. **Crea ng a Cursor:**

Once connected, you need to create a cursor object using the `cursor()` method of the connec on
object. The cursor is used to execute SQL commands and manage the results.

```python

# Create a cursor object

cursor = connec on.cursor()

```

3. **Execu ng SQL Commands:**

You can execute SQL commands using the `execute()` method of the cursor object. You can execute
commands like crea ng tables, inser ng data, upda ng data, dele ng data, etc.

```python

This study source was downloaded by 100000864763182 from CourseHero.com on 05-16-2024 15:37:58 GMT -05:00

https://www.coursehero.com/file/226062581/Python-Detailspdf/
# Create a table

cursor.execute('''CREATE TABLE IF NOT EXISTS Employees (

EmployeeID INT PRIMARY KEY,

LastName VARCHAR(50),

FirstName VARCHAR(50),

Department VARCHAR(50)

)''')

# Insert data into the table

cursor.execute("INSERT INTO Employees VALUES (1, 'Doe', 'John', 'IT')")

# Commit the transac on

connec on.commit()

```

4. **Fetching Data:**

You can fetch data from the database using methods like `fetchone()`, `fetchall()`, or by itera ng
over the cursor object.

```python

# Fetch data from the table

cursor.execute("SELECT * FROM Employees WHERE Department = 'IT'")

rows = cursor.fetchall()

for row in rows:

print(row)

```

5. **Closing the Connec on:**

Once you're done working with the database, it's important to close the connec on to release the
database resources.

```python

This study source was downloaded by 100000864763182 from CourseHero.com on 05-16-2024 15:37:58 GMT -05:00

https://www.coursehero.com/file/226062581/Python-Detailspdf/
# Close the connec on

connec on.close()

```

This is a basic overview of using `sqlite3` in Python. Depending on the library you choose, the syntax
and methods may vary slightly, but the underlying principles remain similar.

This study source was downloaded by 100000864763182 from CourseHero.com on 05-16-2024 15:37:58 GMT -05:00

https://www.coursehero.com/file/226062581/Python-Detailspdf/
Powered by TCPDF (www.tcpdf.org)

You might also like