構成:
クライアント EC2(
Python 3.7.4
mycli==1.20.1
mysqlclient==1.4.4
)
MySQL8.0
以下のコードを使用して、mysqlにカラムを追加しているのですが、"save_mysql.py"の実行結果ではカラムが追加されているようなのですが、実際にmysqlに接続してクエリを発行してみるとカラムが追加されていないみたいです。
どのように改善したらよいでしょうか?
(複数回実行すると、idが繰り上がっていて問題ないようにも見えるんですが、c.execute('select * from cities')で前回分が出力されないのもおかしいとはおもっています。。。)
[save_mysql.py]
import MySQLdb connect = MySQLdb.connect( db='scraping', user='mscraper', passwd='xxxxxxxx', host='xxx.ap-northeast-1.rds.amazonaws.com', charset='utf8mb4' ) c = connect.cursor() #c.execute('DROP TABLE IF EXISTS cities') # citiesテーブルを作成する。 #c.execute('create table cities (id int AUTO_INCREMENT NOT NULL PRIMARY KEY, city text, population integer)') c.execute('insert into cities values(%s, %s, %s)',(0,'上海', 2415000)) c.executemany('insert into cities values (%(id)s,%(city)s,%(population)s)',[ {'id': 0, 'city': 'aaa', 'population': 21100000}, {'id': 0, 'city': '斉藤', 'population': 12340000}, {'id': 0, 'city': 'nozomi', 'population': 14560000}, ]) connect.commit c.execute('select * from cities') for row in c.fetchall(): print(row) connect.close()
(scraping) [ec2-user@ip-10-0-0-159 ~]$ python3 ./save_mysql.py (17, '上海', 2415000) (18, 'aaa', 21100000) (19, '斉藤', 12340000) (20, 'nozomi', 14560000) (scraping) [ec2-user@ip-10-0-0-159 ~]$ python3 ./save_mysql.py (21, '上海', 2415000) (22, 'aaa', 21100000) (23, '斉藤', 12340000) (24, 'nozomi', 14560000) (scraping) [ec2-user@ip-10-0-0-159 ~]$ python3 ./save_mysql.py (25, '上海', 2415000) (26, 'aaa', 21100000) (27, '斉藤', 12340000) (28, 'nozomi', 14560000) [ec2-user@ip-10-0-0-159 ~]$ mycli -P3306 -u -p -hap-northeast-1.rds.amazonaws.com mysql 8.0.16 mycli 1.20.1 Chat: https://gitter.im/dbcli/mycli Mail: https://groups.google.com/forum/#!forum/mycli-users Home: http://mycli.net Thanks to the contributor - Zane C. Bowers-Hadley (none)> use scraping You are now connected to database "scraping" as user "mscraper" Time: 0.001s scraping> select * from cities +----+------+------------+ | id | city | population | +----+------+------------+ 0 rows in set Time: 0.014s scraping>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/01 04:59