Django高级数据库查询

时间:2014-07-10 22:19:54

标签: django oracle django-models django-views

我正在针对oracle运行一个非常复杂的查询来返回一些数据。我正在手动运行它,因为django本身可以从我所读过的内容中做我正在做的事情。 我运行查询但django没有得到任何值作为回报。我最近从我的开发数据库切换到我的生产运行查询,因为它有实时数据和所需 数据还没有存在于开发中,因为它是一个新功能。

查询执行但不返回任何内容。但是,我正在运行的另一个非常类似的查询是返回数据。如果我输出确切的声明 django用于针对未返回的数据对oracle进行查询,然后如果我转到终端并手动执行对sql server的查询,它将返回数据。 我还将fetchall输出到页面作为debuging工具,工作查询具有所有信息,但是无工作的输出空白列表。

起初我认为这是查询,但我已经证明了查询django正在使用作品但django没有数据。唯一合乎逻辑的答案是django试图使用dev数据库或者模型表中的某些东西是关闭的。 但是我运行了syncdb和sql并且没有出现任何错误...如果我对dev运行查询它不会返回数据,但是会反对生产。

这是我正在运行的查询。

if 'customer_licenses_all' in request.POST:
 return_query_type = 'customer_licenses_all'
 cursor.execute("alter session set time_zone='UTC'")
 selected_customer_id = int(selected_customer['selected_customer'])       
 rundate = selected_customer['compute_date']
 date2 = datetime.strptime(rundate, "%d-%b-%Y")
 date_list = ()
 date_list = (date2.day, date2.month, date2.year)
 last_date =  get_last_day(date_list)
 last_date = last_date.strftime("%d-%b-%Y")

 sql = (('''select customer_name, count(*) license_count, sum(cpu_ghz_hours) CPU_Core_Hours, sum(ram_gb_hours) RAM_GB_Hours, licensing_authority, product 
            from customers a, vm_groups b, vms c, vm_license_histories d, licenses e, vm_compute_usage_histories f, license_authorities g 
            where a.customer_id = b.customer_id 
            and b.vm_group_id = c.vm_group_id 
            and c.vm_id = d.vm_id 
            and d.license_id = e.license_id 
            and f.vm_id = c.vm_id 
            and e.license_authority_id = g.license_authority_id 
            and trunc(f.datetime) = to_date(%s,'DD-MON-YYYY') 
            and inactive = 'N' 
            and (deassignment_date is null or trunc(deassignment_date) between to_date(%s,'DD-MON-YYYY') and last_day(to_date(%s,'DD-MON-YYYY'))) 
            and cpu_ghz_hours > 0 
            and g.license_authority_id not in (28,27,31) 
            group by customer_name, licensing_authority, product order by 1,5,6''')% ("'"+rundate+"'","'"+ rundate+"'" ,"'"+last_date+"'"))
 cursor.execute(sql)
 return_query = cursor.fetchall()    
 context = Context({'customers': customers,
                       'return_query': return_query,
                       'return_query_type':return_query_type, 
                       'rundate':rundate,
                       'last_date':last_date, 
                       'sql':sql, 
                       })

django输出

  

选择customer_name,count(*)license_count,sum(cpu_ghz_hours)   CPU_Core_Hours,sum(ram_gb_hours)RAM_GB_Hours,licensing_authority,   来自客户a的产品,vm_groups b,vms c,vm_license_histories d,   许可证e,vm_compute_usage_histories f,license_authorities g where   a.customer_id = b.customer_id和b.vm_group_id = c.vm_group_id和   c.vm_id = d.vm_id和d.license_id = e.license_id和f.vm_id =   c.vm_id和e.license_authority_id = g.license_authority_id和   trunc(f.datetime)= to_date(' 01-Jun-2014',' DD-MON-YYYY')并且不活动   =' N'和(deassignment_date在to_date之间为null或trunc(deassignment_date)(' 01-Jun-2014',' DD-MON-YYYY')和   last_day(to_date(' 2014年6月30日',' DD-MON-YYYY')))和cpu_ghz_hours> 0   和g.license_authority_id不在(28,27,31)group by customer_name中,   licensing_authority,产品订单1,5,6

     

[]

0 个答案:

没有答案