使用Python将CSV数据加载到MySQL中,创建表并添加记录

时间:2019-02-11 22:34:15

标签: python mysql mysql-connector-python

意外错误:<class 'NameError'>

我的代码用于创建表并从csv添加记录,不确定如何纠正错误,我应该使用与csv中相同的变量吗?

code

csv file

1 个答案:

答案 0 :(得分:0)

我也从来没有直接使用过MySQL连接器。现在已经安装了,尝试使用sqlalchemy和pandas的组合。熊猫可以为您创建表,这将大大减少您的代码。

import sqlalchemy
import pandas as pd

# MySQL database connection
engine_stmt = 'mysql+mysqldb://%s:%s@%s:3306/%s' % (username, password,
                                                    server,database)
engine = sqlalchemy.create_engine(engine_stmt)

# get your data into pandas
df = pd.read_csv("file/location/name.csv")

# adjust your dataframe as you want it to look in the database
df = df.rename(columns={0: 'yearquarter', 1: 'sms_volumes')
# using your existing function to assign start/end row by row
for index, row in df.iterrows():
    dt_start, dt_end = getdatesfromquarteryear(row['yearquarter'])
    df.loc[index, 'sms_start_date'] = dt_start
    df.loc[index, 'sms_end_date'] = dt_end

# write the entire dataframe to database
df.to_sql(name='sms_volumes', con=engine,
          if_exists='append', index=False, chunksize=1000)
print('All data inserted!')

与read_csv()类似,Pandas可以使将表中的数据轻松返回到数据框中:

# create a new dataframe from your existing table
new_df = pd.read_sql("SELECT * FROM sms_volumes", engine)