File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
cheatsheets/python/libraries Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments