一、查询数据库信息

方法一

import pymysql,json,time
def get_countOrder_status(counting_order):
    try:
        #定时1min查询数据库,检查入库任务状态是否为“DONE”完成
        status = ''
        while True:
            db = pymysql.connect(host=ip,port=port,user=user,passwd=pwd,db=wes_database,charset='utf8')  #打开数据库连接
            cursor = db.cursor()  # 使用cursor()方法获取操作游标
            # SQL 查询语句
            sql = ('''SELECT status FROM `wes`.`counting_order` WHERE `counting_no` LIKE (%s) LIMIT 0,1000''')%counting_order
            cursor.execute(sql)
            db.commit()   # 提交到数据库执行
            result = cursor.fetchone()
            #print(result)
            status = result[0]

             print("托盘的盘点状态为 "+status)

            if status=='FINISH':
                break

            db.close() # 关闭数据库连接

            time.sleep(10)
    except BaseException as e:
        print("error:",e)
        

方法二(更安全)

 		today_start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
        today_start_str = today_start.strftime('%Y-%m-%d %H:%M:%S')    #形如2025-08-19 00:00:00

		patient_name = '赵'
		doctor_name = '赵'

        sql = '''SELECT COUNT(*) FROM `assistant_task_record` WHERE `create_time` <= %s AND (`patient_name` LIKE %s OR `doctor_name` LIKE %s) '''
        params = (today_start_str, patient_name, doctor_name)

        cursor.execute(sql, params)
        db.commit()  # 提交到数据库执行
        result = cursor.fetchone()
        logging.info(f"查询数据库,获取包含的数量结果为:{result}")
        expected_count = result[0] if result else 0
        
        db.close()

二、更新数据库的值

def modifyInterfaceConfig(self,stateDecisionKey,stateDecisionValue):
        logger.info('修改数据库的config表中,stateDecisionKey和stateDecisionValue值')

        db = pymysql.connect(host=mysql_host,port=int(mysql_port),user=mysql_user,passwd=mysql_passwd,db='book',charset='utf8')  #打开数据库连接
        cursor = db.cursor()  # 使用cursor()方法获取操作游标
        # SQL 更新语句
        time.sleep(10)

        sql_1 = ("UPDATE book.config SET category_value=(%s) where id=2")%repr(stateDecisionKey)
        logger.info('执行的数据库命令:'+str(sql_1))
        cursor.execute(sql_1)
        time.sleep(10)
        sql_2 = ("UPDATE book.config SET category_value=(%s) where id=3")%repr(stateDecisionValue)
        cursor.execute(sql_2)
        db.commit()   # 提交到数据库执行
        #result = cursor.fetchone()
        #print(result)

        db.close() # 关闭数据库连接
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐