Mysql 连接器,为什么我的插入查询不起作用,但注释的查询却起作用?

问题描述 投票:0回答:2

就是这样。我正在使用 mysql-connector-python。我可以成功运行评论部分。但实际部分不起作用

                querry = "INSERT INTO selectdif (name, score) VALUES (%s, %s)"
                value = ("Sssss", 10)
                cursor.execute(querry)
                
                """sql = "INSERT INTO selectdif (name, score) VALUES (%s, %s)"
                val = [
                    ('Peter', 'Lowstreet 4'),
                    ('Amy', 'Apple st 652'),
                    ('Hannah', 'Mountain 21'),
                    ('Michael', 'Valley 345'),
                    ('Sandy', 'Ocean blvd 2'),
                    ('Betty', 'Green Grass 1'),
                    ('Richard', 'Sky st 331'),
                    ('Susan', 'One way 98'),
                    ('Vicky', 'Yellow Garden 2'),
                    ('Ben', 'Park Lane 38'),
                    ('William', 'Central st 954'),
                    ('Chuck', 'Main Road 989'),
                    ('Viola', 'Sideway 1633')
                    ]

                cursor.executemany(sql, val)"""

我很确定这是根据我看到的教程插入的正确方法

编辑:从此更改:

value = ("Sssss", 10)
            cursor.execute(querry)

对此:

value = ('Sssss', '10')
                cursor.execute(querry, value)

修好了。

python mysql connector
2个回答
0
投票

当您尝试插入多个时,请检查“分数”列的值。大概应该是数字吧。在您的代码中,您正在传递字符串。可能就是这个原因。


0
投票

我完全忘了赋予价值。

© www.soinside.com 2019 - 2024. All rights reserved.