0
0

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 5 years have passed since last update.

pythonでデータベースにselectし、1行でも取得できたかをif文で判断させたいです。

Posted at

初めてQiitaを利用するので質問の仕方が下手になるかもしれません。
まずはコードを張ります
シンタックスハイライトの使い方が分からなかったので、見づらいです,すいません。

  #乱数生成
  runnum = random.randint(1,10)

# DBに接続
connector =psycopg2.connect(host="ec2-54-243-39-245.compute1.amazonaws.com", database="d1ha84jurcr3ff",user="vnlxcgzgpqcbrl",password="32db2fdd39e308d7dee47e7830d1fb7331d6f6968f7624b548d6d16e2bb4af06")
cursor = connector.cursor()

#IDが生成した乱数と一致する行を探す
cursor.execute("select id,namae from konkaimondai where id = %s",(runnum))

  #kekkaに取得した行を入れる
kekka = cursor.fetchall()

乱数と同じ数字のIDを持つ行が一行もないか確認したいです。
if kekka is None:
などで大丈夫なんでしょうか。
宜しくお願いします。

0
0
2

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?