using list to input data using psycoyg2 python (with missing data)
Want to get data with some missing values from cvs into postgresql. I am happy to insert using strings. I would like a fast was to do this using list or something similer. I also need to check for missing values and substitute as I get an error other wise.
for w in csvReader:
if a ==0:
var = w
sql_table = 'CREATE TABLE TableName (%s)' % ','.join('%s VARCHAR(50)' % name for name in var)
dict_cur.execute(sql_table)
else:
colnum = 0
for col in w:
nms= col
print w, 'column'
#this is not correct
sql_insert = ('INSERT INTO TableName (%s) VALUES (%s)' % (','.join('%s' % name for name in var),','.join('%s' % nm for nm in nms)))
cursor.execute(sql_insert)
colnum += 1
print w
a = a + 1
error Traceback (most recent call last): File "/home/matthew/workspace/dfdsggd/try_db_thing.py", line 41, in cursor.execute(sql_insert) psycopg2.ProgrammingError: syntax error at or near "." LINE 1: ...ORDER_ADM,MONTH_ADM,DAY_ADM,YEAR_ADM) VALUES (2,6,7,.,3,5,7) 开发者_C百科 ^
Looking at the error message, it just looks like you are trying to insert a '.' just read from csv in your database in the field ORDER_ADM without putting it between quotes.
Also the three fileds DAY_ADM, MONTH_ADM, YEAR_ADM suggests that you probably should merge them into a date and put them in the same field of the database. But it means thinking somehow about structure of database (not making everything VARCHAR). Also creating table in the loop that reads data, even at first iteration is quite unusual.
精彩评论