1

I'm writing a script that inserts win/loss results into a sqlite3 database. I'm also trying to pull the win/loss count from said database table. However, I get an error that I can't convert from sqlite3 cursor object to a string to concatenate into my win message. I'm definitely doing something wrong here. Any help?

        cur.execute("INSERT INTO WINS(winner) VALUES('Computer')")
        numberWins = cur.execute("select count(*) from WINS where winner = 'Computer'")
        print("******************************\n" + numberWins + "Computer Wins\n******************************")

I get this error: TypeError: cannot concatenate 'str' and 'sqlite3.Cursor' objects

1 Answer 1

2

You need to call .fetchone() or .fetchall() on the query. That then returns either a tuple or a list of tuples. So you access the value with either numberWins[0] or numberWins[0][0].

numberWins = cur.execute("select count(*) from WINS where winner = 'Computer'").fetchone()
print("************\n" + str(numberWins[0]) + "Computer Wins\n************")

Alternately you can use string formatting:

print("************\n {} Computer Wins\n************".format(numberWins[0]))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.