473,549 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()

Sep 6 '06 #1
1 2611
jo*******@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

Sep 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
16784
by: Wolfgang.Stoecher | last post by:
Hi, another beginner's question: when I import a module, change the corresponding textfile and try to import the same module again, it looks like the internal code does not change (the source-line in an error-message is up to date, however!). Also del module before import and deleting the .pyc file do not help! How to realize a fast...
1
2529
by: Larry Rekow | last post by:
I have a report that's created each day as a flat textfile. Because I came from the Access world, I created a macro that imports it with a schema that gives meaningful names to the various columns, and then uses a query to massage some of the data for me (deletes the first blank row and does a couple of calculations) Then I use DTS to...
3
3658
by: Elaksomfan | last post by:
Hi, I am having trouble importing data from an excel spreadsheet into MS SQL Server 2000 using DTS Wizard. The DTS import process is successfull, no errors, but only 50 rows of approx. 1500 rows of data are imported. I tried to remove 20 rows in the excel spreadsheet in the interval row 0-50. When i later ran the import, only 30 rows were...
2
455
by: Sigurd Bruteig | last post by:
Hi all! With the transferText method I have made a routine that import a textfile to a table.The textfile is a pricelist from the supplier. The problem is that the supplier uses . (dot) as descimal point, but the standard desimal point is , (comma). How do i change the desimal point, without any user action. Is it possible to do this in a...
4
5287
by: mibispam | last post by:
Hello, I'm trying to import a textfile into an Access table. I want to write a macro to do all the work automatically. My textfile looks like that: artNr;description;sellingValue 111111;top;0.34 The different colums are divided by ";" and the sellingValue is a
1
11047
by: Supersonic | last post by:
I’m a dba for a SQL and I’m trying to learn how to import data from a simple text file. The text file looks like "1, name" "2, name2" and so on. How is it possible to solve this import with python? It’s no problem to connect to the database and do some insert and select statement. It is when I put the read from text file and then insert this data...
2
1819
by: arunaksha7 | last post by:
I am new to ASP.NET. I am facing a problem to import contents of a textfile to my ASP.NET Web Application datagrid.
9
5765
by: a | last post by:
Dear friends I want import data from CSV file to mdb file How can I do that in vb.net?
5
3154
by: kashif73 | last post by:
I have a text file with hundreds of records. each line contain 1250 values seperated by a semicolon. I have created 2 tables in SQL server 2000, one with 1000 columns & the second with 250 columns. my question is how can i import this textfile in to these 2 tables.and how i ensure that first 1000 values go to first table & the rest 250 in the next...
0
7548
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7472
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7986
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6074
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3499
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1083
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.