-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mysql_connector.py
executable file
·52 lines (51 loc) · 1.65 KB
/
test_mysql_connector.py
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
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python
'''
Program:
This is a program for testing mysql connector package.
Usage:
test_mysql_connector.py
Editor:
Jacob975
20180816
#################################
update log
20180816 version alpha 1
1. The code works.
'''
import mysql.connector as mariadb
import time
#--------------------------------------------
# main code
if __name__ == "__main__":
# Measure time
start_time = time.time()
#----------------------------------------
# define table format
authority = {'user':'TAT',
'password':'1234',
'database':'frame_data',
'host':'localhost'
}
source_format = ['id INT AUTO_INCREMENT',
'name VARCHAR(255)',
'address VARCHAR(255)',
'PRIMARY KEY (id)']
# Login mariadb as user 'TAT'@'localhost'
cnx = mariadb.connect(**authority)
cursor = cnx.cursor()
# Create a table in database, try to read and write.
sql = 'create table if not exists test ({0})'.format(' , '.join(source_format))
print sql
cursor.execute(sql)
cursor.execute('insert into test (name, address) values (%s, %s)', ("Alice", "46804804"))
cursor.execute('insert into test (name, address) values (%s, %s)', ("Bob", "46804805"))
cursor.execute('select * from test')
data = cursor.fetchall()
print data
# drop the table
cursor.execute('drop table test')
cursor.execute('show tables')
#---------------------------------------
# Measure time
elapsed_time = time.time() - start_time
print "Exiting Main Program, spending ", elapsed_time, "seconds."