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


Messages In This Thread
Problem with saving data and loading data to mysql - by kirito85 - Feb-06-2019, 07:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with calculation of market data MrQuant1 0 631 Jan-12-2024, 12:38 AM
Last Post: MrQuant1
  Help: Conversion of Electricity Data into Time Series Data SmallGuy 3 1,275 Oct-04-2023, 03:31 PM
Last Post: deanhystad
  loading data astral_travel 7 3,854 Dec-12-2022, 03:29 PM
Last Post: astral_travel
  Saving data into xlxs colomwise erdemath 0 1,427 Oct-02-2021, 09:01 AM
Last Post: erdemath
  Using MySQL database images or data dumps Planetary_Assault_Systems 1 1,837 Jul-20-2021, 04:40 PM
Last Post: Larz60+
  Mysql and data manipulation disloyalzeus 5 62,992 Dec-20-2019, 08:22 AM
Last Post: buran
  Numpy saving and loading introduces zeros in the middle of every element DreamingInsanity 0 1,457 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,386 Oct-16-2019, 06:26 AM
Last Post: AlekseyPython
  Problem with masking precipitation data into a shapefile atrwmb109 0 1,924 Jul-22-2019, 05:10 PM
Last Post: atrwmb109
  Loading .csv data using Pandas zaki424160 1 2,948 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