-1

I have code:

with open('D:\\Downloads\\Train_rev1.csv', 'rb') as csv_file:
    data = csv.reader(csv_file, delimiter=',')
    data.next()
    records = ",".join('({}, {})'.format(row[0], row[1]) for row in data)
print 'inserting records into {} ...'.format('train_rev1')
result = conn.execute("INSERT INTO train_rev1 (id, title) VALUES {}".format(records))
print 'Records inserted into {} {}'.format('train_rev1', 'successful' if result else 'unsuccessful')

and I got

> inserting records into train_rev1 ... Traceback (most recent call
> last):   File "I:\test\IceSugar\test.py", line 81, in <module>
>     insert_trainRev1Records(conn)   File "I:\test\IceSugar\test.py", line 54, in insert_trainRev1Records
>     result = conn.execute("INSERT INTO train_rev1 (id, title) VALUES {}".format(records))   File
> "C:\Python27\lib\site-packages\sqlalchemy-0.7.9-py2.7.egg\sqlalchemy\engine\base.py",
> line 1449, in execute
>     params)   File "C:\Python27\lib\site-packages\sqlalchemy-0.7.9-py2.7.egg\sqlalchemy\engine\base.py",
> line 1628, in _execute_text
>     statement, parameters   File "C:\Python27\lib\site-packages\sqlalchemy-0.7.9-py2.7.egg\sqlalchemy\engine\base.py",
> line 1691, in _execute_context
>     context)   File "C:\Python27\lib\site-packages\sqlalchemy-0.7.9-py2.7.egg\sqlalchemy\engine\default.py",
> line 331, in do_execute
>     cursor.execute(statement, parameters)   File "C:\Python27\lib\site-packages\mysql_python-1.2.4c1-py2.7-win32.egg\MySQLdb\cursors.py",
> line 183, in execute
>     query = query % db.literal(args) TypeError: not enough arguments for format string

I have no idea, what is wrong?

10
  • 2
    That code won't ever raise that error. Is that your exact code? At best I'd expect an IndexError instead. Commented Mar 8, 2013 at 17:07
  • 2
    Please post the full traceback (and more code). Commented Mar 8, 2013 at 17:07
  • if data is correct this should work, something like [[1,2],[3,4]]? Commented Mar 8, 2013 at 17:12
  • 1
    Okay, this is a totally different matter. You are feeding unescaped titles into your database, confusing it greatly. Do your titles have question marks? To write write correct code we'll need to know what database you are using. Commented Mar 8, 2013 at 17:24
  • 1
    Congratulations, @DavidSilva, you have discovered the SQL injection attack. Commented Mar 8, 2013 at 18:10

1 Answer 1

1

Your code seems fine to me:

>>> data = [[1,2],[3,4]]
>>> records = ",".join("({0}, {1})".format(row[0], row[1]) for row in data)
>>> records
'(1, 2),(3, 4)'

Assuming of course that data has the format I deduced.

So my guess is that you have a problem wit the format of your data object or you didn't post actual code.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.