我正在尝试更新基于2个功能选择的单元格。一个函数随机选择行,另一个函数随机选择列。单元格可能保留的任何值都必须替换为NULL
。 10%的行中必须有一个NULL
单元格。
代码如下:
import random as rdm
#decide which random rows will be empty
def select_empty_rows(n,empty_rows=[]):
for i in range(math.ceil(0.1*(n+1))):
empty_rows.append(rdm.choice(range(n)))
return empty_rows
#decide which random columns will be empty
def select_empty_col(col_list=[]):
empty_col=rdm.choice(col_list)
return empty_col
#data entry
def data_entry_pkg_info(n):
query="insert into pkg_info(weight,dimensions,latitude,longitude,isPerishable,isFragile,isHazardous,isDeliverable) values(%s,%s,%s,%s,%s,%s,%s,%s)"
query2="update pkg_info set %s=NULL where pkg_id=%s"
empty_rows=[]
col_list=['weight','dimensions','latitude','longitude','isPerishable','isFragile','isHazardous','isDeliverable']
select_empty_rows(n,empty_rows)
cell=''
for i in range(n):
entry=(rdm.uniform(0.00,100.00),rdm.uniform(0.00,100.00),
rdm.uniform(-90.00000,90.00000),
rdm.uniform(-180.00000,180.00000),rdm.choice([True,False]),
rdm.choice([True,False]),rdm.choice([True,False]),
rdm.choice([True,False]))
cur.execute(query,entry)
if i in empty_rows:
cols=select_empty_col(col_list)
if cell not in cols:
entry=(rdm.uniform(0.00,100.00),rdm.uniform(0.00,100.00),
rdm.uniform(-90.00000,90.00000),
rdm.uniform(-180.00000,180.00000),rdm.choice([True,False]),
rdm.choice([True,False]),rdm.choice([True,False]),
rdm.choice([True,False]))
cur.execute(query,entry)
else:
entry=(cols,empty_rows[i])
cur.execute(query2,entry)
print(entry)
确切的错误如下:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''isDeliverable'=NULL where pkg_id=8' at line 1
我怀疑问题在于列名(在这种情况下为isDeliverable)用引号引起来。欢迎其他可能的问题和解决方案。
谢谢大家!