简体   繁体   English

带有自动增量主键的mysql LOAD DATA INFILE

[英]mysql LOAD DATA INFILE with auto-increment primary key

I am trying to load a data file into mysql table using "LOAD DATA LOCAL INFILE 'filename' INTO TABLE 'tablename'". 我正在尝试使用“LOAD DATA LOCAL INFILE'filename'INTO TABLE'tablename'”将数据文件加载到mysql表中。

The problem is the source data file contains data of every fields but the primary key is missing ('id' column). 问题是源数据文件包含每个字段的数据但缺少主键('id'列)。 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. 我在创建数据库时添加了一个唯一的id字段,但现在我需要从下一个字段开始将数据导入表中,并在导入时自动增加id字段。

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 这是我的LOAD查询

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. 简介:创建一个带有附加id字段的表,该表将自动将加载数据(20列)添加到跳过id字段的21个字段的表中,让id字段自动填充自动增量索引。

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. 默认情况下,如果在LOAD DATA INFILE语句末尾没有提供列列表,则输入行应包含每个表列的字段。 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 http://dev.mysql.com/doc/refman/5.1/en/load-data.html

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)

这是在ubuntu的情况下

你的数据文件应如下所示:null,study chars,其他字段...... MySQL将插入递增的id而不是null

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. 列列表必须在最后,id列必须有一个set = null才能自动递增 - 遗憾的是mysql文档非常模糊。 they justt say col1, col2, but are those in the source file or in the dest table . 他们只是说col1,col2,但它们是源文件或dest表中的那些。 This page helped a lot by clear examples. 这个页面通过清晰的例子帮助了很多。

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

Use the following query in case the csv record is like that 如果csv记录是这样的话,请使用以下查询

"21231","234424","My Category","1" “21231”,“234424”,“我的类别”,“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) LOAD DATA LOCAL INFILE'C:/wamp/www/cakephp/app/webroot/files/product_catalogs/categories.csv'INTO TABLE猫场由'''封闭'''由'\\ r \\ n'终止的线路( CATEGORY_ID,PARENT_ID,类别,is_leaf)

I have done that successfully. 我成功地做到了。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM