adsdb 插入
adsdb INSERT INTO
我有一个可以使用 Advantage Data Architect 修改的 ADT。
不过我希望能够使用 adsdb 修改 table。我创建了 table 使用;
cnxn = adsdb.connect(DataSource='c:/Python27/', ServerType='1')
cursor = cnxn.cursor()
cursor.execute('CREATE TABLE Persons (PersonID INTEGER, LastName CHAR(100), FirstName CHAR(100))'
我可以使用以下方法将数据插入 PersonsID 字段;
cursor.execute('INSERT INTO Persons (PersonID) VALUES (01)')
但是试图将数据插入到char类型的列中;
cursor.execute('INSERT INTO Persons (LastName) VALUES ("Smith")')
我收到错误;
adsdb.OperationalError: Error 7200: AQE Error: State = S0000; NativeError = 2121; [iAnywhere Solutions][Advantage SQL Engine]Column not found: Smith -- Location of error in the SQL statement is: 40
我已经尝试在 VALUE 字段中使用单引号和不使用引号,但仍然出现错误。我有 Google 提供的错误代码,但我找不到解决方案。
在 ADS 中 SQL(实际上在 ANSI-SQL 中)字符串(CHAR 类型)值 have to be enclosed in single quotes:
INSERT INTO Persons (LastName) VALUES ('Smith')
In Python a string literal既可以写成单引号也可以写成双引号:
print("Hello")
print('Hello')
由于正确的 SQL 语句不包含双引号,因此使用双引号字符串文字会更容易:
cursor.execute("INSERT INTO Persons (LastName) VALUES ('Smith')")
如果要使用单引号字符串文字,则必须转义文字中的单引号:
cursor.execute('INSERT INTO Persons (LastName) VALUES (\'Smith\')')
但我不会那样做,因为使用字符串插值或字符串连接将值放入 SQL 语句 is very dangerous 并可能导致 SQL 注入。
正确的方法是使用参数:
cursor.execute('INSERT INTO Persons (LastName) VALUES (?)', 'Smith')
顺便说一句:"Persons" 是一个糟糕的 table 名字(person 的复数形式是 people,你应该使用 "person" 或 "people" 作为 table 姓名).
我有一个可以使用 Advantage Data Architect 修改的 ADT。
不过我希望能够使用 adsdb 修改 table。我创建了 table 使用;
cnxn = adsdb.connect(DataSource='c:/Python27/', ServerType='1')
cursor = cnxn.cursor()
cursor.execute('CREATE TABLE Persons (PersonID INTEGER, LastName CHAR(100), FirstName CHAR(100))'
我可以使用以下方法将数据插入 PersonsID 字段;
cursor.execute('INSERT INTO Persons (PersonID) VALUES (01)')
但是试图将数据插入到char类型的列中;
cursor.execute('INSERT INTO Persons (LastName) VALUES ("Smith")')
我收到错误;
adsdb.OperationalError: Error 7200: AQE Error: State = S0000; NativeError = 2121; [iAnywhere Solutions][Advantage SQL Engine]Column not found: Smith -- Location of error in the SQL statement is: 40
我已经尝试在 VALUE 字段中使用单引号和不使用引号,但仍然出现错误。我有 Google 提供的错误代码,但我找不到解决方案。
在 ADS 中 SQL(实际上在 ANSI-SQL 中)字符串(CHAR 类型)值 have to be enclosed in single quotes:
INSERT INTO Persons (LastName) VALUES ('Smith')
In Python a string literal既可以写成单引号也可以写成双引号:
print("Hello")
print('Hello')
由于正确的 SQL 语句不包含双引号,因此使用双引号字符串文字会更容易:
cursor.execute("INSERT INTO Persons (LastName) VALUES ('Smith')")
如果要使用单引号字符串文字,则必须转义文字中的单引号:
cursor.execute('INSERT INTO Persons (LastName) VALUES (\'Smith\')')
但我不会那样做,因为使用字符串插值或字符串连接将值放入 SQL 语句 is very dangerous 并可能导致 SQL 注入。
正确的方法是使用参数:
cursor.execute('INSERT INTO Persons (LastName) VALUES (?)', 'Smith')
顺便说一句:"Persons" 是一个糟糕的 table 名字(person 的复数形式是 people,你应该使用 "person" 或 "people" 作为 table 姓名).