-1

How do i connect to MySql database using python in Visual studio 2015 community? i have installed the pymssql in my project environment. what else do i need to do? i added the following connection string and its not working when i run my project:

from datetime import datetime
from flask import render_template
from SchoolsPortal import app
import pymssql

connection = pymssql.connect(server='localhost', user='root', password='admin', database='testschool')
print('Connected')

@app.route('/')
@app.route('/home')
def home():
    """Renders the home page."""`enter code here`
    return render_template(
        'index.html',
        title='Home Page',`enter code here`
        year=datetime.now().year,
    )
chikwapuro
  • 1,328
  • 1
  • 9
  • 10

1 Answers1

1

The problem is pymssql is for Microsoft SQL not MySQL. You want to install the package mysqlclient via pip: pip3 install mysqlclient for python 3x or if you are using python 2.7x pip install MySQLdb. I hope that helps. Read more at this answer here.

Community
  • 1
  • 1
james-see
  • 12,210
  • 6
  • 40
  • 47
  • the mysqldb package is not downloadable. i have how ever downloaded the mysqlclient: the following error is what i am getting on the donwload log file: No matching distribution found for MySQLdb 'MySQLdb' failed to install. Exit code: 1 – chikwapuro Oct 23 '15 at 04:22
  • @jamescampbell , where do you run pip3 install mysqlclien in Visual studio , which terminal or console do you use , does visual studio has a special terminal or console for running that ? am a beginner in flask – Lutaaya Huzaifah Idris Mar 08 '18 at 12:46
  • @LutaayaHuzaifahIdris you run it from the command prompt or terminal. It installs it as a process that can be used anywhere, not just in Visual Studio. Visual Studio is an IDE, an advanced code editor, just like Sublime Text and others, many IDE’s you can run terminal commands from within, but to be safe, I run things like this in a separate terminal window. I use OSX but have Windows 10 VM in case I need to test cross OS compatibility. – james-see Mar 08 '18 at 12:50
  • @jamescampbell, thanks so much for the information , i thought it can be done inside VS like pycharm? , ok let me use that – Lutaaya Huzaifah Idris Mar 08 '18 at 12:52
  • @LutaayaHuzaifahIdris I have not used pycharm or Visual Studio. – james-see Mar 08 '18 at 12:54