Reputation: 155
I have installed spyder with anacoda for python 3.7 version for windows. Then if I print("hello")
, it works fine.
Now I want to connect with MYSQL database so I have installed python drivers to communicate with MYSQL. Using Anaconda Prompt I have run below command:
conda install -c anaconda mysql-connector-python
I got message in Anaconda Prompt that packages installed successfully. I don't know how to get username and password to connect with MYSQL? I have already checked that MYSQL installed properly using import mysql.connector
.
Code as per below:
import mysql.connector
from mysql.connector import Error
def connect():
try:
conn = mysql.connector.connect(host='127.0.0.1',
user='',
passwd='')
if conn.is_connected():
print('Connected to MySQL database')
except Error as e:
print(e)
connect()
Error:
2003: Can't connect to MySQL server on '127.0.0.1:3306' (10061 No connection could be made because the target machine actively refused it)
Upvotes: 6
Views: 15633
Reputation: 1
import mysql.connector
# Opening window
def dcr():
global un
global pw
print("========== WELCOME TO MEDIVICE==========\n")
un = input("Enter MYSQL username: ")
pw = input("Enter MYSQL password: ")
db = mysql.connector.connect(
host="localhost",
user=un,
password=pw
)
c = db.cursor()
c.execute("CREATE DATABASE IF NOT EXISTS medivice")
c.execute("USE medivice")
# Create table query
query = """
CREATE TABLE IF NOT EXISTS medicines (
SrNo int,
Medicines_Name varchar(100),
Diseases varchar(100),
MFG varchar(100),
EXP varchar(100),
Children varchar(100),
Adults varchar(100),
Price int
)
"""
c.execute(query)
db.commit()
db.close()
# Function to insert medicine
def insert_medicine():
db = mysql.connector.connect(
host="localhost",
user=un,
password=pw,
database="medivice"
)
c = db.cursor()
SrNo = int(input("Enter SrNo: "))
Medicines_Name = input("Enter medicine name: ")
Diseases = input("Enter diseases: ")
MFG = input("Enter MFG: ")
EXP = input("Enter EXP: ")
Children = input("Enter children dose: ")
Adults = input("Enter adults dose: ")
Price = int(input("Enter price: "))
query = """
INSERT INTO medicines (SrNo, Medicines_Name, Diseases, MFG, EXP, Children, Adults, Price)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
"""
c.execute(query, (SrNo, Medicines_Name, Diseases, MFG, EXP, Children, Adults, Price))
db.commit()
db.close()
print("Medicine inserted successfully!")
# Function to display medicines
def display_medicines():
db = mysql.connector.connect(
host="localhost",
user=un,
password=pw,
database="medivice"
)
c = db.cursor()
query = "SELECT * FROM medicines"
c.execute(query)
results = c.fetchall()
print("Medicines:")
for row in results:
print(f"SrNo: {row[0]}, Medicine Name: {row[1]}, Diseases: {row[2]}, MFG: {row[3]}, EXP: {row[4]}, Children: {row[5]}, Adults: {row[6]}, Price: {row[7]}")
db.close()
# Function to update medicine
def update_medicine():
db = mysql.connector.connect(
host="localhost",
user=un,
password=pw,
database="medivice"
)
c = db.cursor()
SrNo = int(input("Enter SrNo to update: "))
Medicines_Name = input("Enter new medicine name: ")
Diseases = input("Enter new diseases: ")
MFG = input("Enter new MFG: ")
EXP = input("Enter new EXP: ")
Children = input("Enter new children dose: ")
Adults = input("Enter new adults dose: ")
Price = int(input("Enter new price: "))
query = """
UPDATE medicines
SET Medicines_Name = %s, Diseases = %s, MFG = %s, EXP = %s, Children = %s, Adults = %s, Price = %s
WHERE SrNo = %s
"""
c.execute(query, (Medicines_Name, Diseases, MFG, EXP, Children, Adults, Price, SrNo))
db.commit()
db.close()
print("Medicine updated successfully!")
# Function to delete medicine
def delete_medicine():
db = mysql.connector.connect(
host="localhost",
user=un,
password=pw,
database="medivice"
)
c = db.cursor()
SrNo = int(input("Enter SrNo to delete: "))
query = "DELETE FROM medicines WHERE SrNo = %s"
c.execute(query, (SrNo,))
db.commit()
db.close()
print("Medicine deleted successfully!")
# Main function
def main():
dcr()
while True:
print("\n1. Insert Medicine")
print("2. Display Medicines")
print("3. Update Medicine")
print("4. Delete Medicine")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
insert_medicine()
elif choice == "
Upvotes: 0
Reputation: 50
MYSQL has nothing to do with python this is its own rdbms, first you need to download it with other stuff including MySQL Community Server (GPL) and run the server, when you setting it up thats when you set username and password. MAKE SURE your mysql is the same as your python editor i.e. 32bit or 64bit otherwise you wont be able to establish a connection (had that problem myself). But honestly best way would be watch a youtube video: https://www.youtube.com/watch?v=WuBcTJnIuzo
20 minutes and it explains everything pretty well
Upvotes: 1
Reputation: 96
Have you installed MySQL? If you have, then there you will have username and password. Once you have them, use them in:
conn = mysql.connector.connect(host='127.0.0.1',user=username, passwd=password,db=database_name)
Upvotes: 3