相关疑难解决方法(0)

奇怪的SQLAlchemy错误消息:TypeError:'dict'对象不支持索引

我使用手工制作的SQL来使用SqlAlchemy从PG数据库中获取数据.我正在尝试一个包含SQL运算符'%'的查询,并且似乎通过循环抛出SqlAlcjhemy:

sql = """
       SELECT DISTINCT u.name from user u
        INNER JOIN city c ON u.city_id = c.id
        WHERE c.designation=upper('fantasy') 
        AND c.id IN (select id from ref_geog where short_name LIKE '%opt')
      """

# The last line in the above statement throws the error mentioned in the title. 
# However if the last line is change to:
# AND c.id IN (select id from ref_geog where short_name = 'helloopt')
# the script runs correctly.
#
# I also tried double escaping …
Run Code Online (Sandbox Code Playgroud)

python postgresql sqlalchemy

119
推荐指数
4
解决办法
3万
查看次数

Python MySQLdb TypeError:并非在字符串格式化期间转换所有参数

运行此脚本时:

#! /usr/bin/env python
import MySQLdb as mdb
import sys    

class Test:
    def check(self, search):
        try:
            con = mdb.connect('localhost', 'root', 'password', 'recordsdb');

            cur = con.cursor()
            cur.execute( "SELECT * FROM records WHERE email LIKE '%s'", search )

            ver = cur.fetchone()

            print "Output : %s " % ver

        except mdb.Error, e:

            print "Error %d: %s" % (e.args[0],e.args[1])
            sys.exit(1)

        finally:    
            if con:    
                con.close()

test = Test()
test.check("test")
Run Code Online (Sandbox Code Playgroud)

我得到一个错误:

./lookup 
Traceback (most recent call last):
  File "./lookup", line 27, in <module>
    test.check("test")
  File "./lookup", …
Run Code Online (Sandbox Code Playgroud)

python python-2.7

38
推荐指数
3
解决办法
8万
查看次数

标签 统计

python ×2

postgresql ×1

python-2.7 ×1

sqlalchemy ×1