【问题标题】:Using Dictionaries(similar to Python dict)使用字典(类似于 Python 字典)
【发布时间】:2017-10-26 09:25:02
【问题描述】:

我想在 plsql 中创建一个字典对象。 (11克) 字典对象将具有三个字段:employee_id、manager_id、duration 由于查询时间,我不想创建表。我想访问内存中的项目。

我将使用批量收集将 200 万行插入到这个字典对象中。 然后我想访问像这样的字典对象:(334是员工ID) dictEmp[334].manager_id=8 或返回 dictEmp[334].duration

你能推荐任何文件吗? 谢谢

【问题讨论】:

  • 看起来很像 temporary table 的工作。
  • 为您的对象为您需要的每个索引创建dict 映射,并在您的婚姻路上。不要用额外的结构过度复杂化它,因为你不会获得比原生 Python dict 查找更好的性能。
  • 类似this?
  • 听起来像 PL/SQL 中的 associative array

标签: python dictionary plsql associative-array in-memory


【解决方案1】:

听起来像是 PL/SQL 中的关联数组。

declare 
    type employee_t is table of emp_demo%rowtype index by pls_integer;
    l_employees employee_t;
begin
    for r in (
        select * from emp_demo
    )
    loop
        l_employees(r.employee_id) := r;
    end loop;

    dbms_output.put_line('Manager for employee 334 is: ' || l_employees(334).manager_id);

    dbms_output.put_line('Manager''s manager is ' || l_employees(l_employees(334).manager_id).employee_name);
end;

测试数据:

create table emp_demo
( employee_id integer primary key
, employee_name varchar2(40)
, manager_id  integer
, hiredate date
, duration integer );

insert all
    into emp_demo values (8, 'Clark', 5, date '2015-04-02', 18)
    into emp_demo values (334, 'Jones', 8, date '2015-01-05', 12)
    into emp_demo values (455, 'Smith', 7, date '2015-02-04', 14)
    into emp_demo values (566, 'Patel', 6, date '2015-03-03', 16)
select * from dual;

【讨论】:

    【解决方案2】:

    假设您可以使用列 [employee_id, manager_id, duration] 迭代列表(称为员工):

    desired_dict = {}
    for employee_id, manager_id, duration in employees:
         desired_dict[employee_id] = {"manager_id" : manager_id, "duration" : duration }
    
    #or even better
    desired_dict = {employee_id : {"manager_id" : manager_id, "duration" : duration } 
    for employee_id, manager_id, duration in employees}
    
    #now, you can access an  employee like:
    some_manager = desired_dict[334]['manager_id']
    some_duration = desired_dict[334]['duration']
    

    但您可能想使用pandas 之类的东西 并执行以下操作:

    import pandas as pd
    employees_data = pd.DataFrame(employees, columns=['employee_id', 'manager_id', 'duration'])
    employees_data.index = employees_data['employee_id']
    desired_dict = employees_data.to_dict()
    

    【讨论】:

    • 我认为存在误解。想在oracle plsql中实现python dict结构
    猜你喜欢
    • 2016-05-21
    • 2011-11-13
    • 2020-10-23
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    相关资源
    最近更新 更多