나는 Flask를 통해 mysql 서버에 연결하는 데 문제가있다. server.py, testDB.py, testDB2.py 및 가 초기화 평Flask에서 Python을 사용하여 MySQL 서버에 연결하는 방법은 무엇입니까?
init.py 수입 testDB.py과 내가 파이썬 server.py을 실행하면 첫째,이 인쇄됩니다
: 나는 4 개 파일이changes in the database
* Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
내 user_info 테이블에는 user1이 있습니다. init.py 수입 testDB2.py 내가 파이썬 server.py을 실행하면
그러나, 그것은 단지* Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
내 user_info 테이블은 사용자 2가 표시되지 않습니다 인쇄 할 수 있습니다.
어떻게이 문제를 해결할 수 있습니까? testDb.py testDB2.py과 차이가 나는
INIT 평
from flask import Flask
app = Flask(__name__)
import testDB
server.py
from Sw import app
if __name__ == '__main__':
port = 8000
host = '0.0.0.0'
app.run(host = host, port = port)
testDB.py
testDB2.py 의 함수 정의된다import MySQLdb
db = MySQLdb.connect(host="127.0.0.1",user="root",passwd="1234",db="testdb")
cursor = db.cursor()
sql="""INSERT INTO user_info (user_id, user_email, user_password) VALUES ('user1','00000','000000')"""
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
print "changes in the database"
db.commit()
except:
# Rollback in case there is any error
print "there is any error"
db.rollback()
db.close()
testDB2.py
import MySQLdb
def testDB():
db = MySQLdb.connect(host="127.0.0.1",user="root",passwd="1234",db="testdb")
cursor = db.cursor()
sql="""INSERT INTO user_info (user_id, user_email, user_password) VALUES ('user2','00000','000000')"""
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
print "changes in the database"
db.commit()
except:
# Rollback in case there is any error
print "there is any error"
db.rollback()
db.close()
'testDB2.py'에서'testDB' 함수를 호출해야합니다. – dirn