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?

1 Answer 1

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.

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

3 Comments

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.
No, I have tested with str, it also make a tuple. You can try this for test: y=(str(1)),;print(type(y))
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.

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.