简体   繁体   English

sqlite3 OperationalError:靠近“m”:语法错误

[英]sqlite3 OperationalError: near "m": syntax error

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.我正在使用 pandas 导入 a.csv 文件,并使用 sqlite3 将数据输入到由 SQL 脚本创建的数据库中。 Image of SQL script SQL 脚本的图像

You can directly use to_sql :您可以直接使用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)

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

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