Python AttributeError: 'str' object has no attribute 'cursor'

后端 未结 1 949
面向向阳花
面向向阳花 2021-01-28 09:31

Today is my 3rd day learning Python and despite numerous web searches I can\'t seem to get past this issue.

The code takes in input on the command line and passes it to

相关标签:
1条回答
  • 2021-01-28 09:45

    This is just a concatenation of two strings, so a string (the parenthesis don't matter):

    ('pyodbc.connect'+"('blah')")
    

    And string % (value, value) takes the left side string as a printf-style format string and inserts the given values in it. The result again is a string, so your conStr is just a string. A string that probably looks like a Python function call, but a string nonetheless.

    I think you want to just call pyodbc.connect():

    con = pyodbc.connect(
          'DRIVER={SQL Server};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s' % 
          (server_name, database_name, uid, password))
    

    Though I'm not sure about the syntax of the strint it expects as argument. At least according to this that looks about fine.

    0 讨论(0)
提交回复
热议问题