· 6 years ago · Aug 13, 2019, 10:04 AM
1#!/usr/bin/python3
2
3import pymysql
4import sys, getopt
5
6
7def main(argv):
8
9 print("Enter IP")
10 IP = str(sys.stdin.readline()).strip()
11 print("Enter Username")
12 USERNAME = str(sys.stdin.readline()).strip()
13 print("Enter PASSWORD")
14 PASSWORD = str(sys.stdin.readline()).strip()
15
16 try:
17 # Open database connection
18 db = pymysql.connect(IP,USERNAME,PASSWORD)
19
20 # prepare a cursor object using cursor() method
21 cursor = db.cursor()
22 except Exception as e:
23 print(e)
24 return e
25
26 # Create db
27 cursor.execute("""CREATE DATABASE IF NOT EXISTS KSM""")
28 db.commit()
29
30 try:
31 #use database
32 cursor.execute("""USE KSM""")
33 db.commit()
34
35 # Create table as per requirement
36 sql = """CREATE TABLE KEYTABLE (
37 ID CHAR(32) NOT NULL,
38 KSM_KEY CHAR(32),
39 KSM_IV CHAR(32))"""
40 cursor.execute(sql)
41 except Exception as e:
42 print(e)
43 return e
44
45
46 # disconnect from server
47 db.close()
48
49if __name__ == "__main__":
50 main(sys.argv[1:])