mysql LOAD DATA INFILE 带有自动增量主键
我正在尝试使用“LOAD DATA LOCAL INFILE 'filename' INTO TABLE 'tablename'”将数据文件加载到 mysql 表中。
问题是源数据文件包含每个字段的数据,但缺少主键(“id”列)。我在创建数据库时添加了一个唯一的 id 字段,但现在我需要从下一个字段开始将数据导入到表中,并在导入时自动递增 id 字段。
def create_table():
cursor.execute ("""
CREATE TABLE variants
(
id integer(10) auto_increment primary key,
study_no CHAR(40),
other fields.....
)
""")
这是我的 LOAD 查询
query1= "LOAD DATA LOCAL INFILE '"+currentFile+"' INTO TABLE variants FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n'"
有什么想法吗?
概括: 创建一个带有附加 id 字段的表,该字段会自动递增 将数据(20 列)加载到 21 个字段的表中,跳过 id 字段 让 id 字段自动填充自动增量索引。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
指定列列表:
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata (col1,col2,...);
http://dev.mysql.com/doc/refman/5.1/en/load-数据.html
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
这是ubuntu的情况
This is in case of ubuntu
你的数据文件应该是这样的:
null,研究字符,其他字段...
MySQL 将插入递增的 id 而不是 null
your data file should look like this:
null, study chars, other fields...
MySQL will insert incremented id istead of null
列列表必须位于最后,并且 id 列必须设置 set=null 才能自动递增 –
不幸的是mysql文档非常模糊。他们只是说 col1,col2,但它们是在源文件或 dest 表中。该页面通过清晰的示例提供了很多帮助。
http://www. Experts-exchange.com/articles/Database/MySQL/Load-Delimited-Data-into-MySQL-Server.html
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
如果 csv 记录是这样的,请使用以下查询
我已经成功做到了。 :)
Use the following query in case the csv record is like that
I have done that successfully. :)