0

This is the script that I wrote so far. The first blocker that I find is that I am not being able to install MySQLdb package - Maybe I could use a different module?

import soundcloud
import pandas as pd 
from pandas import DataFrame
import MySQLdb
client = 
soundcloud.Client(client_id='696b5ca70f5401cc46c9011c78831877')
userId = '110652450'
tracks = client.get('/users/'+userId+'/tracks')
data = []
for x in tracks:
    data.append({'Track_Name':x.title,'plays':str(x.playback_count)}) 

df = pd.DataFrame(data)

database = MySQLdb.connect (host="127.0.0.1",user ="root",passwd="XXX",db="soundcloudstore")
cursor = database.cursor()
query = """INSERT INTO Tracks (Track_Name, Plays) VALUES (%s,%s)"""

for x in df:
    Track_Name = df[['Track_Name']].value 
    Plays = df[['plays']].value 

    values = (Track_Name, Plays)
    cursor.execute(query, values)

cursor.close() 

database.commit()

database.close()
6
  • stackoverflow.com/questions/25865270/… see this to install mysql Commented Nov 26, 2017 at 23:12
  • Thanks Salmaan, but if I install brew install mysql-connector-c (which works) still doesn't allow me to use the module 'MySQLdb'. Would you recommend a different module perhaps? Commented Nov 26, 2017 at 23:20
  • did you import it using this import mysql.connector If it doesnt work, you can try the other ones. Commented Nov 26, 2017 at 23:23
  • I am not sure if I understood what you said but I also tried: pip install mysql.connector and I got the message 'Could not find a version that satisfies the requirement mysql.connector (from versions: ) No matching distribution found for mysql.connector' Commented Nov 26, 2017 at 23:31
  • dev.mysql.com/doc/connector-python/en/… Commented Nov 26, 2017 at 23:40

1 Answer 1

0

Download the adapter here: https://dev.mysql.com/doc/connector-python/en/connector-python-installation.html

Then you would use it as so:

import mysql.connector

data = []
for x in tracks:
    data.append((x.title, str(x.playback_count))) 

conn = mysql.connector.connect(user='', password='',
                              host='',
                              database='')

cursor = conn.cursor()

q = """INSERT INTO Tracks (Track_Name, Plays) VALUES (%s,%s)"""

cursor.executemany(q, data)

This will save you from loading into a dataframe for no reason and executemany is optimized for inserts.

Sign up to request clarification or add additional context in comments.

13 Comments

Hi Eagle, Thanks. If I ran the script above I see the error 'invalid syntax' which I think is in the append section. Do you know how could I fix it?
I just edited you might have been using what I posted prior to the edit
Ah thanks! Now I see the 'old' error message: No module named 'mysql' Any thoughts?
yea it seems mysql isnt in your path
what does it mean not being in my path? Do I need to install another module?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.