Python的MySQLdb模块应该使用SQL语句字符串中的格式说明符来实现占位符。我下面从MySQL食谱MySQLdb占位符实现不起作用
import sys
import MySQLdb
import Cookbook
try:
conn = Cookbook.connect()
print("Connected")
except MySQLdb.Error as e:
print("Cannot connect to server")
print("Error code:", e.args[0])
print("Error message:", e.args[1])
sys.exit (1)
cursor = conn.cursor()
cursor.execute ("""
INSERT INTO profile (name,birth,color,foods,cats)
VALUES(%s,%s,%s,%s,%s)
""",("Josef", "1971-01-01", None, "eggroll", 4))
个例但是,当我从壳
mysql> SELECT * FROM profile WHERE name LIKE 'J%';
+----+--------+------------+-------+----------------+------+
| id | name | birth | color | foods | cats |
+----+--------+------------+-------+----------------+------+
| 7 | Joanna | 1952-08-20 | green | lutefisk,fadge | 0 |
+----+--------+------------+-------+----------------+------+
检查很明显,没有什么是inserted.Why? 如果我添加cursor.commit建议
cursor.commit()
AttributeError: 'Cursor' object has no attribute 'commit'
检查编辑的答案。将光标更改为conn – Aniket