AKor
AKor

Reputation: 8882

Pulling value from sqlite3 DB in Python

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

Upvotes: 1

Views: 409

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169304

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]))

Upvotes: 2

Related Questions