I am filling up SQL tables with data that I have retrieved from a Python program. I am using Visual Studio Code for the Python program and MySQL Workbench 8.0 for SQL. There are some errors in it that I cannot resolve.
Here is my code:
from gettext import install #Importing PyMySQL extension
import pymysqlcon = pymysql.Connect( #Establishing connectionhost = 'localhost', #port = 3306, #user = 'root', #password = 'Musa2014', #db = 'referees', #charset = 'utf8' #
)Ref_Info = input("Enter referee details: ") #First input statementRef_First_Name, Ref_Last_Name, Ref_Level = Ref_Info.split() #Establishing individual variablesRef_Info_Table = []RefID = 1 #Setting the value of the RefIDcur = con.cursor() #Setting up cursorwhile Ref_Info != 'Stop': #Creating loop to get referee information Ref_Info_Table.append(Ref_Info.split()) #Updating Ref_Info_Table with new referee dataprint(Ref_Info_Table) #Checking for bugs print(Ref_Info.split()) #print(RefID, Ref_Info) #print('Referee ID:', RefID) #print('Referee First Name:', Ref_First_Name) #print('Referee Last Name:', Ref_Last_Name) #print('Referee Level:', Ref_Level) #RefID = RefID + 1 #Increasing the value of RefIdRef_First_Name, Ref_Last_Name, Ref_Level = Ref_Info.split() sql_query1 = 'INSERT INTO ref_info VALUES(RefID, Ref_First_Name, Ref_Last_Name, Ref_Level)'sql_query2 = 'SELECT * FROM ref_info'cur.execute(sql_query1)cur.execute(sql_query2)Ref_Info = input("Enter referee details: ") #Establishing recurring input againsql_query3 = 'SELECT * FROM ref_info'
cur.execute(sql_query3)
data = cur.fetchall()
con.commit()
cur.close()
con.close()
Here is the output in the terminal with the errors.
Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
PS C:\Users\mahd_.vscode\Code Folders\Testing> & C:/Users/mahd_/AppData/Local/Microsoft/WindowsApps/python3.10.exe "c:/Users/mahd_/.vscode/Code Folders/Testing/connection.py"
PS C:\Users\mahd_.vscode\Code Folders\Testing> & C:/Users/mahd_/AppData/Local/Microsoft/WindowsApps/python3.10.exe "c:/Users/mahd_/.vscode/Code Folders/Testing/connection.py"
PS C:\Users\mahd_.vscode\Code Folders\Testing> & C:/Users/mahd_/AppData/Local/Microsoft/WindowsApps/python3.10.exe "c:/Users/mahd_/.vscode/Code Folders/Testing/connection.py"
PS C:\Users\mahd_.vscode\Code Folders\Testing> & C:/Users/mahd_/AppData/Local/Microsoft/WindowsApps/python3.10.exe "c:/Users/mahd_/.vscode/Code Folders/Testing/connection.py"
Enter referee details: Josh Allen 2
[['Zac', 'Constable', '3'], ['Josh', 'Allen', '2']]
['Josh', 'Allen', '2']
2 Josh Allen 2
Referee ID: 2
Referee First Name: Zac
Referee Last Name: Constable
Referee Level: 3
Traceback (most recent call last): File "c:\Users\mahd_.vscode\Code Folders\Testing\connection.py", line 37, in cur.execute(sql_query1)
File "C:\Users\mahd_\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pymysql\cursors.py", line 148, in execute result = self.query(query)
File "C:\Users\mahd\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pymysql\cursors.py", line 310, in query conn.query(q)
File "C:\Users\mahd\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pymysql\connections.py", line 548, in query self.affected_rows = self.read_query_result(unbuffered=unbuffered)
File "C:\Users\mahd\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pymysql\connections.py", line 775, in read_query_result result.read()
File "C:\Users\mahd\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pymysql\connections.py", line 1156, in read first_packet = self.connection.read_packet()
File "C:\Users\mahd\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pymysql\connections.py", line 725, in read_packet packet.raise_for_error()
File "C:\Users\mahd\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pymysql\protocol.py", line 221, in raise_for_error err.raise_mysql_exception(self.data)
File "C:\Users\mahd\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pymysql\err.py", line 143, in raise_mysql_exception raise errorclass(errno, errval)
pymysql.err.IntegrityError: (1062, "Duplicate entry '0' for key 'ref_info.PRIMARY'")
PS C:\Users\mahd.vscode\Code Folders\Testing>