Pythonを用いてMySQLのデータベースにデータを格納していきたいと考えています。
SQL
1show columns from tex; 2+-------+----------+------+-----+---------+-------+ 3| Field | Type | Null | Key | Default | Extra | 4+-------+----------+------+-----+---------+-------+ 5| date | datetime | YES | | NULL | | 6| week | text | YES | | NULL | | 7| tex | text | YES | | NULL | | 8+-------+----------+------+-----+---------+-------+ 93 rows in set (0.05 sec)
以上のようなテーブルに、データを挿入します。
SQL
1INSERT INTO tex VALUES ("2018-07-21 15:28:48", 'Saturday', '海賊王になるのは誰だ。。。 '); 2 3select * from tex; 4+---------------------+----------+------------------------------------------+ 5| date | week | tex | 6+---------------------+----------+------------------------------------------+ 7| 2018-07-21 15:28:48 | Saturday | 海賊王になるのは誰だ。。。 | 8+---------------------+----------+------------------------------------------+ 9
以上のようにMySQLの環境ではうまく実行できるのですが、Python上で実行した時にうまくいきません。
Python
1import mysql.connector 2import re 3 4db=mysql.connector.connect(host="localhost", user="user", password="user_pass") 5 6cursor=db.cursor() 7 8cursor.execute("USE tex_base") 9db.commit() 10 11ret = "海賊王になるのは誰だ。。。" 12date = "Sat Jul 21 15:28:48 +0000 2018" 13 14cursor.execute("SELECT str_to_date('%s', '%%a %%b %%d %%T %%f %%Y')" % date) 15rows = cursor.fetchall() 16 17cursor.execute("SELECT DAYNAME('%s')" % rows[0][0]) 18week = cursor.fetchall() 19 20cursor.execute("INSERT into tex(date, week, tex) values('%s', '%s' '%s')" % (rows[0][0], week[0][0], ret))
このコードを実行すると、以下のようなエラーが出ます。
mysql.connector.errors.DataError: 1136 (21S01): Column count doesn't match value count at row 1
原因がわかりません。
改善策を教えていただきたいです。よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー