mysql LOAD DATA INFILE with auto-increment primary key

假如想象 提交于 2020-03-18 03:31:39

问题


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.


回答1:


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




回答2:


LOAD DATA LOCAL INFILE '/var/www/.........../file.csv' INTO TABLE table_name FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\r\\n' (field1,field2,.........,fieldn)

This is in case of ubuntu




回答3:


your data file should look like this: null, study chars, other fields... MySQL will insert incremented id istead of null




回答4:


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




回答5:


Use the following query in case the csv record is like that

"21231","234424","My Category","1"

LOAD DATA LOCAL INFILE 'C:/wamp/www/cakephp/app/webroot/files/product_catalogs/categories.csv' INTO TABLE cats FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' (category_id,parent_id,category,is_leaf)

I have done that successfully. :)



来源:https://stackoverflow.com/questions/2716054/mysql-load-data-infile-with-auto-increment-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!