问题: 当我的csv datefield =' ',mysql服务器将日期转换为1900,01,01。
我想要的是什么: 我想动态评估我是否要发送过来''对于日期,而是发送NULL或None。
编辑:根据beargle的推荐,我试过pypyodbc写了这个疯狂的代码......而且它似乎有效。
我创建了一个列表,其中列是日期, 逐项逐项,评估是否应为无。
请告诉我你是否有更好的想法!
import pypyodbc as pyodbc
def sendTOSQL(csvLocation,sqlTableName):
x=0
with open (csvLocation, 'r') as f:
cnxn = pyodbc.connect('DSN=dashboarddatadev')
cursor = cnxn.cursor()
reader = csv.reader(f)
list=[]
cursor.execute('select * from %s' % sqlTableName)
for i in cursor.description:
if i[1].__name__=='datetime':
list.append(1)
else:
list.append(0)
cnxn2 = pyodbc.connect('DSN=dashboarddatadev')
cursor = cnxn2.cursor()
reader = csv.reader(f)
columns = next(reader)
print columns
for data in reader:
changeNulls=zip(data,list)
y=0
for item in changeNulls:
if item[1]==1 and item[0]==' ':
data[y]=None
y=y+1
query = 'insert into {0}({1}) values ({2})'
query = query.format(sqlTableName,','.join(columns), ','.join('?' * len(columns)))
itemchange=re.compile(re.escape('case,'),re.IGNORECASE)
query=itemchange.sub('"case",',query)
itemchange=re.compile(re.escape('full,'),re.IGNORECASE)
query=itemchange.sub('"full",',query)
cursor.execute(query, data)
if x % 1000000==0:
if len(str(x)) > 7:
xstar=str(x)[0:2]
else:
xstar=str(x)[0]
if xstar=='0':
logging.debug(r'processing and counting rows to a million' )
else:
logging.debug(r'%s,000,000 rows processed and counting' % (xstar))
x=x+1
cursor.commit()