Skip to content

Added a file in ch15 explaning the connection of mysql and python, cr… #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ch15-sql-database-connections/2-mysql-python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import mysql.connector
import pyautogui

mypasswd=pyautogui.prompt('Enter your mysql password')
mydb=mysql.connector.connect(host='localhost', user='root', password=mypasswd)
cur=mydb.cursor()
cur.execute('create database if not exists rdm123')

mydb1=mysql.connector.connect(host='localhost',
user='root',
password=mypasswd,
database='rdm123')
cur1=mydb1.cursor()
cur1.execute('create table if not exists abc(number int(2) primary key not null auto_increment, name varchar(20), gender varchar(1))')

name=pyautogui.prompt('Enter Name')
gender=pyautogui.prompt('Enter Gender')

print(name)

if name!=None or gender!=None:
data1=(name,gender)
cur1.execute('insert into abc(name , gender) values (%s,%s)',data1)
mydb1.commit()
pyautogui.alert('Data Filled Successfully.')
else:
pyautogui.alert('Data Filling Cancelled')

show=pyautogui.confirm(text='Would you like to show the databases?', buttons=['Yes','No'])

if show == 'Yes':
cur1.execute('select * from abc')
shwd=cur1.fetchall()
pyautogui.alert(shwd)
print('SUCCED!')
else:
print('NOT SUCCED!')