0

I am trying to generate tables based off the paramaters of a function. To generate the table I thought of passing a string of what I wanted to be executed as the following function:

conn = sqlite3.connect('tutorial.db')

c = conn.cursor()

def create_table(name, unix, datestamp, keyword, value):

    command = "CREATE TABLE IF NOT EXISTS " + name + "(" + unix + " REAL, " + datestamp + " TEXT, " + keyword + " TEXT, " + value + " REAL)"

    c.execute('CREATE TABLE' + command)

However when I run the command:

create_table('new','boy','girl','joy','joe')

I get error: Traceback (most recent call last): File "C:\Users\David\Documents\learn_sql_\learn_sql.py", line 22, in create_table('new','boy','girl','joy','joe') File "C:\Users\David\Documents\learn_sql_\learn_sql.py", line 12, in create_table c.execute('CREATE TABLE' + command) sqlite3.OperationalError: near "TABLECREATE": syntax error

Thoughts?

Thanks

2
  • which version python are you using?? Commented Nov 28, 2019 at 2:44
  • 1
    Replace temporarily your "c.execute" with "print" to see what you are sending to Sqlite. Commented Nov 28, 2019 at 2:44

1 Answer 1

0

try with this :

import sqlite3

conn = sqlite3.connect('tutorial.db')

c = conn.cursor()

def create_table(name, unix, datestamp, keyword, value):

    #c.execute('CREATE TABLE' + command)
    cmd = "CREATE TABLE IF NOT EXISTS %s(%s real,%s text,%s text,%s real)" % (name,unix,datestamp,keyword,value)
    print(cmd)
    c.execute(cmd)

create_table('new','boy','girl','joy','joe')
Sign up to request clarification or add additional context in comments.

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.