Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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
Python sqlite3.OperationalError:靠近“;”:插入时语法错误_Python_Sqlite_Character Encoding - Fatal编程技术网

Python sqlite3.OperationalError:靠近“;”:插入时语法错误

Python sqlite3.OperationalError:靠近“;”:插入时语法错误,python,sqlite,character-encoding,Python,Sqlite,Character Encoding,我一直收到一个sqlite3.ERROR:near;,不知道为什么 我做错了什么?我正试图将记录从雇员元组列表插入数据库 阅读过其他类似错误,但与此错误不完全相同 connection = sqlite3.connect("company.db") cursor = connection.cursor() cursor.execute("""DROP TABLE employee;""") staff_data = [ ("William", "Shakespeare", "m", "1

我一直收到一个sqlite3.ERROR:near;,不知道为什么

我做错了什么?我正试图将记录从雇员元组列表插入数据库

阅读过其他类似错误,但与此错误不完全相同

connection = sqlite3.connect("company.db")
cursor = connection.cursor()



cursor.execute("""DROP TABLE employee;""")

staff_data = [ ("William", "Shakespeare", "m", "19611025"),
("Frank", "Schiller", "m", "19550817"), ("Jane", "Wall", "f", "1989-03-14")]


# Define an SQL command
sql_command = """
CREATE TABLE employee (
staff_number INTEGER PRIMARY KEY,
fname VARCHAR(20),
lname VARCHAR(30),
gender CHAR(1),
joining DATE,
birth_date DATE);
"""

# Execute command
cursor.execute(sql_command)

# Insert commands
sql_command = """ INSERT INTO employee (staff_number, fname, lname, gender,
birth_date) VALUES (NULL, "William", "Shakespeare", "m", "1961-10-25");"""

cursor.execute(sql_command)
print(sql_command)

for p in staff_data:
    format_str = """ INSERT INTO employee (staff_number, fname, lname, gender, birth_date) VALUES (NULL, "{first}", "{last}", "{gendera}", "{birthdate}"); """
    sql_command = format_str.format(first=p[0], last=p[1], gendera=p[2], birthdate=p[3])


print(sql_command)
cursor.execute(sql_command)



# DONT FORGET THIS TO SAVE CHANGES
connection.commit()

connection.close()
这是错误消息:

 INSERT INTO employee (staff_number, fname, lname, gender,
birth_date) VALUES (NULL, "William", "Shakespeare", "m", "1961-10-25");
 INSERT INTO employee (staff_number, fname, lname, gender, birth_date) VALUES (NULL, "Jane", "Wall", "f", "1989-03-14"); 
Traceback (most recent call last):
  File "sqliteuse0.py", line 53, in <module>
    cursor.execute(sql_command)
sqlite3.OperationalError: near ";": syntax error
那不是分号;。是;


请用分号替换它,然后重试。

我使用difflib.ndiff将键入的字符串与格式化生成的字符串进行比较,结果显示format_str缺少一个;。我删除并重新键入了它,它解决了这个问题。我相信这是由于直接从pdf复制示例代码造成的


问题已解决

哪一行出现此错误?此外,不能在双引号字符串中使用双引号。您需要对它们进行转义或使用单引号。显示完整的错误消息。@RocketHazmat:what?>>雅各布·克莱尔:哦?我在一个外壳中测试了它,但它不起作用。也许我做错了什么。你说得对。。。不要介意。
    format_str = """ INSERT INTO employee (staff_number, fname, lname, gender, birth_date) VALUES (NULL, "{first}", "{last}", "{gendera}", "{birthdate}"); """