Just a quick question guys: I created a darabase in mysql (opinions):
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| opinions |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.06 sec)
I also have a pandas dataframe df:
import pandas as pd
df = pd.read_csv('file.csv', sep='|', names=['id', 'opinions'])
df.head(3)
Out:
id content
0 id1 'blablababla
1 id2 'blalbabla
...
n idn 'blalbabl
Then, I would like to load df as a table (items) into opinions database. This is what I tried:
import mysql.connector
from sqlalchemy import create_engine
engine = create_engine('mysql+mysqlconnector://user:password@localhost:3306/opinions', echo=False)
df.to_sql(name='items', con=engine, if_exists = 'replace', index=False)
df
However, I got this exception:
OperationalError: (mysql.connector.errors.OperationalError) 2055: Lost connection to MySQL server at 'localhost:3306', system error: 32 Broken pipe
How should I load or write as a table df into opinions database?
cnx = pymysql.connect(user='user', password='pw', host= '127.0.0.1', port=3306, db='mydb', autocommit=True) df=psql.read_sql_query('call sproc', con=cnx)