現在、AWS環境にあるpythonコードを実行し、RDSのmysqlにあるデータベースへアクセスし、データの書き込みを行いたいと思い、作業を行っております。
しかし、コードを回してみたときに、一度データがテーブルに入り表示はさせているのですが、mysqlから見たときには、emptyになっています。
RDSの試用で何か設定しないと書き込みが行えないのでしょうか?
実行は、ec2-userとroot共にやってみましたが結果は同じでした。
考えられる原因は何でしょうか?
ご存じの方、ご教授お願いします。
参考サイト
【mysql-connector-python】PythonからMySQLを操作する
python
1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# ドライバをimport 5import mysql.connector 6 7if __name__ == '__main__': 8 # データベースに接続 9 connect = mysql.connector.connect(user='masterUserNishi', password='DLJnmyjmtchK', host='testdb.c2ry7lzwwt5q.ap-northeast-1.rds.amazonaws.com', database='student',charset='utf8') 10 cursor = connect.cursor() 11 12 id = '01' 13 name = '山田太郎' 14 sex = '男' 15 16 # insert 17 cursor.execute('insert into user values (%s,%s,%s)', (id, name, sex)) 18 19 # select 20 cursor.execute('select * from user') 21 row = cursor.fetchone() 22 23 # 出力 24 for i in row: 25 print(i) 26 27 # データベースから切断 28 cursor.close() 29 connect.close() 30
[ec2-user@ip-****** try_python]$ python db_student.py 01 山田太郎 男
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/20 01:36