Need some debugging in my program: filling up SQL tables with data retrieved from a Python program

2024/11/15 6:14:17

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>

Answer

"Duplicate entry '0' for key 'ref_info.PRIMARY'" means that you try to insert a record in the ref_info table that already exists. A primary key is unique.

https://en.xdnf.cn/q/119737.html

Related Q&A

How do I create a magic square matrix using python

A basket is given to you in the shape of a matrix. If the size of the matrix is N x N then the range of number of eggs you can put in each slot of the basket is 1 to N2 . You task is to arrange the egg…

Ensuring same dimensions in Python

The dimensions of P is (2,3,3). But the dimensions of M is (3,3). How can I ensure that both P and M have the same dimensions i.e. (2,3,3). import numpy as np P=np.array([[[128.22918457, 168.52413295,…

how to stop tkinter timer function when i press button one more times?

id tried to use root.after_cancel(AFTER), but i dont know how.root.after_cancel(AFTER) AFTER = None def countdown(count,time,name):global AFTERtime[text] =name,":",datetime.fromtimestamp(cou…

Read csv into database SQLite3 ODO Python

I am trying to read in a csv into a new table in a new databased using ODO, SQLite3 and Python.I am following these guides:https://media.readthedocs.org/pdf/odo/latest/odo.pdf http://odo.pydata.org/en/…

netmiko cant execute sh run | i host

I notice that my netmiko code cant run sh run | i host which is a legitimate Cisco command.When I replace sh run with other command such as sh clo, or show ip interface brief, it works perfectly.from n…

How to dump the data from file to an excel sheet

I want to dump [3-4 lines together] some data to an excel sheet. I could able to dump single line based on some criteria [like if line is getting start with // or /* ], but in case of when lines starts…

I dont understand why my script is not iterating through all string.split elements?

The objective of this python exercise is to build a function that turns text into pig latin, a simple text transformation that modifies each word by moving the first character to the end and appending …

Unable to change the tick frequency on my chart

I have seen many questions on changing the tick frequency on SO, and that did help when I am building a line chart, but I have been struggling when its a bar chart. So below are my codes import numpy a…

Django Queryset foreign keys

I am trying to get a queryset but it is not displaying anything. Basically, I want to get the Asset objects that are assigned via foreign key to an employee, which is a foreign key of the signed in use…

How to reorder the columns of a CSV?

How can I re-order the columns of a CSV file using Python? These are the first rows of a CSV file I need to change:03;30269714;Ramiro Alberto;Nederz;active;pgc_gral 03;36185520;Andrea;Espare;active;pg…