1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pymysqlのLIKEのパーセント

Last updated at Posted at 2020-11-14

python3.x

ライブラリpymysqlを使って
connection = pymysql.connect(・・・)
部分一致のLIKE文を動かしたかったのだが

with connection.cursor() as cursor:
   cursor.execute("SELECT c1 FROM ttt WHERE c2 LIKE 'abc%'", ())
   ary = cursor.fetchall()

と書くと

TypeError: not enough arguments for format string

となる。
pymysqlに%を無視してもらうために
%を%でエスケープしないとダメらしい。

with connection.cursor() as cursor:
   cursor.execute("SELECT c1 FROM ttt WHERE c2 LIKE 'abc%%'", ())
   ary = cursor.fetchall()

で動いた。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?