【发布时间】:2020-09-26 00:39:28
【问题描述】:
使用 python 3 和 MySQL。我正在尝试发送从 python 获得的 API 数据并将其转储到 MySQL 中。很难通过这部分。我想在 python 中的 mySQL 中创建一个数据库,并创建一个填充这些列的表:
formatted_phone_number、姓名、网站
我不断收到多个错误,所以我删除了我拥有的内容并重新开始最后一部分。
这是我目前所拥有的:
import googlemaps
import json
import pprint
import xlsxwriter
import time
import mysql.connector
# Define the API Key.
API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXX' #I have the API key
# Define the Client
gmaps = googlemaps.Client(key = API_KEY)
# Do a simple nearby search where we specify the location
# in lat/lon format, along with a radius measured in meters
places_result = gmaps.places_nearby(location='39.9584061,-75.1465018', radius = 5000, open_now =False , type = 'restaurant')
time.sleep(3)
place_result = gmaps.places_nearby(page_token = places_result['next_page_token'])
stored_results = []
# loop through each of the places in the results, and get the place details.
for place in places_result['results']:
# define the place id, needed to get place details. Formatted as a string.
my_place_id = place['place_id']
# define the fields you would liked return. Formatted as a list.
my_fields = ['name','formatted_phone_number','website']
# make a request for the details.
places_details = gmaps.place(place_id= my_place_id , fields= my_fields)
# print the results of the details, returned as a dictionary.
pprint.pprint(places_details['result'])
# store the results in a list object.
stored_results.append(places_details['result'])
这是我搞砸的地方...我知道有一种方法可以自动创建这些表以适应 API 信息...
host="localhost",
user="root",
passwd="",
database="googledb"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE googledb");
mycursor.execute("CREATE TABLE rests (formatted_phone_number VARCHAR(255), name VARCHAR(255), website VARCHAR(255))")
sqlFormula = "INSERT INTO rests (formatted_phone_number, name, website) VALUES (%s, %s, %s)"
这是错误:
Traceback(最近一次调用最后一次): 文件“C:/Users/Dan/Desktop/GoogleAPI/GoogleAPImysql.py”,第 49 行,在 mydb = mysql.connector.connect( 文件“C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector__init__.py”,第 179 行,在连接中 返回 MySQLConnection(*args, **kwargs) init 中的文件“C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py”,第 95 行 self.connect(**kwargs) 文件“C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\abstracts.py”,第 716 行,在连接中 self._open_connection() _open_connection 中的文件“C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py”,第 208 行 self._do_auth(self._user, self._password, _do_auth 中的文件“C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py”,第 144 行 self._auth_switch_request(用户名,密码) _auth_switch_request 中的文件“C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py”,第 177 行 引发errors.get_exception(数据包) mysql.connector.errors.ProgrammingError: 1049 (42000): 未知数据库 'googledb'
【问题讨论】:
-
您发布的内容有什么问题?
-
请给minimal reproducible example说明具体问题。你是什么意思“似乎不能”;错误?意外的输出?
-
什么意思“搞砸了”。 实际发生了什么?
-
编辑了原始帖子以反映错误消息。抱歉,我现在脑子有病
-
您在连接信息中包含数据库,在 CREATE DATABASE 查询运行之前。
标签: mysql python-3.x