1

INMySQL にバインドされた Python コードをハッキングするときは、名前付きのプレースホルダーを使用することを好みますが、この句では正しく取得できないようです。例:

con = MySQLdb.connect(db='test', user='test')
cur = con.cursor()

あまりにも単純な 3 つの例:

# (#1) works fine, but I want placeholders.
cur.execute( """ update test
                 set    i = 999
                 where  SNO in (1, 2) """)

# (#2) works fine too, but still not enough placeholders.
cur.execute( """ update test
                 set    i = %(i)s
                 where  SNO in (1, 2) """, {'i' : 999})

# (#3) works, but did not pass the beauty check...
cur.execute( """ update test
                 set    i = %(i)s
                 where  SNO in ( %(a)s, %(b)s ) """, {'i' : 99,
                                                      'a' : 1,
                                                      'b' : 2})

これは私が本当に望んでいるものですが、次のように失敗します: Operand should contain 1 column(s)

# (#4) This one fails with: _mysql_exceptions.OperationalError: (1241, 'Operand should contain 1 column(s)')
cur.execute( """ update test
                 set    i = %(i)s
                 where  SNO in ( %(foo)s ) """, {'i'   : 999,
                                                 'foo' : [1, 2]})

どうやらもっと魔力が必要らしい。Python でループを実装してアプリケーションに問題を移すのは簡単ですが、私はむしろそれを避けたいと思います。

そうそう、パフォーマンスも重要です。

4

1 に答える 1