Search code examples
pythonmysqldatabaseflaskpythonanywhere

how to use flask to connect to a database in pythonanywhere


i have created a database in the database tab in pythonanywhere and now i am trying to connect to it so i can get my tables and information from it.

what is the best way to connect to it and how !?(sqlite3 , SQLAlchemy)

i used

import import MySQLdb
db = MySQLdb.connect(host="mysql.server",
                 user="username",
                  passwd="password",
                  db="db-name")

and it gave me

return Connection(*args, **kwargs)
super(Connection, self).__init__(*args, **kwargs2)
mysql_exceptions.OperationalError: (2005, "Unknown MySQL server host 'mysql.server' (0)")

Solution

  • After creating database in pythonanywhere .

    How to connect it with python?.

    This is python code in which you are going to connect to the database..

    Import MySQLdb as my
    
    db = my.connect ('database-server-name','username-of -db','password-of-db','database-name')
    cursor = db.cursor ()
    

    Now your database is connected with your server and python code

    Hope this clears!!! You will get the db server name, username, and the password you created after making database and your database name from the database tab!!