0

Comparatively new in both python and mysql.

I have solved the issue, but want to know the reason. If I use 'comma' after assigning the values to the variables, then it is changing the next datatype from 'int' to 'tuple'. For sting it is not. If I omit the 'comma' after each line then datatype remains as they are casting. In the both the cases, code exec without 'Exception'.

The DB input datatype are: func_Name as varchar, nbin as int, winWidth as int, ashFilt as varchar etc.

part of the code:-

cursor.execute("SELECT * FROM func_table;")
tab_funcTab = cursor.fetchall()
    print("parameters in [func_table]: ", cursor.rowcount)
    for row in tab_funcTab:
        funcName =      (str(row['func_Name'])), # <- this commas
        nbin =          (int(row['nbin'])), # <- this commas
        winWid =        (int(row['winWidth'])),
        ashFilt =       (str(row['ashFilt']))

     print(tab_funcTab)
     [{'id': 1, 'func_Name': 'all', 'nbin': 100, 'winWidth': 5, 'ashFilt': 'biweight']   

     print(type(funcName), type(nbin), type(winWid), type(ashFilt))
     <class 'str'> <class 'tuple'> <class 'tuple'> <class 'str'>

Could someone pl explain the reason?

Fakir
  • 139
  • 11

1 Answers1

1

Using comma after value will exactly create a tuple. I have tested your code again with both python2, and 3. You need to check it again.

loginmind
  • 563
  • 5
  • 11
  • Yes, I have seen that. When I take out the 'comma' the datatype are as I wish or expected. The comma is making the int as tuple. Though the str remain as str. – Fakir Feb 15 '20 at 06:52
  • No, I have tested with str, it also make a tuple. You can try this for test: `y=(str(1)),;print(type(y))` – loginmind Feb 15 '20 at 07:44
  • Is it? But after taking out the commas at the end of each line, (though kept the casting) it is coming as 'int', 'str' as expected. And using that values the main prog is also working fine. But thanks anyway for your time and suggestion. I will test yours and let you know. – Fakir Feb 17 '20 at 04:38