简体   繁体   English

如何在python中为oracle连接传递变量值

[英]How to pass variable values for oracle connection in python

Im able to connect to Oracle Db successfully when i hardcode the Db details like " connection = cx_Oracle.connect("uname/pass@192.168.xxx.yyy:port/db")" but how to pass the variable values to connect to the db? 我能够成功连接到Oracle Db,当我硬编码Db细节,如“connection = cx_Oracle.connect(”uname/pass@192.168.xxx.yyy:port / db“)”但如何传递变量值以连接到D b?

I tried some like this. 我试过这样的。

connection = cx_Oracle.connect("{}/{}@{}:{}/{}".format(uname,password,IP,port,db))

which is not working as expected so please share some thought on this to make it work. 哪个没有按预期工作所以请分享一些想法,以使其工作。

def knowcobrand(request):
    value_type = request.POST.get('CobranSelection')
    cobrand_value = request.POST.get('cobrand')
    env = request.POST.get('NewEnvName')
    print(value_type)
    print(cobrand_value)
    print(env)
    # feed = Environments.objects.get(Q(Env_name__icontains=env))
    # print(feed.user_name)
    connection = cx_Oracle.connect("app/app@192.168.xxx.yy:port/db")

I want to use the variable's value of value_type and env for Db connection 我想使用value_type的变量值和dv连接的env

EDIT: You should probably run through the Django tutorial , it will explain the basics of Django and using the ORM 编辑:你应该通过Django教程 ,它将解释Django的基础知识和使用ORM

You should configure your database connection in your settings 您应该在设置中配置数据库连接

There is a specific example for oracle oracle有一个具体的例子

You can now use the Django ORM ( after running migrations ) 您现在可以使用Django ORM( 运行迁移后

If you want a raw cursor for the database you can still use Django for this like so 如果你想要数据库的原始游标,你仍然可以像这样使用Django

from django.db import connection
with connection.cursor() as c:
    c.execute(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM