python mysql 返回空_MySQL更新单元格后返回空值

在Python脚本中,作者遇到一个问题:当尝试更新MySQL数据库中的json序列化字符串列后,查询返回空值(null)。文章描述了如何查询、更新含有id列表的特定ip地址行,并在更新后遇到数据丢失的情况。尽管更新和保存过程看似正常,但后续查询显示数据变为null。作者寻求帮助以解决这个问题。
摘要由CSDN通过智能技术生成

我有点MySQL的问题。我编写了一个python脚本来记录点击率并处理重定向。我有一个表,其中对于给定的ip地址,有多个列,其中包括一个包含id列表的列。如果用户从未单击过图像(该行仍将存在),则该单元格包含一个空列表。在

请注意,在表中更新之前,列表是使用json序列化为字符串的。我知道这并不总是一个好的做法,但对我们的应用程序似乎是有效的。在

因此,在用户单击图像之前,可以查询sql表:sql = """select clicked_id from `""" + DBTABLE2 + """`

where ip='%s'"""%(ip_address,)

cur.execute(sql)

row = cur.fetchone()

if row:

ci = row['clicked_id']

print ci, type(ci)

clicked_id=json.loads(ci)

print clicked_id, type(clicked_id)

打印语句返回:

^{pr2}$

但是,在运行以下python脚本(成功运行)之后,输出就完全不同了。下面是python代码:#connecting to the mysql table

con = mysql.connect(host=DBHOST, user=DBUSERNAME, passwd=DBPASSWORD,

db=DBDATABASE, cursorclass=DictCursor)

cur = con.cursor()

#save the click through

sql = """select clicked_id from `""" + DBTABLE2 + """`

where ip='%s'"""%(ip_address,)

cur.execute(sql)

row = cur.fetchone()

if row:

clicked_id = row['clicked_id']

#decoding the data

clicked_id = json.loads(clicked_id)

else:

clicked_id=[]

#Updating the list

clicked_id = clicked_id.append(product_id)

#Encoding the data

clicked_id = json.dumps(clicked_id)

#Updating the mysql database

cur.execute("""update `"""+DBTABLE2+"""` set clicked_id='%s' where ip='%s'"""%(clicked_id,ip_address,))

#Getting the dst url

sql = """select url from `""" + DBTABLE + """`

where id='%s'"""%(product_id,)

cur.execute(sql)

row = cur.fetchone()

if row:

url = row['url']

再次检查表时,print语句返回:null

None

我不知道它为什么要这么做。。。我非常感谢你的帮助!在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值