mysql LOAD DATA INFILE 与自动增量主键 [英] mysql LOAD DATA INFILE with auto-increment primary key

查看:60
本文介绍了mysql LOAD DATA INFILE 与自动增量主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LOAD DATA LOCAL INFILE 'filename' INTO TABLE 'tablename'"将数据文件加载到 mysql 表中.

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

问题是源数据文件包含每个字段的数据,但缺少主键(id"列).我在创建数据库时添加了一个唯一的 id 字段,但现在我需要从下一个字段开始将数据导入表中,并在导入时自动增加 id 字段.

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.....


                    )
                    """)

这是我的 LOAD 查询

here is my LOAD query

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

有什么想法吗?

总结:创建一个带有额外 id 字段的表,该字段会自动增加将数据(20 列)加载到 21 个字段的表中,跳过 id 字段让 id 字段自动填充一个自动递增索引.

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.

推荐答案

指定列列表:

默认情况下,当 LOAD DATA INFILE 语句的末尾没有提供列列表时,输入行应该包含每个表列的字段.如果只想加载表的某些列,请指定列列表:

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

这篇关于mysql LOAD DATA INFILE 与自动增量主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆