-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlscr1.py
More file actions
167 lines (134 loc) · 3.23 KB
/
Copy pathsqlscr1.py
File metadata and controls
167 lines (134 loc) · 3.23 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# # Step 1 : import module
import pymysql as p
# # #cx_Oracle
# #
# # # Step 2 : connect to database
# santhoshdb = p.connect(host = 'localhost', user = 'root', password = 'root', db = 'santhosh')
# print("connected successfully")
#
#
# #
# # # Step3 : create cursor
# mycursor = santhoshdb.cursor()
#
#
# # # # # #### selelect query ....
# # # # Step 4 : Prepare the query
# q1 = [
# 'select * from login;',
# 'select count(*) from login;',
# 'select email,password from login where name = "sandy";',
# 'select * from login where name = "santhosh";'
# ]
#
# for query in q1:
#
# # Step 5 : execute the query
# mycursor.execute(query)
#
# # Step 6 : fetch all result
# res = mycursor.fetchall()
# print(res)
#
#
# # Step7 : close DB
# santhoshdb.close()
#### dml statements
# #
# # #step 1
# import pymysql as p
# # Step 2 : connect to database
# db = p.connect(host = 'localhost', user = 'root', password = 'root', db = 'santhosh')
# print("connected successfully")
# # Step3 : create cursor
# mycursor = db.cursor()
# ##### insert query
# # Step 4 : insert the data
# # q1 = [
# # "INSERT INTO login (id, name, email, password) VALUES (11, 'Venkat','venkat@gmail.com','venkat@987');",
# # "INSERT INTO login (id, name, email, password) VALUES (12, 'gokul','gokul@gmail.com','gukul@880');",
# # ]
# q1 = [
# """
# CREATE TABLE Ashwini (
# PersonID int,
# LastName varchar(255),
# FirstName varchar(255),
# Address varchar(255),
# City varchar(255)
# );
# """,
# """
# CREATE TABLE Ameen (
# PersonID int,
# LastName varchar(255),
# FirstName varchar(255),
# Address varchar(255),
# City varchar(255)
# );
# """
# ]
# # Step 5 : execute the query
# for x in q1:
# try:
# mycursor.execute(x)
# except Exception as err:
# print("error msg was = ", err)
# # Step 6 : commit
# db.commit()
# # Step 7 : Close DB
# db.close()
# print("successfully inserted ")
##############################################################################################
newdb = p.connect(host='localhost', user='root', password='root', db='srinivas')
print('Connected successfully')
mycursor = newdb.cursor()
q1 =['select * from users',
'select name from users',
'select email from users',
'show tables;',
"""CREATE TABLE empdetail(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
number VARCHAR(10),
email VARCHAR(255),
address VARCHAR(255)
);"""
]
# for query in q1:
try:
mycursor.execute(q1[3])
except Exception as err:
print('Error is: ', err)
res = mycursor.fetchall()
if (res == 'empdetail'):
print('Table empdetail exist')
else:
try:
mycursor.execute(q1[4])
except Exception as err:
print('Erroe is: ', err)
print('created')
newdb.commit()
newdb.close()
#
# q1 = [
# """CREATE TABLE empdetail(
# id INT AUTO_INCREMENT PRIMARY KEY,
# name VARCHAR(255),
# number VARCHAR(10),
# email VARCHAR(255),
# address VARCHAR(255)
# );"""]
#
# for x in q1:
# try:
# mycursor.execute(x)
# except Exception as err:
# print('Error is: ', err)
#
# newdb.commit()
#
# newdb.close()
#
# print('Database Created')