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

1
  • pls do this : c = conn.cursor() Commented Mar 12, 2015 at 11:53

2 Answers 2

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))
Sign up to request clarification or add additional context in comments.

4 Comments

After fixing this I now also receive a new error as shown above
@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.
I don't see anything wrong with being a "Help Vampire" if that is ultimately the purpose of the site.
@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))
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.