使用phpmyadmin,加载一个mysql数据库,并尝试使用python修改db

iklwldmw  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(313)

我想:
更新数据,使marriage=2(单身)和marriage=3(其他)合并为2(单身)。以及:
删除所有帐单金额值为负的数据记录(在帐单金额1到帐单金额6中的任何一个中)
问题是,我运行代码,得到所有明确的消息,检查数据库,似乎什么都没有改变#1可能正在工作,但我不知道如何使用所有行运行它,2当我回顾数据库时,似乎什么都没有改变:

import csv

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  database="hw6"
)

mycursor = mydb.cursor()

f = open("UCI_Credit_Card.csv")
for row in csv.reader(f):
    a= row[0]
    b= row[1]
    c= row[2]
    d= row[3]
    e= row[4]
    f= row[5]
    g= row[6]
    h= row[7]
    i= row[8]
    j= row[9]
    k= row[10]
    l= row[11]
    m= row[12]
    n= row[13]
    o= row[14]
    p= row[15]
    q= row[16]
    r= row[17]
    s= row[18]
    t= row[19]
    u= row[20]
    v= row[21]
    w= row[22]
    x= row[23]
    y= row[24]
    sql = "INSERT INTO customers (ID, LIMIT_BAL, SEX, EDUCATION, MARRIAGE, AGE, PAY_0, PAY_2, PAY_3, PAY_4, PAY_5, PAY_6, BILL_AMT1, BILL_AMT2, BILL_AMT3, BILL_AMT4, BILL_AMT5, BILL_AMT6, PAY_AMT1, PAY_AMT2, PAY_AMT3, PAY_AMT4, PAY_AMT5, PAY_AMT6, default_payment_next_month) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
    val = (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y)
    mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")

mycursor = mydb.cursor(buffered=True)
print("Before updating a record ")
sql_select_query = """select * from customers"""
mycursor.execute(sql_select_query)
record = mycursor.fetchone()
print(record)

# Update single record now

sql_update_query = """UPDATE CUSTOMERS SET SEX = 2 where SEX = 3"""
mycursor.execute(sql_update_query)
mydb.commit()
print("Record Updated successfully ")

print("After updating record ")
mycursor.execute(sql_select_query)
record = mycursor.fetchone()
print(record)

# Delete record now

mycursor = mydb.cursor(buffered=True)
sql_update_query = "DELETE FROM customers WHERE (BILL_AMT1<0)AND(BILL_AMT2<0)AND(BILL_AMT3<0)AND(BILL_AMT4<0)AND(BILL_AMT5<0)AND(BILL_AMT6<0) """
sql_delete_query = """select * from customers"""
mycursor.execute(sql_update_query)
mydb.commit()
print("Record Delete successfully ")```
vkc1a9a2

vkc1a9a21#

这个 UPDATE 声明看起来不错。
但说到第二个要求:
删除所有帐单金额值为负的数据记录(在帐单金额1到帐单金额6中的任何一个中)
想必,你想 or 不考虑条件而不是 anddelete 查询:

DELETE FROM customers 
WHERE  
    BILL_AMT1 < 0
    OR BILL_AMT2 < 0
    OR BILL_AMT3 < 0
    OR BILL_AMT4 < 0
    OR BILL_AMT5 < 0
    OR BILL_AMT6 < 0

相关问题