如何解决使用python将json数据插入到数据库表中
我试图将我的 json 文件数据插入到名为 location 的数据库表中,但无论我做什么它都不起作用,我一直在 mycursor.execute 中收到相同的错误,我是 Python 的初学者,所以我不知道很多 任何帮助将不胜感激,谢谢
但是现在我不断收到此错误,因为我将 tweet_id 约束设为外键,知道如何解决吗?
无法添加或更新子行:外键约束失败 (tweets.tweet_location,CONSTRAINT tweet_location_ibfk_1 FOREIGN KEY (tweet_id) REFERENCES tweet (tweet_id))
导入MysqL.connector 导入json
mydb = MysqL.connector.connect(host='localhost',port='3306',user='root',password='nihad147',database='tweets')
mycursor = mydb.cursor()
sql_location = """insert into tweet_location (
latitude,longitude,tweet_id,VALUES(%s,%s,%s)"""
myJsonFile = open('tweet.json',encoding="utf-8")
mycursor.execute("DELETE FROM tweet_location")
c = 0
for line in myJsonFile:
c = c + 1
print("tweet number ",c," is uploading to the server")
data = json.loads(line)
tweet = ("select * from tweet ")
mycursor.execute(tweet)
myresult = mycursor.fetchall()
row_count = len(myresult)
if row_count == 0:
val_location = (data['location']['lat'],data['location']['lon'],data['tweet_id'])
mycursor.execute(sql_location,val_location)
mydb.commit()
这是我不断收到的错误:
mycursor.execute(sql_location,val_location)
in execute self._handle_result(self._connection.cmd_query(stmt))
in cmd_query result = self._handle_result(self._send_cmd(ServerCmd.QUERY,query))
in _handle_result raise errors.get_exception(packet)
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 'VALUES(28.0000272,2.9999825,'1298045077621800960')' at line 5
我的json文件数据示例:
{
"tweet_id": "1261276320878788609","date": "Fri May 15 12:44:42 +0000 2020","raw_text": "برنامج وطني لدعم المبدعين في مواجهة #كورونا","geo_source": "user_location","location": {
"address": {
"country": "Tunisia","country_code": "tn","state_district": "غزالة","county": "العرب","state": "Bizerte"
},"response": "{'place_id': 235309103,'licence': 'Data © OpenStreetMap contributors,ODbL 1.0. https://osm.org/copyright','osm_type': 'relation','osm_id': 7124228,'boundingBox': ['37.105957','37.2033466','9.4739053','9.6124953'],'lat': '37.1551868','lon': '9.54834183807249','display_name': 'العرب,غزالة,Bizerte,Tunisia','class': 'boundary','type': 'administrative','importance': 0.45,'icon': '/data/nominatimimages/mapicons/poi_boundary_administrative.p.20.png','address':{'county': 'العرب','state_district': 'غزالة','state': 'Bizerte','country': 'Tunisia','country_code': 'tn'}}","geohash": "snwg37buskzd","query_term": "arab","lon": 9.54834183807249,"lat": 37.1551868
},"user_friends_count": 61,"user_description": "I love UAE and his great leadership","user_created_at": "Wed Oct 09 11:41:41 +0000 2013","user_screen_name": "SikandarMirani","user_id_str": "706377881","user_verified": false,"user_statuses_count": 50804,"user_followers_count": 946,"user_location": "dubai United arab Emirates"
}
我将我的数据库表声明为 tweet_id bigint / 纬度浮点数 / 经度浮点数
解决方法
日志消息指向 insert 语句中的语法错误。列列表应放在一对括号内。你需要更换
tweet_id,
与
tweet_id)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。