I use Dateframe to_sql to write records to a postgresql DB 9.6 , It can create the table 'Test_stock_basic_Info_2' ,but the records cannot be wrote into the table 'Test_stock_basic_Info_2'
thanks for your help .
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 ...')