I'm trying to insert data into sql table whose datatype is of pandas.series.SO for which it gives me error ProgrammingError: ('Invalid parameter type. param-index=1 param-type=Series', 'HY105')
Here is the code snippet
import pandas as pd
from pandas.io.json import json_normalize
import pyodbc
import json
import numpy as np
cnxn = pyodbc.connect('server connection')
cursor = cnxn.cursor()
cursor.execute("select GEOCODE_ID, JSON from GEOCODE_TBL where JSON is NOT NULL AND GEOCODE_ID = 20")
ID=[]
JSON=""
for row in cursor.fetchall():
ID.append(row[0])
JSON=row[1]
data = json.loads(JSON)
result = json_normalize(data,'results')
def get_cols(st):
pol = []
for i in result['address_components'].apply(json_normalize):
pol.append(','.join(i.apply(lambda x : x['long_name'] if st in x['types'] else np.nan,1).dropna()))
return pol
result['country'] = get_cols('country')
result['room'] = get_cols('room')
result['premise'] = get_cols('premise')
sql = "UPDATE OTH_TBL SET GEOCODE_ID=?,country=?,room=?,premise=? where GEOCODE_ID=?"
param= (
int(ID[0]),
result['country'],
result['room'],
result['premise'],
int(ID[0])
)
cursor.execute(sql, params)
the type(result['country'])
is pandas.core.series.Series AND type(ID[0])
is int
result['country']
Out[142]:
0 India
1 India
2 India
3 India
4 India
5 India
6 India
7 India
8 India
9 India
Name: country, dtype: object
Does anyone has any idea about how could it be done.
Thanks..
Domnick
The DataFrame.to_sql command should solve your problem
documentation here:
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_sql.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.