【问题标题】:insert json data into databse table using python使用python将json数据插入数据库表
【发布时间】:2021-06-04 12:46:16
【问题描述】:

我试图将我的 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 / 纬度浮点数 / 经度浮点数

【问题讨论】:

    标签: python mysql database connect


    【解决方案1】:

    日志消息指向 insert 语句中的语法错误。列列表应放在一对括号内。你需要更换

    tweet_id,
    

    tweet_id)
    

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 2013-03-25
      相关资源
      最近更新 更多