Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d4fcade

Browse files
sqlite3 and python tut
1 parent 4b2328c commit d4fcade

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

‎PythonSQLite3.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import sqlite3
2+
import sys
3+
import csv
4+
5+
db_conn = sqlite3.connect('test.db')
6+
print("Database Created")
7+
the_cursor = db_conn.cursor()
8+
9+
the_cursor.execute("DROP TABLE employees;")
10+
11+
12+
def print_db():
13+
try:
14+
result = the_cursor.execute("SELECT id, f_name, l_name, age, address, salary, hire_date "
15+
"FROM employees")
16+
for row in result:
17+
print("id:", row[0])
18+
print("f_name:", row[1])
19+
print("l_name:", row[2])
20+
print("age:", row[3])
21+
print("address:", row[4])
22+
print("salary:", row[5])
23+
print("hire_date:", row[6])
24+
except sqlite3.OperationalError:
25+
print("The table doesn't exist")
26+
except:
27+
print("Couldn't retrieve data from database")
28+
29+
30+
try:
31+
db_conn.execute("CREATE TABLE employees("
32+
"id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
33+
"f_name TEXT NOT NULL,"
34+
"l_name TEXT NOT NULL, "
35+
"age INT NOT NULL, "
36+
"address TEXT, "
37+
"salary REAL, "
38+
"hire_date TEXT);")
39+
db_conn.commit()
40+
print("Table Created")
41+
except sqlite3.OperationalError as e:
42+
print("Table couldn't be created:", str(e))
43+
44+
db_conn.execute("INSERT INTO employees(f_name, l_name, age, address, salary, hire_date) VALUES('Derek', 'Banas', '43', "
45+
"'123 Main St', '500000', date('now'));")
46+
db_conn.commit()
47+
print("Employee Entered")
48+
49+
print_db()
50+
51+
try:
52+
db_conn.execute("UPDATE employees SET address = '121 Main St'"
53+
"WHERE ID = 1")
54+
db_conn.commit()
55+
except sqlite3.OperationalError:
56+
print("Database couldn't be updated")
57+
58+
print_db()
59+
60+
try:
61+
db_conn.execute("DELETE FROM employees"
62+
"WHERE id = 1")
63+
db_conn.commit()
64+
except sqlite3.OperationalError:
65+
print("Data couldn't be deleted")
66+
67+
print_db()
68+
69+
db_conn.rollback()
70+
71+
print_db()
72+
73+
try:
74+
db_conn.execute("ALTER TABLE employees"
75+
"ADD COLUMN 'image' BLOB DEFAULT NULL")
76+
db_conn.commit()
77+
except sqlite3.OperationalError:
78+
print("Table couldn't be altered")
79+
80+
the_cursor.execute("PRAGMA TABLE_INFO(employees)")
81+
row_names = [nameTuple[1] for nameTuple in the_cursor.fetchall()]
82+
print(row_names)
83+
84+
the_cursor.execute('SELECT COUNT(*)'
85+
'FROM employees')
86+
num_of_rows = the_cursor.fetchall()
87+
print("Total Rows:", num_of_rows[0][0])
88+
89+
the_cursor.execute("SELECT SQLITE_VERSION()")
90+
print("SQLITE VERSION:", the_cursor.fetchone())
91+
92+
with db_conn:
93+
db_conn.row_factory = sqlite3.Row
94+
the_cursor = db_conn.cursor()
95+
the_cursor.execute("SELECT * FROM employees")
96+
rows = the_cursor.fetchall()
97+
for row in rows:
98+
print("{} {}".format(row["f_name"], row["l_name"]))
99+
100+
with open('dumpl.sql', 'w') as f:
101+
for line in db_conn.iterdump():
102+
f.write("%s\n" % line)
103+
104+
db_conn.close()

‎dumpl.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
BEGIN TRANSACTION;
2+
CREATE TABLE employees(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, f_name TEXT NOT NULL,l_name TEXT NOT NULL, age INT NOT NULL, address TEXT, salary REAL, hire_date TEXT);
3+
INSERT INTO "employees" VALUES(1,'Derek','Banas',43,'121 Main St',500000.0,'2021年06月25日');
4+
DELETE FROM "sqlite_sequence";
5+
INSERT INTO "sqlite_sequence" VALUES('employees',1);
6+
COMMIT;

‎test.db

12 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /