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

发布于 2024-08-30 16:53:39 字数 799 浏览 13 评论 0原文

我正在尝试使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦亿 2024-09-06 16:53:39

指定列列表:

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

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:

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

悸初 2024-09-06 16:53:39
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的情况

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

绮筵 2024-09-06 16:53:39

你的数据文件应该是这样的:
null,研究字符,其他字段...
MySQL 将插入递增的 id 而不是 null

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

初心未许 2024-09-06 16:53:39

列列表必须位于最后,并且 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

情未る 2024-09-06 16:53:39

如果 csv 记录是这样的,请使用以下查询

“21231”,“234424”,“我的类别”,“1”

加载数据本地INFILE
'C:/wamp/www/cakephp/app/webroot/files/product_catalogs/categories.csv'
INTO TABLE cats 字段以 ',' 结尾,并由 '"' 括起
以 '\r\n' 结尾的行
(category_id,parent_id,类别,is_leaf)

我已经成功做到了。 :)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文