473,803 Members | 3,899 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 2621
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
16791
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 edit-try-cycle? (btw: I am using python 2.2 under windows) thanks in advance!
1
2547
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 import the Access query as a table. the textfile has a column called "File_num", and among several...
3
3672
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 imported. I deleted almost every row in the interval 0-50, with the result of the import having 0...
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 query? Sigurd --
4
5297
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
11246
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 to the database I got problem. One script I have tried looks like this: import pymssql import...
2
1836
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
5792
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
3166
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 from each line of the text file. can be this be done via VB.NET or ASP? I would be grateful for...
0
9564
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10310
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9121
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7603
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
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.