在PythonAnywhere上的Django在线界面中看不到新的SQL表

时间:2017-07-29 20:40:08

标签: mysql django pythonanywhere

作为托管我网站的一部分,我在pythonanywhere.com上有一个SQL服务器,其中包含从我的网站收集的一些数据。我需要将一些信息聚合到存储在同一数据库中的新表中。如果我使用下面的代码,我可以创建SHOW TABLES查询所观察到的新表。但是,我无法在SQL服务器旁边的Django在线界面中看到该表。

为什么会这样?如何在Django界面上显示新内容,以便浏览内容并进行修改?

from __future__ import print_function

from mysql.connector import connect as sql_connect
import sshtunnel
from sshtunnel import SSHTunnelForwarder
from copy import deepcopy

sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0


def try_query(query):
    try:
        cursor.execute(query)
        connection.commit()
    except Exception:
        connection.rollback()
        raise


if __name__ == '__main__':
    remote_bind_address = ('{}.mysql.pythonanywhere-services.com'.format(SSH_USERNAME), 3306)
    tunnel = SSHTunnelForwarder(('ssh.pythonanywhere.com'),
                                ssh_username=SSH_USERNAME, ssh_password=SSH_PASSWORD,
                                remote_bind_address=remote_bind_address)
    tunnel.start()
    connection = sql_connect(user=SSH_USERNAME, password=DATABASE_PASSWORD,
                             host='127.0.0.1', port=tunnel.local_bind_port,
                             database=DATABASE_NAME)

    print("Connection successful!")
    cursor = connection.cursor()                      # get the cursor
    cursor.execute("USE {}".format(DATABASE_NAME))    # select the database

    cursor.execute("SHOW TABLES")
    prev_tables = deepcopy(cursor.fetchall())

    try_query("CREATE TABLE IF NOT EXISTS TestTable(TestName VARCHAR(255) PRIMARY KEY, SupplInfo VARCHAR(255))")
    print("Created table.")
    cursor.execute("SHOW TABLES")
    new_tables = deepcopy(cursor.fetchall())

0 个答案:

没有答案