首页 > 数据库 >python 操作Oracle 自关联表进行树结构复制算法

python 操作Oracle 自关联表进行树结构复制算法

时间:2022-11-23 22:34:32浏览次数:41  
标签:树结构 python self pid CheckItemTable cursor Oracle id select


 最近一个项目中,用关系型表来存储树型结构,其中有一段树节点复制的算法,典型的递归运用,可作为递归算法参考练习。

def CheckItem_GET_ById(self, dataid):
"""
通过id取完整记录
:param dataid:数据id
:return:fieldlabel, pid, fieldtype, required, imglimit, dsporder, id, bz
"""
c = self.conn.cursor()
keys = self.CheckItemTableColumns.keys()
cursor = c.execute(
f"select {','.join(keys)} "
f"from {self.CheckItemTable} where id = :1", (dataid,))
return cursor.fetchone()


def CheckItem_ADD_CopyTree(self, from_id, to_pid, added_list):
if from_id in added_list:
return added_list # 排除掉新添加的id,又作为源来添加,避免无限递归.
c = self.conn.cursor()
srcid = from_id
toid = to_pid
if toid <=0: # 复制目的不存在,需要新建为方案
c.execute(f"insert into {self.CheckItemTable} (pid,fieldlabel,fieldtype,required,imglimit,dsporder,bz) "
f"select 0,fieldlabel||' 复制',fieldtype,required,imglimit,dsporder,bz "
f"from {self.CheckItemTable} where id = :1", (srcid,))
lastrowid = c.lastrowid
cursor = c.execute(f"select id from {self.CheckItemTable} where rowid=:1 ", (lastrowid,))
row = cursor.fetchone()
new_pid = row[0]
added_list.append(new_pid)
else:
c.execute(f"insert into {self.CheckItemTable} (pid,fieldlabel,fieldtype,required,imglimit,dsporder,bz) "
f"select :1,fieldlabel,fieldtype,required,imglimit,dsporder,bz "
f"from {self.CheckItemTable} where id = :2", (toid, srcid))
lastrowid = c.lastrowid
cursor = c.execute(f"select id from {self.CheckItemTable} where rowid=:1 ", (lastrowid,))
row = cursor.fetchone()
new_pid = row[0]
c.execute(f"insert into {self.ChooseItemTable} (mainid,selectname,selectvalue) "
f"select :1,selectname,selectvalue from {self.ChooseItemTable} where mainid=:2", (new_pid, srcid))

added_list.append(new_pid)


cids = self.CheckItem_GET_ByPid(srcid)
for nid in cids:
self.CheckItem_ADD_CopyTree(nid[0], new_pid, added_list)
self.conn.commit()

标签:树结构,python,self,pid,CheckItemTable,cursor,Oracle,id,select
From: https://blog.51cto.com/fangkailove/5881786

相关文章

  • python中的列表和元组
    #1.列表的格式#[数据1,数据2,数据3,···]#列表可变数据类型#列表可以存储多个数据,数据之间的逗号以英文逗号分隔#列表可以存储不同类型数据,但一般存储同一数......
  • python 读写西门子PLC例子
    client.py 存取 西门子PLC数据的客户端例子 #!/usr/bin/envpython#-*-coding:utf-8-*-#Project:pi4b_aliyuniot#File:snap_client.py#Author:Long.Xu<fa......
  • python subprocess.cal调用wkhtmltohtml中遇到的问题
    最近项目中使用了wkhtmltohtml 工具,这个工具转换pdf功能非常方便。但是在python作为命令来调用时有些要注意的地方,另外还有一些和字体相关的问题要注意。一.字体问题......
  • python requests 上传文件
    起因为了测试企业微信上传临时素材接口,我写了一段python脚本,使用的是requests 库,按照官方接口说明: 要构造相应表单的栏位,才能指定上传后文件的显示名称,于是我测试脚本如下......
  • python http.server 的测试和常见问题解决方法
    一.测试准备先分别写一个简单httpserver 和一个html文件。html文件只是引入了jquery, 后面测试用<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8">......
  • SAP oracle 复制新实例后数据库远程连接报错 ora-01031
    问题:oracle 服务器本地用sqlplus 可以用sys 作为dba 登入,但是用 pl/sql登入时就报ORA-01031insufficientprivileges错误。如下图。 这个问题的原因是,......
  • python 编程技巧
     元组命名如何为元组中的每个元素命名,提高程序可读性案例:学生信息系统中数据为固定格式:(名字,年龄,性别,邮箱地址,......)学生数量很大为了减小存储开销,对每个学生信息用元......
  • Oracle-重建临时表空间 rebuild temporary tablespace
    查询用户的默认表空间/临时表空间TEMPselectuserid,username,account_status,to_char(created,'yyyymmdd'),default_tablespace,tem......
  • python初体验
    第一句代码print("helloworld")快速切换项目栏alt+1快速运行当前文件shift+ctrl+F10关闭控制台shift+ctrl+F4 注释:单行注释:#(快捷键ctrl+/)多行注释:""......
  • 进入python的世界_day37_数据库——mysql字符编码配置、数据引擎配置、字段类型及语法
    一、字符编码与配置文件输入\s可以看到一些信息如果想要永久修改编码配置,可以在mysql的文件夹根目录下自己复制并改名一个my.ini去操作配置文件[mysqld]character-se......