0

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

2 Answers2

2

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))
Community
  • 1
  • 1
HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34
  • After fixing this I now also receive a new error as shown above – Bradley Ridgway Mar 12 '15 at 12:01
  • 1
    @BradleyRidgway Then instead of editing your question for your new error (*commonly known as being a "help vampire"*) it would probably be in your best interest to maintain the current integrity of your question, investigate your new problem, and if you are completely incapable of solving it ***then*** posting another new question. – HavelTheGreat Mar 12 '15 at 12:03
  • I don't see anything wrong with being a "Help Vampire" if that is ultimately the purpose of the site. – Bradley Ridgway Mar 12 '15 at 12:07
  • 1
    @BradleyRidgway It really isn't the purpose of this site. I'm not here to debug your code, I'm here to help you figure out a ***specific*** issue with your code should you be incredibly stuck on it. Ultimately questions should provide value to other users that may come across them. If you edit your quesiton continuously with your "new error", all it does is confuse anyone who sees your question. For your information however, you have a brace issue, `c.execute("CREATE TABLE Class{}(ID INT, Name TEXT, Score (Out of 10) INT".format(Class))` – HavelTheGreat Mar 12 '15 at 12:09
0

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))
user1016274
  • 4,071
  • 1
  • 23
  • 19