Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sqlite3,python,创建表-near/&引用;:语法错误_Python_Sqlite - Fatal编程技术网

sqlite3,python,创建表-near/&引用;:语法错误

sqlite3,python,创建表-near/&引用;:语法错误,python,sqlite,Python,Sqlite,我只想使用python和sqlite在数据库中创建一个表。但是,我需要用户提供我的db名称。我设法做到了这一点: #!/usr/bin/env python # -*- coding: utf-8 -*- import sqlite3 as sqlite import sys, os sys.path.append(os.path.join(os.path.dirname(__file__), '..')) def create_table(dbname): sql = '

我只想使用python和sqlite在数据库中创建一个表。但是,我需要用户提供我的db名称。我设法做到了这一点:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sqlite3 as sqlite
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))


def create_table(dbname):
        sql = '''\
                    CREATE TABLE IF NOT EXISTS ''' + dbname + ''' (
                        ID INTEGER PRIMARY KEY NOT NULL,
                        FIRSTNAME TEXT,
                        LASTNAME TEXT,
                        ACADEMIC_DEGREE TEXT,
                        DISCIPLINE TEXT
                '''

        connection = sqlite.connect(dbname, check_same_thread=False)
        try:
            with connection:
                cursor = connection.cursor()
                try:
                    cursor.execute(sql)
                except sqlite.DatabaseError, dbe:
                    print dbe
        finally:
            connection.close()

tmp = os.path.split(os.path.dirname(os.path.abspath(__file__)))
path = os.path.join(tmp[0], 'researchers.sql')
create_table(path)
然而,我的代码并没有创建一个表,只是给我一条错误消息:
near/:syntax error
。这个代码怎么了?python提到的语法错误在哪里,如何解决

此代码有效:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sqlite3 as sqlite
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))


def create_table(dbname):
        sql = '''
                    CREATE TABLE IF NOT EXISTS ''' + str(dbname.split('.')[0]) + ''' (
                        ID INTEGER PRIMARY KEY NOT NULL,
                        FIRSTNAME TEXT,
                        LASTNAME TEXT,
                        ACADEMIC_DEGREE TEXT,
                        DISCIPLINE TEXT
                    )
                '''

        connection = sqlite.connect(dbname, check_same_thread=False)
        try:
            with connection:
                cursor = connection.cursor()
                try:
                    cursor.execute(sql)
                except sqlite.DatabaseError, dbe:
                    print dbe
        finally:
            connection.close()

create_table('researchers.sql')

表名不能包含任意字符,当然不是
/
,因此您不能
创建表(路径)
。选择一个简单的表名,如“研究者”。

用此替换sql变量

 sql = '''
                    CREATE TABLE IF NOT EXISTS ''' + dbname + ''' (
                        ID INTEGER PRIMARY KEY NOT NULL,
                        FIRSTNAME TEXT,
                        LASTNAME TEXT,
                        ACADEMIC_DEGREE TEXT,
                        DISCIPLINE TEXT
                    )
                '''

这看起来不像是您关闭了
创建表
语句,错误来自
'
中的第一个字符,我也不确定该字符是否有效