· 10 years ago · Feb 15, 2016, 07:15 PM
1import mysql.connector
2import requests
3import math
4
5cnx = mysql.connector.connect(user='testUser', password='p@ssw0rd',
6 host='192.168.197.130',
7 database='simpleDB')
8
9cursor = cnx.cursor()
10
11createTable = """CREATE TABLE IF NOT EXISTS users (id INT(10) NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, luckyNumber INT(10) NOT NULL, PRIMARY KEY(id))"""
12
13insertQuery = """INSERT INTO users (name, luckyNumber) VALUES ('lsd', 23),('kenny', 13),('root', 1),('henry', 5)"""
14selectQuery = "SELECT name, luckyNumber FROM users WHERE luckyNumber>9"
15
16dropQuery = "DROP TABLE users"
17
18print "creating"
19cursor.execute(createTable)
20
21print "inserting"
22cursor.execute(insertQuery)
23
24print "select"
25cursor.execute(selectQuery)
26
27for (name, luckyNumber) in cursor:
28 print str(name) + " has a lucky number %i" % luckyNumber
29 r = requests.post("http://posttestserver.com/post.php", data={'username': name.capitalize(), 'luckyNumber': int(math.pi*luckyNumber*math.sqrt(404))%100, 'super': True})
30 print r.status_code
31 print r.text + "\n\n"
32
33print "dropping"
34cursor.execute(dropQuery)
35
36cursor.close()
37cnx.close()