0
import pandas as pd
import sqlite3
df = pd.read_csv('liked_songs.csv')
!sqlite3 spotify.db < spotify.sql
connection = sqlite3.connect('spotify.db')
df.columns = df.columns.str.replace(' ','_')
cursor = connection.cursor()
for index in df.index:
 sr = df.iloc[index]
 cursor = cursor.execute(f"""INSERT INTO spotify (SpotifyID, ArtistID, Track_Name, 
 Album_Name, Artist_Name, Release_Date, Duration,Popularity, Genres)
 VALUES ('{sr.SpotifyID}', '{sr.Artist_ID}', '{sr.Track_Name}', 
 '{sr.Album_Name}', '{sr.Artist_Name}', '{sr.Release_Date}',
 '{sr.Duration}', '{sr.Popularity}', '{sr.Genres}')""")

I'm using pandas to import a .csv file and sqlite3 to enter data into a database made from a SQL script.Image of SQL script

asked Apr 24, 2022 at 5:12
1
  • 1
    Probably one of the values contains an embedded quote. This is why you should always use parameter subtitution when passing values to cursor.execute. Commented Apr 24, 2022 at 8:28

1 Answer 1

0

You can directly use to_sql:

import pandas as pd
import sqlite3
df = pd.read_csv('liked_songs.csv')
connection = sqlite3.connect('spotify.db')
df.columns = df.columns.str.replace(' ','_')
df.to_sql('spotify', connection, if_exists='replace', index=None)
answered Apr 24, 2022 at 5:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.