Dan Severn
Dan Severn

Reputation: 23

Attemping to dump API info from python into mySQL

Using python 3 and MySQL.  I am trying to send the API data that I got from python and dump it into MySQL.  Having a really hard time getting past this part.  I want to Create a database in mySQL in python and table that populates  these columns:   

formatted_phone_number, name, website

I keep getting multiple errors so I deleted what I had and starting the last part fresh.

This is what I have so far:

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'])

Here is where I'm messing up... I know there is a way to automatically create these tables to fit the API info...

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

This is the error:

Traceback (most recent call last): File "C:/Users/Dan/Desktop/GoogleAPI/GoogleAPImysql.py", line 49, in mydb = mysql.connector.connect( File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector__init__.py", line 179, in connect return MySQLConnection(*args, **kwargs) File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py", line 95, in init self.connect(**kwargs) File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\abstracts.py", line 716, in connect self._open_connection() File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py", line 208, in _open_connection self._do_auth(self._user, self._password, File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py", line 144, in _do_auth self._auth_switch_request(username, password) File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py", line 177, in _auth_switch_request raise errors.get_exception(packet) mysql.connector.errors.ProgrammingError: 1049 (42000): Unknown database 'googledb'

Upvotes: 0

Views: 62

Answers (1)

Sam Marvasti
Sam Marvasti

Reputation: 118

Where do you define myDb variable ? Have you specified the correct configuration for the MySQL instance. Check IP, check passwords?

The code you are using shows Google API usage. Please include a stacktrace of the error you are seeing from the Python interpreter.

Upvotes: -1

Related Questions