-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinit_db.py
More file actions
executable file
·38 lines (34 loc) · 938 Bytes
/
init_db.py
File metadata and controls
executable file
·38 lines (34 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Used to initialize example db and table
"""
import mysql.connector
def init_db():
"""
create db, table and insert some example
"""
# connect to db
local_db = mysql.connector.connect(
host="localhost",
user="root",
passwd="123456"
)
local_cursor = local_db.cursor()
# create db users
local_cursor.execute("CREATE DATABASE users")
# connect to specified db
user_db = mysql.connector.connect(
host="localhost",
user="root",
passwd="123456",
database="users"
)
user_cursor = user_db.cursor()
# create table userinfo
user_cursor.execute("CREATE TABLE userinfo (name VARCHAR(255), address VARCHAR(255))")
# insert example
sql = "INSERT INTO userinfo (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
user_cursor.execute(sql, val)
user_db.commit()
if __name__ == "__main__":
init_db()