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))
からexempleを以下ですが、私はシェル
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'
編集した回答を確認してください。カーソルを – Aniket