python - 带有自动递增主键的mysql LOAD DATA INFILE

标签 python mysql import 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 字段自动填充自增索引。

最佳答案

指定列列表:

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

关于python - 带有自动递增主键的mysql LOAD DATA INFILE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2716054/

相关文章:

python - 如何将列表的平均值插入到同一个列表中?

python - OpenCmis 客户端 - 创建露天标签

php - 如何使用 dbSelect 将此 Mysql 查询实现到 Zend 框架 1

java - 如何使用不同包中的方法?

java - 导入包.* 与导入包.SpecificType

python - 基于 Django 类的 View 中的身份验证用户

python - Django 模板 : key, 值在 for 循环中不可能

mysql - $$DELIMITER 附近的语法错误

PHP isset 不会填充表单中的单选元素

Perl:如何从基类导入子程序?