V2EX › Python
使用 MySQLdb 连接 MySQL 时,如果 id 字段定义是 auto_increment 的,那么 insert 时该如何插入这个字段呢?
By imkh at 2014 年 8 月 23 日 · 10560 次点击代码如下:
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中好像都不行。
各位有什么解决方法呢?
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
|
1
Zuckonit 2014 年 8 月 23 日 自增字段需要自己插么?
|
|
2
Automan 2014 年 8 月 23 日
insert into test1 (name) values ('juno')
提示什么错误? |
|
5
Chigogo 2014 年 8 月 23 日
MySQL的自增值不要插入的。创建的时候就自动有了。
|
|
7
imkh OP @Chigogo 是不用啊,但insert语句要怎么写?以insert into test1 (name) values ('juno')这种形式插入会出现“mysql.connector.errors.ProgrammingError: Wrong number of arguments during string formatting”这种错误。
|
|
8
jerry 2014 年 8 月 23 日 贴代码吧,应该是用错了
|
|
10
imkh OP 谢谢各位,是格式用错了。原来我写的语句是cursor.execute('insert into test1 (name) values (%s)','juno'),改成cursor.execute('insert into test1 (name) values (%s)',['juno'])就行了。
|
|
11
zts1993 2014 年 8 月 23 日
null
|
|
13
imkh OP @zts1993 在MySQL命令行里是可以的,在代码里会出现“mysql.connector.errors.DatabaseError: 1366 (HY000): Incorrect integer value: 'null' for column 'id' at row 1”错误。
|
|
14
kongkongyzt 2014 年 8 月 23 日 建议写成 cursor.execute("insert into test1(name) values('{}')".format('juno'))
|
|
15
Ctech 2014 年 8 月 23 日
insert into (name1,name2) values('%s','%s') %(value1,value2).........
|
|
16
Ctech 2014 年 8 月 23 日
@Ctech insert into tablename (name1,name2) values('%s','%s') %(value1,value2).........
|
|
17
iptux 2014 年 8 月 23 日
没人这样写么?
cursor.execute('insert into test1 (name) values (?)',('juno',)) |
|
18
pc10201 2014 年 8 月 23 日 @ctech 的写法有问题,会被sql注入的
@iptux 的写法是对的, python的mysql最佳实践,来源 http://stackoverflow.com/questions/7929364/python-best-practice-and-securest-to-connect-to-mysql-and-execute-queries |
|
19
fatpa 2014 年 8 月 23 日
sql = "insert into test1 (name) values (%s)"
mysql.execute(sql, 'name') |
|
20
zeayes 2014 年 8 月 24 日
SQL insert语句指定字段名字,SQL语句中变量用占位符。
|
|
22
ulic95 2014 年 8 月 24 日
学习了~
|
|
23
suckli 2014 年 8 月 24 日 via iPhone
null
|
推荐学习书目
› 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