zoukankan      html  css  js  c++  java
  • pymysql原生语句案例

     # 1. 查询所有商品信息
     def __fetch_all_info(self):
         cur = self.db_connect.cursor()
         sql_str = '''select * from goods'''
         cur.execute(sql_str)
         result = cur.fetchall()
         self.__show_query_result(result)
         cur.close()
    
    # 2. 查询所有包含商品的分类
     def __fetch_cate_of_goods(self):
         cur = self.db_connect.cursor()
         sql_str = '''select distinct good_cates.name from goods inner join good_cates on goods.cate_id = good_cates.id;'''
         # sql_str = ''' select name from good_cates where id in (select distinct cate_id from goods); '''
         cur.execute(sql_str)
         result = cur.fetchall()
         self.__show_query_result(result)
         cur.close()
    
     # 3. 添加商品分类
     def __add_new_cate(self):
         new_cate = input('请输入一个新商品分类:')
         sql_str = ''' insert into good_cates(name) values("%s") ''' % new_cate
         cur = self.db_connect.cursor()
         cur.execute(sql_str)
         self.db_connect.commit()
         cur.close()
    
     # 4. 将所有商品价格加1000
     def __update_price(self):
         cur = self.db_connect.cursor()
         sql_str = ''' update goods set price = price + 1000'''
         cur.execute(sql_str)
         self.db_connect.commit()
         cur.close()
    
    # 5. 将所有笔记本的分类改为超级本
    def __update_cate(self):
        cur = self.db_connect.cursor()
        sql_str = '''update goods set cate_id = (select id from good_cates where name = '超级本') where name like '%笔记本%';'''
        cur.execute(sql_str)
        self.db_connect.commit()
        cur.close()
    
    # 6. 根据id查询商品信息
    def __fetch_info_with_id(self):
        s_id = input('请输入一个商品ID:')
        sql_str = ''' select * from goods where id = %s ''' % s_id
        print(sql_str)
        cur = self.db_connect.cursor()
        cur.execute(sql_str)
        result = cur.fetchall()
        self.__show_query_result(result)
        cur.close()
    
    # 7. 根据id查询商品信息安全方式
    def __fetch_info_with_id_safe(self):
        s_id = input('请输入一个商品ID:')
        sql_str = ''' select * from goods where id = %s '''
        print(sql_str)
        cur = self.db_connect.cursor()
        cur.execute(sql_str, (s_id,))
        result = cur.fetchall()
        self.__show_query_result(result)
        cur.close()
  • 相关阅读:
    linux下文件结束符
    【转】跟我学Kafka之NIO通信机制
    【转】 详解Kafka生产者Producer配置
    【转】项目延期的⑦大因素
    (转)EOSIO开发(三)钱包、账户与账户权限之概念篇
    CentOS里alias命令
    (转)EOSIO开发(一)使用Docker构建本地环境
    Marathon自动扩缩容(marathon-lb-autoscale)
    (转)Springboot日志配置(超详细,推荐)
    Spring Boot下的lombok安装以及使用简介
  • 原文地址:https://www.cnblogs.com/wwr3569/p/13966359.html
Copyright © 2011-2022 走看看