1

This is my code (from the "Python for Data Analysis" book):

import sqlite3

query = """ CREATE TABLE test (a VARCHAR(20), b VARCHAR(20)), c REAL, d INTEGER); """
con = sqlite3.connect('mydata.sqlite')
con.execute(query)

This is the error I get:

OperationalError                          Traceback (most recent call last)
<ipython-input-223-702795376e74> in <module>()
      1 con = sqlite3.connect('mydata.sqlite')
----> 2 con.execute(query)

OperationalError: near ",": syntax error

I tried with single and triple quotes around the code, checked the commas, but I can't figure out whats wrong.

4 Answers 4

2

Did you check the brackets? I can see 3 opening ( and 4 closing ). Looks like you need to remove one of the two closing for b VARCHAR(20))

Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Thank you
1

Always count your brackets and no need for triple " here.

import sqlite3

query = " CREATE TABLE test (a VARCHAR(20), b VARCHAR(20), c REAL, d INTEGER); "
con = sqlite3.connect('mydata.sqlite')
con.execute(query)

Comments

1

Your are using Pandas - you don't need to care of "create table ..." things. Pandas does it for you:

In [100]: df = pd.DataFrame(np.random.randint(5,size=(5,3)), columns=list('abc'))

In [101]: df
Out[101]:
   a  b  c
0  1  0  0
1  0  0  3
2  0  4  1
3  0  2  0
4  0  3  1

In [102]: conn = sqlite3.connect('c:/temp/test.sqlite')

In [103]: df.to_sql('test', conn, index=False)

In [104]: df2 = pd.read_sql('select * from test', conn)

In [105]: df2
Out[105]:
   a  b  c
0  1  0  0
1  0  0  3
2  0  4  1
3  0  2  0
4  0  3  1

1 Comment

Thanks, I didn't know that. I'm gonna look into that
1

Try this

query = "CREATE TABLE test (a VARCHAR(20), b VARCHAR(20), c REAL, d INTEGER)";

instead of yours

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.