简体   繁体   English

使用 python 将数据插入 mysql 表

[英]insert data into mysql table using python

im a beginner in python language, and im trying to insert my json file data into my database table using python but the problem is i have no errors i just get:我是 python 语言的初学者,我尝试使用 python 将我的 json 文件数据插入我的数据库表中,但问题是我没有错误,我刚刚得到:

tweet number 49634 is uploading to the server推文号 49634 正在上传到服务器

i don't get where the problem is, please i would appreciate any help我不知道问题出在哪里,我将不胜感激

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 ()

here's an example of my json file data:这是我的 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"}

and this my table's attributes tweet: tweet_id, id_user, text, tweet_location, created_at, name_screen, categorie_id,这是我表的属性 tweet: tweet_id, id_user, text, tweet_location, created_at, name_screen, categorie_id,

Don't read the JSON file one line at a time.不要一次读取 JSON 文件一行。 Use json.load() to load the entire file into a dictionary.使用json.load()将整个文件加载到字典中。

Use a parameter in the query that selects the tweet, rather that concatenating data['tweet_id'] into the SQL.在选择推文的查询中使用参数,而不是将data['tweet_id']连接到 SQL 中。

The code that inserts the new row should be inside all the if statements that set latitude and longitude from the data.插入新行的代码应该在所有从数据中设置latitudelongitudeif语句中。 In fact, you might as well put all the database code inside the if statement that checks whether location is set in the JSON .实际上,您不妨将所有数据库代码放在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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM