Must be str, not int

Viewed 50

Keep gettting this error:in pos sqlcuery="insert into position, 'position', values('"+position+"')" TypeError: must be str, not int

position= x + int(w/2)
        if position > 320:
             sqlcuery = pos(position)
             insertrec.db.execute(sqlcuery)
             db.commit()
             print(klar)
             db.close()

def pos(position):
    sqlcuery="insert into position ('position'), values('"+position+"')"
    return sqlcuery

Keep gettting this error, even if I change to str() or using ","

3 Answers

Personally, I'd recommend using an f-string. These allow you to input variables of any type within your code without too much issue. Wrap your variables in {}, like I've shown below.

sqlcuery=f"insert into position {position}"

Can I suggest that you use an f-string if you're on a version of python that supports them.

sqlcuery=f"insert into position ('position'), values('{position}')"

Try this:

"insert into position ('position'), values('"+str(position)+"')"

To transform the integer or float to a string.

You can do this more elegantly with a f-string:

f"insert into position ('position'), values('{position}')"
Related