Skip to content

pandas dataframe to_sql insert to postgresql not work  #18981

@teeger

Description

@teeger

def testSQL():
import psycopg2
import pandas as pd
from sqlalchemy import create_engine

try:    
    engine =  create_engine('postgresql+psycopg2://pub:pub@localhost/Wind_Quote') 
    
    #conn=engine.raw_connection()
    
    #cur = conn.cursor()

    df = pd.DataFrame()
   
    df['stock_code']='0000' 
    df['ipo_date']    ='2015-01-07' 
    df['sec_name'] ='11111111'
    df['trade_code']  ='22222222222'   
   
   
    try :
        df.to_sql(name='Test_stock_basic_Info_2', con=engine,if_exists='replace',index=False)       
        #conn.commit()   
        #cur.execute('COMMIT')
    except Exception as e:
        print ( time.strftime('At [%Y-%m-%d %H:%M:%S]', time.localtime(time.time())),": index_data: Exception :" , (e) )
        
except Exception as e:
    print ( time.strftime('At [%Y-%m-%d %H:%M:%S]', time.localtime(time.time())),": program: Exception :" , (e) )
 

print('Finished  ...')

##########################################################

I use Dateframe to_sql to write records to a postgresql DB , It can create the table 'Test_stock_basic_Info_2' ,but the records cannot be wrote into the table 'Test_stock_basic_Info_2'
no exception , is there any errors of the code ???
postgresql version 9.5.10-2-windows
thanks for your help .

Metadata

Metadata

Assignees

No one assigned

    Labels

    IO SQLto_sql, read_sql, read_sql_query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions