· 8 years ago · Apr 12, 2018, 10:48 AM
1'''
2import platform
3
4
5class Test:
6 def system_data(self):
7 global get_data
8 var = 0
9 variable = 'variable'
10 for value in zip(['system ','node ','release_version ','version ','machine ','processor'],platform.uname()):
11 var = var + 1
12 data = value[1]
13 get_data = variable+str(var)+' = '+str(data)
14 print(get_data)
15obj = Test()
16data= obj.system_data()
17'''
18
19'''
20class Test:
21 def test(self):
22 pass
23 print(get_data)
24
25
26
27obj = Test()
28obj.test()
29'''
30# Connect to the database
31'''
32class Crud:
33 connection = pymysql.connect(host='127.0.0.1',
34 user='root',
35 password='',
36 db='system',
37 charset='utf8mb4',
38 cursorclass=pymysql.cursors.DictCursor)
39
40 try:
41 with connection.cursor() as cursor:
42 # Create a new record
43 sql = "INSERT INTO `system_info` \
44 (`system`,`node`, `release_version`,\
45 `version`, `machine`,`processor`)\
46 VALUES (%s, %s, %s, %s, %s, %s)"
47
48 cursor.execute(sql, ('variable1', 'variable2',
49 'variable3', 8, 'variable5', 'variable6'))
50
51 # Commit for save data
52 connection.commit()
53 finally:
54 connection.close()
55
56obj = Crud()
57
58'''
59
60'''
61import pymysql
62
63# open database connection
64db = pymysql.connect("localhost", "root", "", "TESTDB")
65
66# prepare a cursor object
67cursor = db.cursor()
68
69# Drop table if it already exists
70cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
71
72# create table as per requirement
73sql = """CREATE TABLE SYSTEMINFO (
74 SYSTEM CHAR(20) NOT NULL,
75 NODE CHAR(20) NOT NULL,
76 RELEASE_VR INT NOT NULL,
77 VERSION FLOAT NOT NULL,
78 MACHINE CHAR(20) NOT NULL,
79 PROCESSOR CHAR(20) NOT NULL)"""
80
81cursor.execute(sql)
82
83# disconnect from server
84db.close()
85
86'''
87import platform
88import pymysql
89import pymysql.cursors
90
91# open database connection
92db = pymysql.connect("localhost", "root", "", "TESTDB")
93
94# prepare a cursor object using cursor() method
95cursor = db.cursor()
96
97# Drop table if it already exists
98cursor.execute("DROP TABLE IF EXISTS SYSTEMINFO")
99
100# create table as per requirement
101sql = """CREATE TABLE SYSTEMINFO (
102 SYSTEM CHAR,
103 NODE CHAR,
104 RELEASED_VR CHAR,
105 VERSION CHAR,
106 MACHINE CHAR,
107 PROCESSOR CHAR)"""
108
109cursor.execute(sql)
110
111# prepare SQL Query to INSERT a record into the database
112sql = "INSERT INTO SYSTEMINFO(SYSTEM, NODE, RELEASED_VR, VERSION, MACHINE, PROCESSOR)"
113
114for i in zip(['system ','node ','release ','version ','machine ','processor'],platform.uname()):
115 # print(i[0],':',i[1])
116 a = i[1]
117 print(a)
118 sql = sql + "VALUES ('"+str(a)+"')" # problem
119
120 try:
121 # execute the SQL command
122 cursor.execute(sql)
123
124 # commit changes in the database
125 db.commit()
126
127 except:
128 # rollback in case there is any error
129 db.rollback()
130
131
132# disconnect from server
133db.close()