簡體   English   中英

使用python在mysql表中插入JSON數組

[英]Insert JSON array in mysql table using python

在mysql表“ section”中,我有5列(1個int,1個文本和3個json數組)。 JSON數組值以變量's','l'和't'的形式出現。 我無法在表中插入值。 我的代碼如下:

mycursor.execute("""CREATE TABLE section (tid int, text text, spots json, spotl json, terms json)""")
s = json.dumps(["s1", "s2 s3"], ensure_ascii=False)
l = json.dumps(["L1", "L2"], ensure_ascii=False)
t = json.dumps(["t1", "t2"], ensure_ascii=False)
mycursor.execute("""INSERT INTO section (tid, text, spots, spotl, terms) VALUES ('101', 'pageview', ?, ?, ?)""", (s, l, t),)
print(mycursor.rowcount, " record inserted")
mycursor.execute("""SELECT * FROM section""")
x = mycursor.fetchall()
sp = x[0][2]
print(type(sp))

該行的錯誤是:“ mysql.connector.errors.ProgrammingError:SQL語句中未使用所有參數”:“ mycursor.execute(”“”“ INSERT INTO節(tid,文本,斑點,spotl,術語)”值('101','pageview',?,?,?)“”“,(s,l,t),)”。 請幫忙。

我已經編輯了答案,請嘗試以下操作:

mycursor.execute("""INSERT INTO section (tid, text, spots, spotl, terms) VALUES ('101', 'pageview', %s, %s, %s)""", (s, l, t))

您無法提取列,因為通過查看代碼,您尚未提交查詢 。在mycursor.execute(Insert)之后編寫commit語句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM