1

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?

7
  • 1
    your problem is with reading the database, not writing to it Commented Jun 7, 2016 at 5:36
  • Thanks for the help @PaulH, Could you provide an example of how to do it correctly?. Commented Jun 7, 2016 at 5:42
  • 1
    Maybe help PyMySQL and 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) Commented Jun 7, 2016 at 6:04
  • 1
    Hmmm, maybe help this. Commented Jun 7, 2016 at 6:15
  • 1
    Did you actually create a table in the database, If not, Then the problem is not pandas its your knowledge of databases in general, I would go learn about basic database use, then tackle the above problem. If you did did create table in MySQL, show the 'create table' sql above. Commented Jun 7, 2016 at 18:22

2 Answers 2

2

On your MySQL Server opinions is not a table, but a database (as show databases implies), which is basically a collection of tables. You can issue select statements only on tables however.

You can check which tables e.g. exist via SHOW TABLES IN opinions. It's also convenient to add the database as an argument to the connection:

config = {
    'user' : 'root',
    'passwd' : 'password',
    'host' : 'localhost',
    'raise_on_warnings' : True,
    'use_pure' : False,
    'database' : 'opinions'
    }
con = mysql.connector.connect(**config)

This results in all table references to be targeted at tables in the opinions database. A plain select on an existing table should then work:

data = pd.read_sql('SELECT * FROM <some_table_name>', con)
Sign up to request clarification or add additional context in comments.

4 Comments

This actually worked. However, I still can not load the dataframe to the database as a table, neither insert the dataframe to the table:Empty DataFrame Columns: [id, opinions] Index: []
I'm not really familiar with pandas - but did you actually retrieve any results with the read_sql call? Also: If my answer helped you out, please accept it :)
Thanks for the help sebastian, I will accept your answer. One more thing, I tried the following: df.to_sql(name = 'opinions2', con = con, flavor='mysql', schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None). However when I describe it with sql the table is empty... how should I load the dataframe to the database as a new table or overwrite it to an existing table?
Well if the table is empty you'll have to fill in some data first. You could e.g. create a dataframe with some arbitrary data (matching your table definition) and insert that into your database. Maybe you should also think about what you're actually trying to accomplish (reading data from sql and trying to insert it right back in doesn't make that much sense after all...)
1

Use Pandas to read from MySQL. MySQLdb to write to database.

  mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | opinions           |
    | performance_schema |
    | sys                |
    +--------------------+

Great! For your example, replace test with opinions

mysql> use test;
Reading table information for completion of table and column names

Database changed
mysql> show tables;
+-----------------+
| Tables_in_test |
+-----------------+
| _t              |
| food            |
| food_in         |
| food_min        |
| type            |
| user            |
+-----------------+
4 rows in set (0.00 sec)

mysql> select * from test.food;
+----+-------+--------------+--------+----------+
| ID | Cat   | Item         | price  | quantity |
+----+-------+--------------+--------+----------+
|  1 | Food  | Pizza        | 2.7500 |      300 |
|  2 | Liq   | Beer         | 2.5000 |      300 |
|  4 | Food  | Sandwich     | 4.0000 |      222 |
| 12 | Food  | Soup         | 3.5000 |      100 |
+----+-------+--------------+--------+----------+
4 rows in set (0.00 sec)

So:

data = pd.read_sql('SELECT * FROM opinions', con)

Turns into:

data = pd.read_sql('SELECT * FROM opinions.MYTABLETHATIWANT', con)

I use MySQLdb to write to database. So, I can use basic python and pandas. You may also have permission problems that inhibits database/table access. Are you using root, or another user and have set the permission for user.

6 Comments

Thanks for the help, Could you provide an example of how to write it?.
@ml_student write what?
A pandas dataframe df, assuming I all ready have one... Actually I allready have a dataframe with data... I just omitted it for space issues...
You are still not telling me what you would like to help you write? Update the question above
I see you are using 'root' on MySQL. Stop it!-- create a user and use that user. Did you read the docs on pandas writing to database. I dont use pandas to write to MySQL. Sorry, Not much help there --
|

Your Answer

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.