Import a textfile to MS SQL with python

Tim Golden tim.golden at viacom-outdoor.co.uk
Wed Sep 6 09:09:48 EDT 2006


[joel.sjoo at gmail.com]

| i gott the same results with both executemany and execute. i will try
| with some other sql modules. if you try tim so let me now if 
| you cot it
| to work.

OK, the relevant thing here is the paramstyle. When I made that
misguided claim earlier that "?" was the most common parameter
character, I didn't check whether pymssql actually uses it. And
it doesn't.

<code>
import pymssql
print pymssql.paramstyle
# pyformat

</code>

This means that you are expected to put %s everywhere you
want a substitution from your parameter list. Again, don't
bother quoting for strings etc. Therefore, the following 
works for this db-module:

<code>
import pymssql
db = pymssql.connect (...)

#
# Fake some data as tho' it had come from
# your text file.
#
data = data = [[1, 'Tim', 'Golden'], [2, 'Fred', 'Smith']]

q = db.cursor ()
q.execute ("CREATE TABLE ztemp (id INT, first_name VARCHAR (60),
last_name VARCHAR (60))")

q.executemany ("INSERT INTO ztemp (id, first_name, last_name) VALUES
(%s, %s, %s)", data)

q.execute ("SELECT * FROM ztemp")
for row in q.fetchall ():
  print row

q.execute ("DROP TABLE ztemp")

</code>

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list