我想使用python从sql数据库中插入,删除和修改数据。我设法连接数据库并输入值,但是即使插入不存在的数字(“ pacienti”表中总共有25个条目),也会出现“重复主键”错误。我想修复该错误,但我不知道如何解决。我还希望能够基于主键(Nr_Fisa)修改现有行并删除其中一行。我该怎么办?
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="spital")
#Afiseaza numarul pacientilor cu varsta cuprinsa intre 20 si 40 ani
print("Numarul pacientilor cu varsta cuprinsa intre 20 si 40 de ani")
mycursor = mydb.cursor()
sql = "SELECT COUNT(*) as count FROM pacienti WHERE Varsta >'20' AND Varsta <'40'"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for a in myresult:
print(a)
# Afiseaza pacientii cu hipertensiune
print("\n Pacientii cu Hipertensiune")
mycursor = mydb.cursor()
sql = "SELECT COUNT(*) as count FROM pacienti WHERE Diagnostic ='Hipertensiune'"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for b in myresult:
print(b)
# Afiseaza cea mai frecventa boala pentru grupa A
print("\n Cea mai frecventa boala pentru grupa A")
mycursor = mydb.cursor()
sql = "SELECT Diagnostic, COUNT(Diagnostic) AS value_occurrence FROM pacienti \
WHERE Grupa_Sanguina = 'A' GROUP BY Diagnostic ORDER BY value_occurrence DESC LIMIT 1"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
# Afiseaza cea mai frecventa boala pentru grupa B
print("\n Cea mai frecventa boala pentru grupa B")
mycursor = mydb.cursor()
sql = "SELECT Diagnostic, COUNT(Diagnostic) AS value_occurrence FROM pacienti \
WHERE Grupa_Sanguina = 'B' GROUP BY Diagnostic ORDER BY value_occurrence DESC LIMIT 1"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for y in myresult:
print(y)
# Afiseaza cea mai frecventa boala pentru grupa AB
print("\n Cea mai frecventa boala pentru grupa AB")
mycursor = mydb.cursor()
sql = "SELECT Diagnostic, COUNT(Diagnostic) AS value_occurrence FROM pacienti \
WHERE Grupa_Sanguina = 'AB' GROUP BY Diagnostic ORDER BY value_occurrence DESC LIMIT 1"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for z in myresult:
print(z)
# Afiseaza cea mai frecventa boala pentru grupa 0
print("\n Cea mai frecventa boala pentru grupa 0")
mycursor = mydb.cursor()
sql = "SELECT Diagnostic, COUNT(Diagnostic) AS value_occurrence FROM pacienti \
WHERE Grupa_Sanguina = '0' GROUP BY Diagnostic ORDER BY value_occurrence DESC LIMIT 1"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for q in myresult:
print(q)
# Numere si nr cu ale pacientilor care sunt singurii cu o boala
print("\n Pacientii care sunt singurii cu o boala")
mycursor = mydb.cursor()
sql = "SELECT Nr_fisa, Nume_Prenume FROM pacienti \
GROUP BY Diagnostic HAVING COUNT(*)=1"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for d in myresult:
print(d)
Nr_Fisa = input('Numar Fisa: ')
Nume_Prenume = input('Nume si Prenume: ')
Varsta = input('Varsta: ')
Grupa_Sanguina = input('Grupa Sanguina: ')
Diagnostic = input('Diagnostic: ')
mycursor.execute ("""UPDATE pacienti SET Nr_Fisa=%s, Nume_Prenume=%s, Varsta=%s, Grupa_Sanguina=%s, Diagnostic=%s """, (Nr_Fisa, Nume_Prenume, Varsta, Grupa_Sanguina, Diagnostic))