在pandas中插入SQL查询结果时,需要转义子查询的结果。

huangapple go评论72阅读模式
英文:

Escaping the result of a subquery in sql insert statements for pandas

问题

我正在使用Python和pandas在MariaDB上导入CSV,插入语句需要一些子查询,以下是代码:

empdata = pd.read_csv(
    "static/files/testfile.csv",
    index_col=False, delimiter=";", on_bad_lines="skip"
)

if conn.is_connected():
    cursor = conn.cursor()
    cursor.execute("select database();")
    record = cursor.fetchone()
    print("You're connected to database: ", record)
    for i, row in empdata.iterrows():
        sql = "INSERT INTO PRODOTTI (PROD_ATTIVO,EAN13,prod_nome,Prezzo,CAT_IVA_idCAT_IVA,Costo,Quantita,Fornitori_idFornitori,Data_ins) \
                VALUES (%s,%s,%s,%s,(select idCAT_IVA from CAT_IVA where CAT_IVA_aliquota = %s),%s,%s,(select idFornitori from Fornitori where Fornitori_nome = %s),%s)"

一切都正常,直到子查询的答案之一包含空格,然后我得到了这个错误:

(1, '3649833526', 'Example product', 15.9, 0.22, '8.94', '2', 'example dealer', '2021/01/02')
插入数据库时出错 1048 (23000):列 'Fornitori_idFornitori' 不能为空

对我来说似乎子查询只搜索 'example' 而不是 'example dealer',所以它得到一个空答案,我还没有找到一种方法来转义这个字段并避免这种行为。

有人知道如何解决这个问题吗?

谢谢。

我尝试了不同的CSV编码和在查询中进行转义,但没有结果。

英文:

I'm importing a csv on MariaDB using python & pandas,the isert statements need a couple of subqueries, here is the code:

empdata = pd.read_csv(
    "static/files/testfile.csv",
    index_col=False, delimiter=";", on_bad_lines="skip"
)


if conn.is_connected():
    cursor = conn.cursor()
    cursor.execute("select database();")
    record = cursor.fetchone()
    # print (record)
    print("You're connected to database: ", record)
    # loop through the data frame
    for i, row in empdata.iterrows():
        # print(row)
        # here %S means string values
        sql = "INSERT INTO PRODOTTI (PROD_ATTIVO,EAN13,prod_nome,Prezzo,CAT_IVA_idCAT_IVA,Costo,Quantita,Fornitori_idFornitori,Data_ins) \
                VALUES (%s,%s,%s,%s,(select idCAT_IVA from CAT_IVA where CAT_IVA_aliquota = %s),%s,%s,(select idFornitori from Fornitori where Fornitori_nome = %s),%s)"

and all works fine until one of the answer of a subuery contains a space than I got this error:

(1, '3649833526', 'Example product', 15.9, 0.22, '8.94', '2', 'example dealer', '2021/01/02')
Error while inserting in DB 1048 (23000): Column 'Fornitori_idFornitori' cannot be null

It seems to me that the subqueries search just for 'example' and not for 'example dealer' so it get a NULL answer, and I haven't found a way to escape this field and avoid this behaviour.

Some one knows how to solve this issue?

Thank you.

I've tried different csv encoding and escaping in the query but without result.

答案1

得分: 0

如您定义了Fornitori_idFornitoriNOT NULL,您需要为该列提供一个值,因为0不是自动递增的一部分,您可以使用0或-1。

sql = "INSERT INTO PRODOTTI (PROD_ATTIVO, EAN13, prod_nome, Prezzo, CAT_IVA_idCAT_IVA, Costo, Quantita, Fornitori_idFornitori, Data_ins) \
VALUES (%s, %s, %s, %s, COALESCE((SELECT idCAT_IVA FROM CAT_IVA WHERE CAT_IVA_aliquota = %s), 0), %s, %s, COALESCE((SELECT idFornitori FROM Fornitori WHERE Fornitori_nome = REPLACE(%s, ' ', '')), 0), %s)"
英文:

As you defined Fornitori_idFornitori as NOT NULL you need to provide a value for the column, as 0 isn't part if an auto_increment you can use it or -1

sql = "INSERT INTO PRODOTTI (PROD_ATTIVO,EAN13,prod_nome,Prezzo,CAT_IVA_idCAT_IVA,Costo,Quantita,Fornitori_idFornitori,Data_ins) \
VALUES (%s,%s,%s,%s,COALESCE((select idCAT_IVA from CAT_IVA where CAT_IVA_aliquota = %s),0),%s,%s,COALESCE((select idFornitori from Fornitori where Fornitori_nome = REPLACE(%s, ' ','')),0),%s)"

huangapple
  • 本文由 发表于 2023年7月20日 20:35:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定