-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmysql_db_example.py
More file actions
42 lines (35 loc) · 1.43 KB
/
mysql_db_example.py
File metadata and controls
42 lines (35 loc) · 1.43 KB
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
39
40
41
42
# first install PyMySQL with - pip install PyMySQL
# start XAMPP and start all services - NOTE: not XAMPP-VM
# this does not work with XAMPP-VM, but does work with plain XAMPP
# see https://github.com/PyMySQL/PyMySQL
import pymysql.cursors
# connect to the local database
conn = pymysql.connect(host='localhost',
user='root',
password=None,
db='myshoutbox',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
# variables for new record - used below
name = "Monty"
shout = "Python rules!"
mydate = "12:12:12 pm 02-24-2018"
try:
with conn.cursor() as cursor:
# create a new record in the database
sql = "INSERT INTO `shouts` (`name`, `shout`, `date`) VALUES (%s, %s, %s)"
# format strings in Python: string (%s), integer (%d),
# float (%f), and float restricted to 2 decimal places (%.2f)
cursor.execute(sql, (name, shout, mydate))
# the connection is not autocommit by default - you must commit to
# save your changes
conn.commit()
with conn.cursor() as cursor:
# read a single record that exists in the database
# note, this is a record in MY local databse, not yours
sql = "SELECT `name`, `shout` FROM `shouts` WHERE `date`=%s"
cursor.execute(sql, ('01:26:00 pm 01-22-2018',))
result = cursor.fetchone()
print(result)
finally:
conn.close()