Skip to content

Commit 2422f36

Browse files
committed
Add notes on connecting to SQL dbs with Python
1 parent b12912e commit 2422f36

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,3 +3023,14 @@ You can run a SQL file using `.read file_name.sql` inside Sqlite.
30233023
`SELECT * from table_name WHERE column_name IS NOT value AND column_name2 IS NOT value2;`
30243024

30253025
`SELECT * FROM table_name WHERE column_name LIKE %value%`;
3026+
3027+
### Connecting to a Database with Python
3028+
3029+
Python comes with a built-in library to communicate with Sqlite.
3030+
3031+
```python
3032+
import sqlite3
3033+
conn = sqlite3.connect("example.db")
3034+
```
3035+
3036+

python_sql/friends.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sqlite3
2+
conn = sqlite3.connect("my_friends.db")
3+
# create cursor object
4+
c = conn.cursor()
5+
# execute some sql
6+
# c.execute("CREATE TABLE friends (first_name TEXT, last_name TEXT, closeness INTEGER);")
7+
# insert_query = "INSERT INTO friends VALUES ('Merry', 'Brandybuck', 7);"
8+
# form_first = "Dana"
9+
# query = f"INSERT INTO friends (first_name) VALUES (?)"
10+
# c.execute(query, (form_first,))
11+
12+
data = ("Steve", "Irwin", 9)
13+
query = "INSERT INTO friends VALUES (?,?,?)"
14+
c.execute(query, data)
15+
# commit changes
16+
conn.commit()
17+
conn.close()

python_sql/my_friends.db

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)