尝试将转义字符插入表中会导致警告.
例如:
create table EscapeTest (text varchar(50));
insert into EscapeTest (text) values ('This is the first part \n And this is the second');
Run Code Online (Sandbox Code Playgroud)
产生警告:
WARNING: nonstandard use of escape in a string literal
Run Code Online (Sandbox Code Playgroud)
(使用PSQL 8.2)
有谁知道怎么解决这个问题?
我有以下代码,使用pscyopg2:
sql = 'select %s from %s where utctime > %s and utctime < %s order by utctime asc;'
data = (dataItems, voyage, dateRangeLower, dateRangeUpper)
rows = cur.mogrify(sql, data)
Run Code Online (Sandbox Code Playgroud)
这输出:
select 'waterTemp, airTemp, utctime' from 'ss2012_t02' where utctime > '2012-05-03T17:01:35+00:00'::timestamptz and utctime < '2012-05-01T17:01:35+00:00'::timestamptz order by utctime asc;
Run Code Online (Sandbox Code Playgroud)
当我执行它时,它会失败 - 这是可以理解的,因为表名周围的引号是非法的.
有没有办法合法地将表名作为参数传递,或者我是否需要执行(显式警告)字符串连接,即:
voyage = 'ss2012_t02'
sql = 'select %s from ' + voyage + ' where utctime > %s and utctime < %s order by utctime asc;'
Run Code Online (Sandbox Code Playgroud)
为任何见解干杯.
我一直得到这个
错误:psycopg2.ProgrammingError:“someentry”列不存在。
该错误表明该列someentry不存在,当someentry它不是列时,它只是一个要输入到数据库中的值。
这是给出错误的代码:
cur.execute('INSERT INTO {0!s} (ip_id, item) VALUES ({1!s}{2!s})'.format('mytable',1,'someentry'))
Run Code Online (Sandbox Code Playgroud)
这是我创建表的方法:
tablename = 'mytable'
command = """
CREATE TABLE IF NOT EXISTS {} (
ip_id SERIAL PRIMARY KEY,
item VARCHAR(255) NOT NULL
)
""".format(tablename)
cur.execute(command)
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试在 Python 的 psycopg 模块中的 PostgreSQL 查询中使用占位符。这是我正在使用的代码示例。
table.execute('SELECT * FROM table WHERE col2 = %s ORDER BY pID ASC LIMIT %s OFFSET %s;',(val1,val2,val3))
Run Code Online (Sandbox Code Playgroud)
我在某处读到,不可能对 LIMIT 和 OFFSET 使用这样的占位符,但是我应该对 WHERE = 使用这种占位符格式。
从python / postgresql / psycopg2中的用户输入安全地指定'order by'子句
有谁知道这个 sql 查询的正确占位符语法?谢谢!