使用 python 将 json 插入到 mysql
inserting json to mysql using python
您好,我可以查询但无法使用以下代码插入我的 json 字段,问题不是很棘手,但由于我是 mysql 的新手,无法弄清楚。 .每次我会得到一个变量调用 last_time 首先我需要插入然后从第二次我需要更新 last_time.. 如果我手动做我能够做并根据需要获得输出。 .喜欢下面的照片..
import pymysql.cursors
import datetime
import json
last_time='2344' #this value i will get output from my program
connection = pymysql.connect(host='localhost',
user='root',
password='Admin...',
db='cl......',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
print "connect successfull"
try:
with connection.cursor() as cursor:
sql = "INSERT INTO `tt_inv_refresh` (`cust_id`, `res_type`, `refresh_dtls`) VALUES (%s, %s, %s)"
cursor.execute(sql, ('2','elb','{"vot":[{"elb":"name1","last_refreshtime":last_time},{"elb":"name2","last_refreshtime":last_time}]}'))
connection.commit()
except Exception as e:
print str(e)
print "inserted"
finally:
connection.close()
如果有人指出我代码中的错误,将不胜感激...谢谢
您错过了 last_time
周围的报价
将cursore.execute
行更正为
cursor.execute(sql, ('2','elb','{"vot":
[{"elb":"name1","last_refreshtime":' + last_time + '},
{"elb":"name2","last_refreshtime":' + last_time+ '}]}'))
为避免将来出现此类问题,您可以考虑定义一个对象并使用 json.dumps
class Elb:
def toJSON(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=True, indent=4)
# you can use array or dict | I just copied from one of my code
# which reqd it to be an array
def mapper(self, detailArray = []): # you can use array or dict
self.elb = detailArray[0];
self.last_refreshtime = detailArray[1];
因此,在为实例设置数据后,
el_instance = Elb()
el_instance.mapper(<array-with-data>)
您可以在任何地方调用el_instance.toJSON()
来获取序列化数据。
您好,我可以查询但无法使用以下代码插入我的 json 字段,问题不是很棘手,但由于我是 mysql 的新手,无法弄清楚。 .每次我会得到一个变量调用 last_time 首先我需要插入然后从第二次我需要更新 last_time.. 如果我手动做我能够做并根据需要获得输出。 .喜欢下面的照片..
import pymysql.cursors
import datetime
import json
last_time='2344' #this value i will get output from my program
connection = pymysql.connect(host='localhost',
user='root',
password='Admin...',
db='cl......',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
print "connect successfull"
try:
with connection.cursor() as cursor:
sql = "INSERT INTO `tt_inv_refresh` (`cust_id`, `res_type`, `refresh_dtls`) VALUES (%s, %s, %s)"
cursor.execute(sql, ('2','elb','{"vot":[{"elb":"name1","last_refreshtime":last_time},{"elb":"name2","last_refreshtime":last_time}]}'))
connection.commit()
except Exception as e:
print str(e)
print "inserted"
finally:
connection.close()
如果有人指出我代码中的错误,将不胜感激...谢谢
您错过了 last_time
将cursore.execute
行更正为
cursor.execute(sql, ('2','elb','{"vot":
[{"elb":"name1","last_refreshtime":' + last_time + '},
{"elb":"name2","last_refreshtime":' + last_time+ '}]}'))
为避免将来出现此类问题,您可以考虑定义一个对象并使用 json.dumps
class Elb:
def toJSON(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=True, indent=4)
# you can use array or dict | I just copied from one of my code
# which reqd it to be an array
def mapper(self, detailArray = []): # you can use array or dict
self.elb = detailArray[0];
self.last_refreshtime = detailArray[1];
因此,在为实例设置数据后,
el_instance = Elb()
el_instance.mapper(<array-with-data>)
您可以在任何地方调用el_instance.toJSON()
来获取序列化数据。