Import a textfile to MS SQL with python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joel.sjoo@gmail.com

    #1

    Import a textfile to MS SQL with python

    I'm a dba for SQL server and I Will import a textfile to SQL. For
    example I use a file with 3 columns. ID, Name and Surname and the
    columns are tab separated. I don't know much about programming.
    Anyway, I use this code below. It works, but it will not split the
    columns. I have tried to change the argumnts in str(alllines[]) Some of
    the columns can include many characters and some not. For exampel names
    can be Bo or Lars-Ture.

    I be glad if some can help me with this.

    Regar Joel

    import pymssql
    import string,re

    myconn =
    pymssql.connect (host='lisa',us er='sa',passwor d='AGpu83!#',da tabase='junk')
    mycursor = myconn.cursor()

    inpfile=open('c :\\temp\\test.t xt','r')
    for alllines in inpfile.read(). split('\n'):
    stmt="insert into python (id, namn, efternamn) values ('%s', '%s',
    '%s')" %(str(alllines[0]),str(alllines[2:10]),str(alllines[3:10]))

    mycursor.execut e(stmt)
    print stmt
    inpfile.close()
    myconn.commit()
    myconn.close()

  • Steve Holden

    #2
    Re: Import a textfile to MS SQL with python

    joel.sjoo@gmail .com wrote:
    I'm a dba for SQL server and I Will import a textfile to SQL. For
    example I use a file with 3 columns. ID, Name and Surname and the
    columns are tab separated. I don't know much about programming.
    Anyway, I use this code below. It works, but it will not split the
    columns. I have tried to change the argumnts in str(alllines[]) Some of
    the columns can include many characters and some not. For exampel names
    can be Bo or Lars-Ture.
    >
    I be glad if some can help me with this.
    >
    Regar Joel
    >
    import pymssql
    import string,re
    >
    myconn =
    pymssql.connect (host='lisa',us er='sa',passwor d='AGpu83!#',da tabase='junk')
    Thanks for letting us know the administrator password for your database.
    You might want to consider changing it (unless you modified this line
    before posting).
    mycursor = myconn.cursor()
    >
    inpfile=open('c :\\temp\\test.t xt','r')
    for alllines in inpfile.read(). split('\n'):
    stmt="insert into python (id, namn, efternamn) values ('%s', '%s',
    '%s')" %(str(alllines[0]),str(alllines[2:10]),str(alllines[3:10]))
    >
    mycursor.execut e(stmt)
    print stmt
    This is much better expressed as something like the following (untested):

    stmt = "insert into python (id, namn, efternamn) values (?, ?, ?)"
    for line in inpfile:
    mycursor.execut e(stmt, tuple(line.spli t()))

    Note that the "(?, ?, ?)" list of parameter markers assumes that pymssql
    uses the "qmark" paramstyle, you'll have to check the documentation if
    you get SQL syntax errors or similar - I couldn't easily find a
    reference on the web.

    The point of passing the tuple of data values as a second argument to
    the .execute() method is to have the DB module take care of any
    necessary quoting and representation issues. Otherwise values that (for
    example) include a singel quotes, such as "O'Reilly" can be problematical.
    inpfile.close()
    myconn.commit()
    myconn.close()
    >
    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden

    Comment

    Working...