刘冠红 2021-08-18 12:09 采纳率: 0%
浏览 1005

python pymysql 相关 关于判断cursor.fetchone()是否为空

      for keys, values in data_frame.iterrows():
        new_dict = {
            '歌曲名': values['歌曲名'],
            '歌手名': values['歌手名'],
            '中央曲库ID': values['中央曲库歌曲ID']
        }
        if '伴奏' in str(values['歌曲名']):
            sql = 'select song_id from songs_ip where center_inst_ids like "%s" ' \
                  'and company_id=4 and deleted_at is null;' % values['中央曲库歌曲ID']
        else:
            sql = 'select song_id from songs_ip where center_ids like "%s" ' \
                  'and company_id=4 and deleted_at is null;' % values['中央曲库歌曲ID']

        cursor.execute(sql)
        if cursor.fetchone():
            print(cursor.fetchone())
            song_id = cursor.fetchone()
            print(song_id)
        else:
            print(0)

写了一串这样的代码,如果不判断cursor.fetchone()是否为空值,那么cursor.fetchone()可以打印出来,一旦判断之后全部为空不知道为什么.

  • 写回答

3条回答 默认 最新

  • python收藏家 2021-08-18 12:15
    关注

    cursor.fetchone() 只能执行一次,第一次可以先赋值

    tmp = cursor.fetchone()
    if tmp:
        print(tmp )
        song_id = tmp 
        print(song_id)
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 8月18日

悬赏问题

  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥100 关于#audobe audition#的问题,如何解决?
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯
  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥15 高通uboot 打印ubi init err 22