简体   繁体   English

sqlite3 python生成表

[英]sqlite3 python generate tables

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 我收到错误消息:追溯(最近一次通话最近):文件“ C:\\ Users \\ David \\ Documents \\ learn_sql_ \\ learn_sql.py”,行22,位于create_table('new','boy','girl','joy ','joe')文件“ C:\\ Users \\ David \\ Documents \\ learn_sql_ \\ learn_sql.py”,在create_table中的第12行c.execute('CREATE TABLE'+ command)sqlite3.OperationalError:在“ TABLECREATE”附近:语法错误

Thoughts? 有什么想法吗?

Thanks 谢谢

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM