BAD_2019_PojProg/mysql_python/mysql_connect.py

35 lines
735 B
Python
Raw Normal View History

2019-04-19 16:39:15 +02:00
import mysql.connector
import getpass
HOST="localhost"
DATABASE="Fabryka_Sprzetu_IT"
2019-04-19 17:24:46 +02:00
SQL="SELECT * FROM Produkt;"
2019-04-19 16:39:15 +02:00
print("Welcome to MySQL connector. \nYou are trying to connect to host: " + HOST + "\nType your credentials to connect:")
while(True):
print("User: ", end='')
user=input()
password=getpass.getpass()
print()
try:
sqlDatabase = mysql.connector.connect(host=HOST, user=user, passwd=password, database=DATABASE)
break
except mysql.connector.Error as e:
print(e)
if e.errno!=1045:
exit()
else:
print("Try again.\n")
curs=sqlDatabase.cursor()
print(SQL)
curs.execute(SQL)
res=curs.fetchall()
for r in res:
print(r)