-6

我写了这个程序:

con=kinterbasdb.create_database("create database 'D:/ThirdTask/test.db' user 'sysdba' password 'masterkey'")
conn=kinterbasdb.connect(host='localhost',database='D:/ThirdTask/test.db', user='sysdba', password='masterkey')
cur=con.cursor()
s="""
 create table CLIENTS
  (        
     ID   FMTBCD PRIMARY KEY,
     COMPANY   CHAR(50),
     LAST NAME   CHAR(50),
     FIRST NAME   CHAR(50),
     E-MAIL ADDRESS   CHAR(50),
     JOB TITLE   CHAR(50),
     BUSINESS PHONE   CHAR(25),
     HOME PHONE   CHAR(25),
     MOBILE PHONE   CHAR(25),
     FAX NUMBER   CHAR(25),
     ADDRESS   MEMO,
     CITY   CHAR(50),
     STATE/PROVINCE   CHAR(50),
     ZIP/POSTAL CODE   CHAR(15),
     COUNTRY/REGION   CHAR(50),
     WEB-SITE   CHAR(25),
     NOTES   MEMO,
     INCLUDING   BLOB
     )"""
     print s
     cur.execute(s)
     con.close()
     conn.close()

编译器给出以下错误:

 create table CLIENTS
 (
    ID   FMTBCD PRIMARY KEY,
    COMPANY   CHAR(50),
    LAST NAME   CHAR(50),
    FIRST NAME   CHAR(50),
    E-MAIL ADDRESS   CHAR(50),
    JOB TITLE   CHAR(50),
    BUSINESS PHONE   CHAR(25),
    HOME PHONE   CHAR(25),
    MOBILE PHONE   CHAR(25),
    FAX NUMBER   CHAR(25),
    ADDRESS   MEMO,
    CITY   CHAR(50),
    STATE/PROVINCE   CHAR(50),
    ZIP/POSTAL CODE   CHAR(15),
    COUNTRY/REGION   CHAR(50),
    WEB-SITE   CHAR(25),
    NOTES   MEMO,
    INCLUDING   BLOB
    )

Traceback (most recent call last):
File "D:\ThirdTask\connect.py", line 119, in <module>
      cur.execute(s)
ProgrammingError: (-104, 'isc_dsql_prepare: \n  Dynamic SQL Error\n  SQL error code = -104\n  Token unknown - line 6, column 16\n  CHAR')

错误的原因可能是什么?

4

1 回答 1

2

您正在使用空格以及-破折号和\斜杠定义列名。将这些名称放在双引号中:

s="""
 create table CLIENTS
  (        
     ID   FMTBCD PRIMARY KEY,
     COMPANY   CHAR(50),
     "LAST NAME"   CHAR(50),
     "FIRST NAME"   CHAR(50),
     "E-MAIL ADDRESS"   CHAR(50),
     "JOB TITLE"   CHAR(50),
     "BUSINESS PHONE"   CHAR(25),
     "HOME PHONE"   CHAR(25),
     "MOBILE PHONE"   CHAR(25),
     "FAX NUMBER"   CHAR(25),
     ADDRESS   MEMO,
     CITY   CHAR(50),
     "STATE/PROVINCE"   CHAR(50),
     "ZIP/POSTAL CODE"   CHAR(15),
     "COUNTRY/REGION"   CHAR(50),
     "WEB-SITE"   CHAR(25),
     NOTES   MEMO,
     INCLUDING   BLOB
     )"""

Firebird 非常严格地遵守 ANSI SQL 标准,请参阅Interbase/Firebird 标识符以了解有关可以使用和不使用引号定义哪些标识符的详细规则。

于 2012-09-24T10:34:14.817 回答