Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python将数据插入mysql表_Python_Mysql_Json_Database_Connect - Fatal编程技术网

使用python将数据插入mysql表

使用python将数据插入mysql表,python,mysql,json,database,connect,Python,Mysql,Json,Database,Connect,我是python语言的初学者,我试图使用python将json文件数据插入数据库表,但问题是我没有遇到任何错误: 推特号码49634正在上传到服务器 我不知道问题出在哪里,请告诉我,如果有任何帮助,我将不胜感激 import mysql.connector import json mydb = mysql.connector.connect(host='localhost', port='3306', user='root', password='nihad147', database='tw

我是python语言的初学者,我试图使用python将json文件数据插入数据库表,但问题是我没有遇到任何错误:

推特号码49634正在上传到服务器

我不知道问题出在哪里,请告诉我,如果有任何帮助,我将不胜感激

import mysql.connector
import json

mydb = mysql.connector.connect(host='localhost', port='3306', user='root', password='nihad147', database='tweets')
mycursor = mydb.cursor()
sql_request='insert ignore into tweet_location (latitude, longitude, tweet_id) values (%s,%s,%s)'""

myJsonFile = open('tweet.json', encoding="utf-8")
c = 0



for line in myJsonFile:
  c = c + 1
print("tweet number ", c, " is uploading to the server")
data = json.loads(line)
#line = line.replace('','')

tweet = "SELECT * FROM tweet WHERE tweet_id = '" + str(data['tweet_id']) + "'"
mycursor.execute(tweet)

myresult = mycursor.fetchall()

row_count = mycursor.rowcount
if row_count == 0:
 if 'location' in data.keys() and data['location'] != None and 'address' in data['location']:
  latitude = data ['location']['lat']
  longitude=data ['location']['lon']


mycursor.execute(sql_request, (latitude,longitude,data['tweet_id']))
print ('------------')


mydb.commit ()
下面是我的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"}
这是my table的属性推文: tweet_id, id\u用户, 文本, 推特位置, 创建于, 姓名屏幕,
categorie_id,

不要一次读取一行JSON文件。使用
json.load()
将整个文件加载到字典中

在选择tweet的查询中使用一个参数,而不是将
数据['tweet\u id']
连接到SQL中

插入新行的代码应该位于从数据中设置纬度和经度的所有
if
语句中。事实上,您最好将所有数据库代码放在
if
语句中,该语句检查
JSON
中是否设置了
location

import mysql.connector
import json

mydb = mysql.connector.connect(host='localhost', port='3306', user='root', password='nihad147', database='tweets')
mycursor = mydb.cursor()
sql_request='insert ignore into tweet_location (latitude, longitude, tweet_id) values (%s,%s,%s)'""

with open('tweet.json', encoding="utf-8") as myJsonFile:
    data = json.load(myJsonFile)

if data.get('location') and 'address' in data['location']:
    tweet = "SELECT 1 FROM tweet WHERE tweet_id = %s"
    mycursor.execute(tweet, (data['tweet_id'],))
    myresult = mycursor.fetchall()

    row_count = len(myresult)
    if row_count == 0:
        print(f"Inserting {data['tweet_id']} to the database");
        latitude = data['location']['lat']
        longitude = data['location']['lon']
        mycursor.execute(sql_request, (latitude,longitude,data['tweet_id']))
        mydb.commit ()
        print ('------------')
    else:
        print(f"Tweet {data['tweet_id']} is already in the database")

请使用适当的4个空格缩进,仅用1个空格阅读代码非常困难。为什么在
tweet
表中找不到tweet ID时,您只能从JSON获取
location