Skip to content

Commit 8d684d2

Browse files
Create sqlite.md
1 parent 84ec043 commit 8d684d2

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# SQLite
2+
3+
4+
## Setup cursor
5+
6+
```python
7+
import sqlite3
8+
9+
10+
conn = sqlite3.connect('trends.db')
11+
cur = conn.cursor()
12+
```
13+
14+
## Create table
15+
16+
```python
17+
drop_query = 'DROP TABLE IF EXISTS foo;'
18+
cur.execute(drop_query)
19+
20+
create_query = """\
21+
CREATE TABLE IF NOT EXISTS foo (
22+
id INTEGER PRIMARY KEY,
23+
bar VARCHAR(50)
24+
baz INTEGER
25+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
26+
);
27+
"""
28+
cur.execute(create_query)
29+
```
30+
31+
32+
## Insert
33+
34+
```python
35+
insert_sql = """
36+
INSERT INTO foo (bar, baz, updated_at)
37+
VALUES (?, ?, CURRENT_TIMESTAMP);
38+
"""
39+
cur.execute(insert_sql, "Fizz", 123)
40+
```
41+
42+
43+
## Select
44+
45+
```python
46+
47+
select_sql = """
48+
SELECT *
49+
FROM foo;
50+
"""
51+
52+
cur.execute(select_sql)
53+
for row in cur:
54+
print(row)
55+
```

0 commit comments

Comments
 (0)