我正在尝试连接到本地mariadb数据库并将一些数据插入表中。
表定义已经用:
创建CREATE TABLE asx_companies (stock_id INT AUTO_INCREMENT PRIMARY KEY, company_name TEXT,symbol CHAR(3),industry TEXT,yahoo_code CHAR(6));
我将数据框插入数据库的代码是:
from db import DB
import pandas as pd
import MySQLdb as mariadb
HOST = 'localhost'
USER = 'josh'
PASSWD = 'josh'
DATABASE = 'stock_db'
db_connection = mariadb.connect(host=HOST,user=USER,passwd=PASSWD,db=DATABASE)
cursor = db_connection.cursor()
cursor.execute('use stock_db')
cursor.execute('show tables')
prettyPrint(cursor.fetchall())
data = pd.read_csv("/Users/Joshua/Documents/asx_data/asx_companies2.csv")
data.to_sql(con=db_connection, name='asx_companies', if_exists='replace', flavor='mysql')
我得到的错误是从数据框插入以下内容时:
1-PAGE LIMITED,1PG,Software&服务,1PG.AX
ValueError:无效的MySQL标识符'1-PAGE LIMITED'