1

I want to connect to MySql database using Python through PythonAnywhere, without creating a Flask/Django application.

I have seemingly managed to connect through MySQLdb, using the code below, but I do not receive a response when I run the code. 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()
2
  • How are you running this code, and what form of "response" are you expecting? Commented Jun 30, 2022 at 15:33
  • The print statement is not responding, for example Commented Jun 30, 2022 at 15:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.