views:

1799

answers:

2

I am trying to load a data file into mysql table using "LOAD DATA LOCAL INFILE 'filename' INTO TABLE 'tablename'".

The problem is the source data file contains data of every fields but the primary key is missing ('id' column). I add a unique id field while I create the database but now I need to import the data into the table starting from the next field and auto increment the id field while importing.

def create_table():
            cursor.execute ("""
                    CREATE TABLE variants
                    (
                    id integer(10) auto_increment primary key,
                    study_no CHAR(40),
                    other fields.....


                    )
                    """)

here is my LOAD query

query1= "LOAD DATA LOCAL INFILE '"+currentFile+"' INTO TABLE variants FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n'"

any ideas?

summary: create a table with an additional id field that would auto increment load data (20 columns) into the table of 21 fields skipping the id field let the id field automatically populate with an auto increment index.

Thanks

+4  A: 

Specify a column list:

By default, when no column list is provided at the end of the LOAD DATA INFILE statement, input lines are expected to contain a field for each table column. If you want to load only some of a table's columns, specify a column list:

LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata (col1,col2,...);

http://dev.mysql.com/doc/refman/5.1/en/load-data.html

Aaron
+1: Beat me by just a few seconds.
Mark Byers
is there an easy way of listing all 20 column names may be like a list [1:] notation? Or do I have to type each column name such as study_no, family_no, etc.?thanks
biomed
the column list must be at the very end and id column must have a set=null to be auto incremented
biomed
To add the column names, you should be able to do: cursor.execute("DESCRIBE tablename"); query = "LOAD DATA INFILE 'data.txt' INTO TABLE tablename (%s)" % ', '.join([x[0] for x in cursor.fetchall()][1:])
Aaron
A: 

the column list must be at the very end and id column must have a set=null to be auto incremented – unfortunately the mysql documentation is very vague. they justt say col1, col2, but are those in the source file or in the dest table . This page helped a lot by clear examples.

http://www.experts-exchange.com/articles/Database/MySQL/Load-Delimited-Data-into-MySQL-Server.html

biomed