简体   繁体   English

MySQL 通过 PythonAnywhere 与 Python 连接

[英]MySQL connection with Python through PythonAnywhere

I want to connect to MySql database using Python through PythonAnywhere, without creating a Flask/Django application.我想通过 PythonAnywhere 使用 Python 连接到 MySql 数据库,而无需创建 Flask/Django 应用程序。

I have seemingly managed to connect through MySQLdb, using the code below, but I do not receive a response when I run the code.我似乎设法使用下面的代码通过 MySQLdb 进行连接,但是当我运行代码时没有收到响应。 Any solutions?有什么解决办法吗?

import MySQLdb

db = MySQLdb.connect(
    host = "myuser.mysql.pythonanywhere-services.com",
    user = "myuser",
    passwd = XXX,
    db = "myuser$db_name"
    )

cursor = db.cursor()

cursor.execute("SELECT * FROM table_name")

for x in cursor:
    print(x)

cursor.close()
db.close()

You retrieve all rows in the table, without error.您检索表中的所有行,没有错误。

cursor.execute("SELECT * FROM table_name")

for x in cursor:
    print(x)

Yet you see no output.然而你看不到任何输出。 This is normal for a table that contains zero rows.这对于包含零行的表来说是正常的。

Consider doing one or more INSERTs, and a COMMIT, prior to the query.考虑在查询之前执行一个或多个 INSERT 和一个 COMMIT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM