Bradley Ridgway
Bradley Ridgway

Reputation: 55

I encounter an error when using the SQLITE3 module

I am attempting to use SQL in order to make a table currently I have created this

import sqlite3
conn = sqlite3.connect("Classes.db")
c = conn.cursor()
score = 5
name = ("Brad")
Class = 2
def tableCreate():
    c.execute("CREATE TABLE Class{}(ID INT, Name TEXT, Score (Out of 10) INT").format(Class)
def dataEntry():
    c.execute("INSERT INTO Class{} (Name, Score) VALUES (?,?)",
        (score,name)).format(Class)
    conn.commit()

When I run the tableCreate function it returns the error:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    tableCreate()
c.execute("CREATE TABLE Class{}(ID INT, Name TEXT, Score (Out of 10)INT").format(Class)
sqlite3.OperationalError: unrecognized token: "{"

I would appreciate any help in resolving this error

Upvotes: 0

Views: 54

Answers (2)

HavelTheGreat
HavelTheGreat

Reputation: 3386

You should replace

c = conn.cursor

with

c = conn.cursor()

in order to do queries with the cursor object. All that c = conn.cursor allows you to do is create a database. If you're confused about the difference between a regular db cursor and one used for queries, this answer (and question) may help you make the distinction.

Edit:
For your second problem you have a parentheses issue, and the execute line should be enclosed properly, like this:

c.execute("CREATE TABLE Class{}(ID INT, Name TEXT, Score (Out of 10) INT".format(Class))

Upvotes: 2

user1016274
user1016274

Reputation: 4209

The format method is a method of the string object, i.e. you put the closing bracket too early:

c.execute("CREATE TABLE Class{}(ID INT, Name TEXT, Score (Out of 10) INT").format(Class)

should read

c.execute("CREATE TABLE Class{}(ID INT, Name TEXT, Score (Out of 10) INT".format(Class))

Upvotes: 0

Related Questions