简体   繁体   English

我的sqlite3脚本怎么了?

[英]What is wrong with my sqlite3 script?

c = conn.cursor()                                                       
c.executescript("""                                                     
    CREATE TABLE IF NOT EXISTS dvdlist (                                    
        title text,                                                         
        barcode,                                                            
        added_date text,                                                    
        out numeric,                                                        
        borrower text,                                                      
        price real                                                          
        );                                                                  

    CREATE TABLE IF NOT EXISTS userdb (                                     
        username text,                                                      
        password text,                                                      
        address text,                                                       
        phone text,                                                         
        email text,                                                         
        borrowed integer
        );                                                                  

    CREATE TABLE IF NOT EXISTS staffdb (                                    
        username text,                                                      
        password text,                                                      
        address text,                                                       
        phone text,                                                         
        email text,                                                         
        group integer
        );                                                                  
    """)

Traceback says: 追溯说:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "shopdatabase.py", line 52, in __init__
    self.__db_init()
  File "shopdatabase.py", line 108, in __db_init
    """)
sqlite3.OperationalError: near "group": syntax error

I have looked up the data types in sqlite at http://www.sqlite.org/datatype3.html , I am not quite sure what is going on. 我在http://www.sqlite.org/datatype3.html的 sqlite中查询了数据类型,但我不太确定发生了什么。 Please help. 请帮忙。

Group is one of the reserved keywords . 组是保留关键字之一 You can't use it this way. 您不能以这种方式使用它。

And as @MartijnPieters states in his answer, you can wrap group with quotes to make the problem solved. 正如@MartijnPieters在他的回答中指出的那样,您可以用引号将group包装起来以解决问题。

group is a reserved keyword (it is part of the SQL language). group保留关键字 (它是SQL语言的一部分)。 You'll need to quote it if you want to use it as a column name: 如果要将其用作列名,则需要用引号引起来:

CREATE TABLE IF NOT EXISTS staffdb (                                    
    username text,                                                      
    password text,                                                      
    address text,                                                       
    phone text,                                                         
    email text,                                                         
    'group' integer
    );                                                                  

You can use single quotes ( 'group' ), double quotes ( "group" ) or square brackets or backticks ( [group] and `group` ), but the latter two forms are only supported for compatibility with non-compliant database engines. 您可以使用单引号( 'group' ),双引号( "group" )或方括号或反引号( [group]`group` ),但是仅支持后两种形式以与不兼容的数据库引擎兼容。

You'll have to quote the name anywhere you use it in a SQL statement: 您必须在SQL语句中使用的任何地方都引用该名称:

SELECT 'group' FROM staffdb WHERE username=?

for example. 例如。

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

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