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
相关文章