python操作数据库(cursor游标讲解)


cursor游标简介

游标:执行sql语句后,取出返回结果的接口。提供游标接口,借助游标一行一行取出数据,直到最后一行,游标为空。

一、案例说明

在这里插入图片描述
图示说明:
在这里插入图片描述
如果不使用游标功能,直接使用select查询,会一次性将结果集打印到屏幕上,你无法针对结果集做第二次编程

案例:student表
在这里插入图片描述
使用fetchall()方法,获取SQL查询结果集中的数据

db = pymysql.connect(host='localhost',user='root',db='huangwei',
           password='123456',port=3306,charset='utf8')
cursor = db.cursor()
cursor.execute('select sname,ssex from student')
aa = cursor.fetchall()
# print(aa)
for a,b in aa:
  if b == "女":
    a = "我叫{},我是一个学生!".format(a)
    print(a)
db.close()

结果如下:
在这里插入图片描述

二、使用步骤

① 连接数据库,创建一个数据库对象

db = pymysql.connect(host='localhost',user='root',db='huangwei',
          password='123456',port=3306,charset='utf8')

② 开启游标功能,创建游标对象

# 这里使用的是数据库对象db中的cursor()方法,
cursor = db.cursor()

③ 使用execute()方法,执行SQL语句

cursor.execute('select sname,ssex from student')

④ 使用fetchone()或fetchall(),fetchmany(n)获取数据

# 一次性获取一条数据
a = cursor.fetchone()
# 一次性获取所有数据
a = cursor.fetchall()
# 获取n条数据
a = cursor.fetchmany(n)

⑤ 断开数据库,释放资源

db.close()

三、源码

def sqlMigrate():
    source_db = cx_Oracle.connect('********', '********', '133.0.**.**:**/xydb')  # 源库
    target_db = pymysql.connect('10.37.6.**', 'root', '********', 'enterprise_internet')  # 目标库

    # 2.创建游标
    cur_select = source_db.cursor()  # 源库查询对象
    cur_insert = target_db.cursor()  # 目标库插入对象

    # 3.执行SQL
    sql_select_product = "SELECT product_id, product_name, flag1, flag2 FROM xy_report.ldl_zq_zw_hlw_product "

    cur_select.execute(sql_select_product)

    # 获取源表有多少个列
    desc = cur_select.description
    col_len = len(desc)

    # MySQL批量插入语法是 insert into tb_name values(%s,%s,%s,%s)
    val_str = ''
    for i in range(1, col_len):
        val_str = val_str + '%s' + ','
    val_str += '%s'

    # 拼接insert into 目标表 values  #目标表插入语句
    sql_insert_product = 'insert into bossInfo_product(product_id, product_name,flag_provinces,flag_hlzw)' \
                         + ' values(' + val_str + ')'

    print('开始执行product:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    cur_select.execute(sql_select_product)  # 执行
    while True:
        # 每次获取100行,由cur_select.arraysize值决定,MySQL fetchmany 返回的是 tuple 数据类型 所以用list做类型转换
        rows = list(cur_select.fetchmany(10))
        # 批量插入每次100行,需要注意的是 rows 必须是 list [] 数据类型
        cur_insert.executemany(sql_insert_product, rows)
        target_db.commit()  # 提交
        if not rows:
            break  # 中断循环
    print('执行成功product:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

    # order_status
    sql_select_order_status = "select attr_value,attr_value_name,attr_value_desc " \
                              "from cpcp_spec.attr_value@to_crm30db where attr_id ='9990010422'"

    sql_insert_order_status = 'insert into bossInfo_order_status(order_value, value_name,value_desc)' \
                              + ' values(%s,%s,%s)'

    print('开始执行order_status:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    cur_select.execute(sql_select_order_status)  # 执行
    while True:
        # 每次获取100行,由cur_select.arraysize值决定,MySQL fetchmany 返回的是 tuple 数据类型 所以用list做类型转换
        rows = list(cur_select.fetchmany(10))
        # 批量插入每次100行,需要注意的是 rows 必须是 list [] 数据类型
        cur_insert.executemany(sql_insert_order_status, rows)
        target_db.commit()  # 提交
        if not rows:
            break  # 中断循环

    # Service_Offer
    print('开始执行Service_Offer:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

    sql_select_service_offer = "SELECT service_offer_id,service_offer_name from CPCP_SPEC.SERVICE_OFFER@to_crm30db"

    sql_insert_service_offer = 'insert into bossInfo_service_offer(service_offer_id, service_offer_name)' \
                               + ' values(%s,%s)'
    cur_select.execute(sql_select_service_offer)  # 执行
    rows = list(cur_select.fetchall())
    cur_insert.executemany(sql_insert_service_offer, rows)
    target_db.commit()  # 提交

    # common_region
    print('开始执行common_region:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

    sql_select_common_region = "select common_region_id, par_region_id, region_nbr, region_name " \
                               "from cpcp_spec.common_region@to_crm30db where lan_id = 8420600"

    sql_insert_common_region = 'insert into bossInfo_common_region(common_region_id, par_region_id, region_nbr, region_name )' \
                               + ' values(%s,%s,%s,%s)'
    cur_select.execute(sql_select_common_region)  # 执行
    rows = list(cur_select.fetchall())
    cur_insert.executemany(sql_insert_common_region, rows)
    target_db.commit()  # 提交

    cur_select.close()
    cur_insert.close()
    source_db.close()
    target_db.close()
    print('执行成功:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
def sqlMigrate_boss():
       source_db = cx_Oracle.connect('********', '********', '133.0.**.**:**/xydb')  # 源库
    target_db = pymysql.connect('10.37.6.**', 'root', '********', 'enterprise_internet')  # 目标库
    # 2.创建游标
    cur_select = source_db.cursor()  # 源库查询对象
    cur_insert = target_db.cursor()  # 目标库插入对象

    # 3.执行SQL
    sql_select_prod_inst = "SELECT distinct prod_inst_id,order_item_id,acc_num,address_desc,prod_id," \
                           "owner_cust_id,use_cust_id,region_id,create_date " \
                           "FROM ORD_SO.ORD_PROD_INST@to_crm30db where prod_id in (" \
                           "select product_id from lh_zq_lx_product) " \
                           "and lan_id = '8420600'" \
                           "and to_char(create_date, 'yyyy-mm-dd') = to_char(sysdate - 30, 'yyyy-mm-dd')"

    cur_select.execute(sql_select_prod_inst)
    # 获取源表有多少个列
    desc = cur_select.description
    col_len = len(desc)
    # MySQL批量插入语法是 insert into tb_name values(%s,%s,%s,%s)
    val_str = ''
    for i in range(1, col_len):
        val_str = val_str + '%s' + ','
    val_str += '%s'
    # 拼接insert into  prod_inst 目标表 values
    sql_insert_prod_inst = 'insert into bossInfo_prod_inst(prod_inst_id,order_item_id,acc_num,address_desc,prod_id,' \
                           'owner_cust_id,use_cust_id,region_id,create_date)' \
                           + ' values(' + val_str + ')'
    # 是否重复
    sql_insert_prod_inst_r = 'select prod_inst_id,acc_num from bossInfo_prod_inst'

    # insert into order_item 目标表 values
    sql_insert_order_item = 'insert into bossInfo_order_item(prod_inst_id,order_item_id,acc_num) values(%s,%s,%s)'
    sql_insert_order_item_r = 'select prod_inst_id,order_item_id from bossInfo_order_item'

    # insert into contact_cust_info 目标表 values
    sql_insert_contact_cust_info = 'insert into bossInfo_contact_cust_info(owner_cust_id, use_cust_id) values(%s,%s)'
    sql_insert_contact_cust_info_r = 'select owner_cust_id,use_cust_id from bossInfo_contact_cust_info'

    # insert into contact_manager_info 目标表 values
    sql_insert_contact_manager_info = 'insert into bossInfo_contact_manager_info(prod_inst_id,acc_num ) values(%s,%s)'
    sql_insert_contact_manager_info_r = 'select prod_inst_id,acc_num from bossInfo_contact_manager_info'

    print('开始执行prod_inst:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    cur_select.execute(sql_select_prod_inst)
    while True:
        # 每次获取100行,由cur_select.arraysize值决定,MySQL fetchmany 返回的是 tuple 数据类型 所以用list做类型转换
        rows = list(cur_select.fetchmany(1))
        if not rows:
            break  # 中断循环
        # sql_insert_prod_inst
        cur_insert.execute(sql_insert_prod_inst_r)
        data = cur_insert.fetchall()
        data_insert = str(rows[0][0]), rows[0][2]
        rows_insert = [(str(rows[0][0]), str(rows[0][2]))]
        if data_insert not in data:
            cur_insert.executemany(sql_insert_prod_inst, rows)
            # sql_insert_contact_manager_info
            cur_insert.executemany(sql_insert_contact_manager_info, rows_insert)
            target_db.commit()  # 提交

        # sql_insert_order_item
        cur_insert.execute(sql_insert_order_item_r)
        data = cur_insert.fetchall()
        data_insert = str(rows[0][0]), str(rows[0][1])
        rows_insert = [(str(rows[0][0]), str(rows[0][1]), str(rows[0][2]))]
        if data_insert not in data:
            cur_insert.executemany(sql_insert_order_item, rows_insert)
            target_db.commit()  # 提交

        # sql_insert_contact_cust_info
        cur_insert.execute(sql_insert_contact_cust_info_r)
        data = cur_insert.fetchall()
        data_insert = str(rows[0][5]), str(rows[0][6])
        rows_insert = [(str(rows[0][5]), str(rows[0][6]))]
        if data_insert not in data:
            cur_insert.executemany(sql_insert_contact_cust_info, rows_insert)
            target_db.commit()  # 提交

        # sql_insert_contact_cust_info
        # cur_insert.execute(sql_insert_contact_manager_info_r)
        # data = cur_insert.fetchall()
        # data_insert = str(rows[0][0]), rows[0][2]
        # rows_insert = [(str(rows[0][0]), str(rows[0][2]))]
        # if data_insert not in data:
        #     cur_insert.executemany(sql_insert_contact_manager_info, rows_insert)
        #     target_db.commit()  # 提交

    print('开始执行contact_cust_info_update:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

    cur_insert.execute('select owner_cust_id,use_cust_id from bossInfo_contact_cust_info ')
    data_contact = cur_insert.fetchall()
    for owner, use in data_contact:
        # owner_cust_name
        sql_select_owner_cust_name = "select cust_name,region_id from  CUS_CUST.XY_CUSTOMER@to_crm30db  " \
                                     " where cust_id = " + owner
        # use_cust_name
        sql_select_use_cust_name = "select cust_name from  CUS_CUST.XY_CUSTOMER@to_crm30db  " \
                                   " where cust_id = " + use
        # cust_mobile_phone
        sql_select_cust_mobile_phone = "select mobile_phone from CUS_CUST.CONTACTS_INFO@to_crm30db where  contact_id = (" \
                                       "select contact_id from CUS_CUST.CUST_CONTACT_INFO_REL@to_crm30db " \
                                       " where cust_id =" + owner + ")"

        # 收集结果集
        cur_select.execute(sql_select_owner_cust_name)
        rows_owner_cust_name = cur_select.fetchone()
        cur_select.execute(sql_select_use_cust_name)
        rows_use_cust_name = cur_select.fetchone()
        cur_select.execute(sql_select_cust_mobile_phone)
        rows_cust_mobile_phone = cur_select.fetchone()
        # 插入空值
        rows_cust_mobile_phone_str = rows_cust_mobile_phone[0] if rows_cust_mobile_phone[0] else ""
        # 更新 contact_cust_info
        sql_update_contact_cust_info = "UPDATE bossInfo_contact_cust_info set owner_cust_name =" + "'" + rows_owner_cust_name[0] +\
                                       "', use_cust_name =" + "'" + rows_use_cust_name[0] + \
                                       "', region_id =" + "'" + str(rows_owner_cust_name[1]) + \
                                       "' , cust_mobile_phone =" + "'" + rows_cust_mobile_phone_str + "'" + \
                                       " where owner_cust_id =" + owner
        cur_insert.execute(sql_update_contact_cust_info)
        target_db.commit()  # 提交

    print('开始执行contact_manager_info_update:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    cur_insert.execute('select prod_inst_id,acc_num from bossInfo_contact_manager_info where manager_mobile_phone = "" or manager_mobile_phone is null')
    data_contact = cur_insert.fetchall()
    for prod, acc in data_contact:
        # cust_manage
        sql_select_manager = "select attr_value from ord_so.ord_prod_inst_attr@to_crm30db " \
                                 " where prod_inst_id =" + prod + "and attr_id = 4604"

        # manage_mobile_phone
        sql_select_manage_mobile = "select attr_value from ord_so.ord_prod_inst_attr@to_crm30db " \
                                    " where prod_inst_id =" + prod + "and attr_id = 4605"

        # 收集结果集
        cur_select.execute(sql_select_manager)
        rows_manager = cur_select.fetchone()
        cur_select.execute(sql_select_manage_mobile)
        rows_manage_mobile = cur_select.fetchone()

        # 插入空值
        rows_manager_str = rows_manager[0] if rows_manager else ""
        rows_manager_mobile_str = rows_manage_mobile[0] if rows_manage_mobile else ""
        # 更新 contact_manager_info
        sql_update_contact_manager_info = "UPDATE bossInfo_contact_manager_info set cust_manager = " + "'" + rows_manager_str + "'" + \
                                          " where prod_inst_id =" + prod
        cur_insert.execute(sql_update_contact_manager_info)
        sql_update_contact_manager_info = "UPDATE bossInfo_contact_manager_info set manager_mobile_phone = " + "'" + rows_manager_mobile_str + "'"\
                                          + " where prod_inst_id =" + prod
        cur_insert.execute(sql_update_contact_manager_info)
        target_db.commit()  # 提交

    print('开始执行Order_Item_update:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    cur_insert.execute('select prod_inst_id,order_item_id from bossInfo_order_item where create_date = "" or create_date is null')
    data_order = cur_insert.fetchall()
    for prod, order in data_order:
        sql_select_order = "SELECT cust_order_id, status_cd, service_offer_id, service_offer_name, apply_obj_spec_name, create_staff_name, " \
                           "create_org_name, update_staff_name, update_org_name,accept_region_id,create_date FROM ORD_SO.ORDER_ITEM @ to_crm30db " \
                           "where order_item_id =" + order
        # 收集结果集
        cur_select.execute(sql_select_order)
        rows_order = cur_select.fetchone()
        for i in range(9):
            rows_order[i] if rows_order[i] else ""
        # 更新 order_item
        sql_update_order_item = "UPDATE bossInfo_order_item set cust_order_id = " + "'" + str(rows_order[0]) +\
                                                           "', status_cd =" + "'" + str(rows_order[1]) + \
                                                           "', service_offer_id =" + "'" + str(rows_order[2]) + \
                                                           "', service_offer_name =" + "'" + str(rows_order[3]) + \
                                                           "', apply_obj_spec_name =" + "'" + str(rows_order[4]) + \
                                                           "', create_staff_name =" + "'" + str(rows_order[5]) + \
                                                           "', create_org_name =" + "'" + str(rows_order[6]) + \
                                                           "', update_staff_name =" + "'" + str(rows_order[7]) + \
                                                           "', update_org_name =" + "'" + str(rows_order[8]) + \
                                                           "', accept_region_id =" + "'" + str(rows_order[9]) + \
                                                           "', create_date =" + "'" + str(rows_order[10]) + "'" + \
                                                           " where order_item_id =" + order
        cur_insert.execute(sql_update_order_item)
        target_db.commit()  # 提交

        # 更新 cust_order_nbr
        sql_select_order_nbr = "SELECT cust_order_nbr FROM ORD_SO.CUSTOMER_ORDER @ to_crm30db " \
                               "where accept_lan_id = '8420600' and cust_order_id =" + str(rows_order[0])
        cur_select.execute(sql_select_order_nbr)
        rows_order_nbr = cur_select.fetchone()
        print(rows_order_nbr)
        sql_update_order_nbr = "UPDATE bossInfo_order_item set cust_order_nbr =" + str(rows_order_nbr[0]) + \
                               " where cust_order_id =" + str(rows_order[0])
        cur_insert.execute(sql_update_order_nbr)
        target_db.commit()  # 提交

    print('执行成功:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

    cur_select.close()
    cur_insert.close()
    source_db.close()
    target_db.close()

猜你喜欢

转载自blog.csdn.net/qq_35911309/article/details/113128380