Python Forum
Problem with saving data and loading data to mysql
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with saving data and loading data to mysql
#1
Hi all,

I have problems with the saving my data to mysql and loading it back again from mysql.
I have to load the data first with pandas from a csv file, and that part works fine.
But after when i tried to save the data to mysql, only the table is created but the data is not saved.

Below is the code.

Appreicate your kind help. Thanks.

import numpy as np
import matplotlib.pyplot as plt
import mysql.connector,sys, pandas as pd
from datetime import date, datetime, timedelta
from dateutil.parser import parse
from bokeh.io import output_notebook
from bokeh.plotting import figure, show
from sqlalchemy import create_engine
engine = create_engine('sqlite://', echo=False)
from pandas.io import sql

### Read the hdb resale price index csv file with the pandas readcsv() function
hdbrpi = pd.read_csv('housing-and-development-board-resale-price-index-1q2009-100-quarterly.csv', 
                   index_col=None, delimiter=',')
user,pw, host,db = 'root','S8534107c!','127.0.0.1','mydatabase'
cnx = mysql.connector.connect(user=user, password=pw, host=host, database=db)
cursor = cnx.cursor()
print("Successfully loaded dataset housing-and-development-board-resale-price-index-1q2009-100-quarterly.csv from sql database")
print()

query_for_creating_database = 'CREATE DATABASE mydatabase'
query_for_creating_table = ("CREATE TABLE `table_hdbrpi` ("
    "`quarter` varchar(10) NOT NULL,"
    "`index` int(11) NOT NULL,"
    "PRIMARY KEY (`quarter`)"
    ") ENGINE=InnoDB")

try:
#     cursor.execute(query_for_creating_database)
#     cursor.execute(query_for_creating_table)
    
    hdbrpi.to_sql(name='table_hdbrpi', con=engine, if_exists='replace', index=False)
    cnx.commit()
    print("Data saved!")
    #engine.execute("SELECT * FROM table_hdbrpi").fetchall()
    pull_hdbrpi = pd.read_sql("SELECT * FROM table_hdbrpi;", engine)
    print("Data loaded!")

except:
   print("Unexpected error:", sys.exc_info()[0])
   print("Unexpected error:", sys.exc_info()[1])
   print("Unexpected error:", sys.exc_info()[2])
   exit()

finally:
  cursor.close()
  cnx.close()
Reply
#2
Does hdbrpi.to_sql only create insert statement
** guessing **
cur = cnx.cursor()
query = hdbrpi.to_sql(name='table_hdbrpi', con=engine, if_exists='replace', index=False)
cur.execute(cnx)
Reply
#3
Hi Larz60+,

Thanks for the reply. I hoped that line of code would have saved the data from the csv file to the mysql database. However it did not work, it might be connection problem which i copy from elsewhere or i have missing code.

i will try your 3 lines of code first.
Reply
#4
Something is wrong with the code above. First of all, one can see a lot of raw sql queries. In the same time
SqlAlchemy is an ORM, ORM is used to avoid such queries.

In line 9 you are creating sqlite engine, but you are talking about mysql connection!?
How is cnx related with engine? (line 33)
Reply
#5
Hi,

I have actually copied and pasted different codes here and there, hence the mess.

I know that the sql engine shouldnt be there but if i take it out i dont know what else to replace it.

Hi,

Ok you were right i dont even need that sqllite code, i just commented it.

I try to figure out the rest of error why the cnx doesnt connect to the database and save data.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with calculation of market data MrQuant1 0 603 Jan-12-2024, 12:38 AM
Last Post: MrQuant1
  Help: Conversion of Electricity Data into Time Series Data SmallGuy 3 1,235 Oct-04-2023, 03:31 PM
Last Post: deanhystad
  loading data astral_travel 7 3,766 Dec-12-2022, 03:29 PM
Last Post: astral_travel
  Saving data into xlxs colomwise erdemath 0 1,407 Oct-02-2021, 09:01 AM
Last Post: erdemath
  Using MySQL database images or data dumps Planetary_Assault_Systems 1 1,812 Jul-20-2021, 04:40 PM
Last Post: Larz60+
  Mysql and data manipulation disloyalzeus 5 58,270 Dec-20-2019, 08:22 AM
Last Post: buran
  Numpy saving and loading introduces zeros in the middle of every element DreamingInsanity 0 1,440 Dec-11-2019, 07:21 PM
Last Post: DreamingInsanity
  How to add data to the categorical index of dataframe as data arrives? AlekseyPython 1 2,354 Oct-16-2019, 06:26 AM
Last Post: AlekseyPython
  Problem with masking precipitation data into a shapefile atrwmb109 0 1,901 Jul-22-2019, 05:10 PM
Last Post: atrwmb109
  Loading .csv data using Pandas zaki424160 1 2,910 Jul-15-2019, 09:48 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020