2

我有一点 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)

打印语句返回:

    []  <type 'str'>
    []  <type 'list'>

但是,在您运行以下 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']

再次检查表,打印语句返回:

 null  <type 'str'>
 None  <type 'NoneType'>

我不知道它为什么这样做......我非常感谢任何帮助!

4

1 回答 1

0

python脚本有一个问题,它说:

    #Updating the list
    clicked_id = clicked_id.append(product_id)

这是不正确的,并且给 clicked_id 一个 NoneType,当加载到 MySQL 时被解释为 null 值。

只需将代码更新为:

    clicked_id.append(product_id)
于 2013-01-08T15:59:09.230 回答