使用 MySQLdb 连接 MySQL 时,如果 id 字段定义是 auto_increment 的,那么 insert 时该如何插入这个字段呢? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
imkh
V2EX    Python

使用 MySQLdb 连接 MySQL 时,如果 id 字段定义是 auto_increment 的,那么 insert 时该如何插入这个字段呢?

  •  1
     
  •   imkh 2014-08-23 16:21:48 +08:00 9773 次点击
    这是一个创建于 4074 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码如下:
    import mysql.connector
    cOnn= mysql.connector.connect(user='test',password='123456',database='test',use_unicode='Ture')

    cursor = conn.cursor()
    cursor.execute('drop table if exists test1')
    cursor.execute('create table test1(id int not null auto_increment primary key,name varchar(20))')



    如果直接在MySQL里操作是可以以insert into test1 select null,'juno'或insert into test1 (name) values ('juno')或insert into test1 (id,name) values (null,'juno')这三种形式插入的,但放在MySQLdb中好像都不行。
    各位有什么解决方法呢?
    23 条回复    2014-08-24 22:32:03 +08:00
    Zuckonit
        1
    Zuckonit  
       2014-08-23 16:29:58 +08:00   1
    自增字段需要自己插么?
    Automan
        2
    Automan  
       2014-08-23 16:31:30 +08:00
    insert into test1 (name) values ('juno')

    提示什么错误?
    imkh
        3
    imkh  
    OP
       2014-08-23 16:32:30 +08:00
    @Automan 提示Wrong number of arguments during string formatting。
    Automan
        4
    Automan  
       2014-08-23 16:36:06 +08:00
    @imkh 不懂python..不过我建议你查下cursor.execute,应该是这里的格式错误。
    Chigogo
        5
    Chigogo  
       2014-08-23 16:37:19 +08:00
    MySQL的自增值不要插入的。创建的时候就自动有了。
    imkh
        6
    imkh  
    OP
       2014-08-23 16:40:48 +08:00
    @Zuckonit 我知道不用,但insert语句要怎么写?
    imkh
        7
    imkh  
    OP
       2014-08-23 16:41:45 +08:00
    @Chigogo 是不用啊,但insert语句要怎么写?以insert into test1 (name) values ('juno')这种形式插入会出现“mysql.connector.errors.ProgrammingError: Wrong number of arguments during string formatting”这种错误。
    jerry
        8
    jerry  
       2014-08-23 16:47:35 +08:00   1
    贴代码吧,应该是用错了
    lujjjh
        9
    lujjjh  
       2014-08-23 16:48:53 +08:00   1
    @imkh 这明显不是 SQL 的问题了,错误提示告诉你是字符串格式化的时候参数个数不对。贴完整代码吧。
    imkh
        10
    imkh  
    OP
       2014-08-23 16:51:06 +08:00
    谢谢各位,是格式用错了。原来我写的语句是cursor.execute('insert into test1 (name) values (%s)','juno'),改成cursor.execute('insert into test1 (name) values (%s)',['juno'])就行了。
    zts1993
        11
    zts1993  
       2014-08-23 16:51:22 +08:00
    null
    imkh
        12
    imkh  
    OP
       2014-08-23 16:54:13 +08:00
    @zts1993 我定义了not null。
    imkh
        13
    imkh  
    OP
       2014-08-23 16:56:07 +08:00
    @zts1993 在MySQL命令行里是可以的,在代码里会出现“mysql.connector.errors.DatabaseError: 1366 (HY000): Incorrect integer value: 'null' for column 'id' at row 1”错误。
    kongkongyzt
        14
    kongkongyzt  
       2014-08-23 18:04:55 +08:00   1
    建议写成 cursor.execute("insert into test1(name) values('{}')".format('juno'))
    Ctech
        15
    Ctech  
       2014-08-23 18:39:56 +08:00
    insert into (name1,name2) values('%s','%s') %(value1,value2).........
    Ctech
        16
    Ctech  
       2014-08-23 18:40:28 +08:00
    @Ctech insert into tablename (name1,name2) values('%s','%s') %(value1,value2).........
    iptux
        17
    iptux  
       2014-08-23 18:51:07 +08:00
    没人这样写么?
    cursor.execute('insert into test1 (name) values (?)',('juno',))
    pc10201
        18
    pc10201  
       2014-08-23 19:01:05 +08:00   1
    @ctech 的写法有问题,会被sql注入的
    @iptux 的写法是对的,
    python的mysql最佳实践,来源
    http://stackoverflow.com/questions/7929364/python-best-practice-and-securest-to-connect-to-mysql-and-execute-queries
    fatpa
        19
    fatpa  
       2014-08-23 19:28:42 +08:00
    sql = "insert into test1 (name) values (%s)"
    mysql.execute(sql, 'name')
    zeayes
        20
    zeayes  
       2014-08-24 00:06:16 +08:00
    SQL insert语句指定字段名字,SQL语句中变量用占位符。
    zts1993
        21
    zts1993  
       2014-08-24 09:22:28 +08:00
    @imkh 你难道写的是 'null' 而不是 null 没有引号
    ulic95
        22
    ulic95  
       2014-08-24 09:56:53 +08:00
    学习了~
    suckli
        23
    suckli  
       2014-08-24 22:32:03 +08:00 via iPhone
    null
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2643 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 14:32 PVG 22:32 LAX 07:32 JFK 10:32
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86