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?
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
EDIT: You should probably run through the Django tutorial, it will explain the basics of Django and using the ORM
You should configure your database connection in your settings
There is a specific example for oracle
You can now use the Django ORM (after running migrations)
If you want a raw cursor for the database you can still use Django for this like so
from django.db import connection
with connection.cursor() as c:
c.execute(...)
Related
I need to connect to the ms-sql database and then create a new database there using python script.
I have the user credentials for the login. So how to create the connection to the ms-sql server using python.
If you do not have database name then use the connection string as mentioned in the code below. Create a database after connection and use the database finally.
import pyodbc
# if you have user id and password then try with this connection string
connection_string = f"DRIVER={SQL Server};SERVER={server_name};UID={user_id};PWD={password}"
# if using in the local system then use the following connection string
connection_string = f"DRIVER={SQL Server};SERVER={server_name}; Trusted_Connection=True;"
connection= pyodbc.connect(connection_string)
cursor = connection.cursor()
sql_create_database = f"CREATE DATABASE {database_name}"
cursor.execute(sql_create_database)
set_database = f"USE {database_name}"
cursor.execute(set_database)
I tried the exact same code as found here...
https://github.com/TomMalkin/SimQLe
I am not sure how to connect to mysql database.
from simqle import ConnectionManager
cm = ConnectionManager("connections.yaml")
sql = "SELECT name, age FROM people WHERE category = :category"
params = {"category": 5}
result = cm.recordset(con_name="my-database", sql=sql, params=params)
Getting an error:
UnknownConnectionError: Unknown connection my-database
This is how I can connect to mysql database from command prompt.
mysql -h 172.31.84.39 -udba -pXXXX -P 3392
How do I write the connection string?
I usually use sqlalchemy to connect mysql database. I have readed the document of SimQLe which you are using. In SimQLe document,
cm = ConnectionManager("connections.yaml")
is the way to connect to database and you should put your login param in this yaml file called connections.yaml.
Here is the offical document simple:
https://github.com/TomMalkin/SimQLe#the-connectionsyaml-file
connections:
# The name of the connection - this is what will be used in your project
# to reference this connection.
- name: my-sql-server-database
driver: mssql+pyodbc:///?odbc_connect=
connection: DRIVER={SQL Server};UID=<username>;PWD=<password>;SERVER=<my-server>
# some odbc connections require urls to be escaped, this is managed by
# setting url_escaped = true:
url_escape: true
# File based databases like sqlite are slightly different - the driver
# is very simple.
- name: my-sqlite-database
driver: sqlite:///
# put a leading '/' before the connection for an absolute path, or omit
# if it's relative to the project path
connection: databases/my-database.db
# This connection will be used if no name is given if the default
# parameter is used:
default: true
Maybe you should change some params in here:
driver: mssql+pyodbc:///?odbc_connect=
connection: DRIVER={SQL Server};UID=<username>;PWD=<password>;SERVER=<my-server>
And from the document, it says that SimQle is built on SQLAlchemy,
SimQLe is built on top of the fantastic SQLAlchemy library.
maybe you can use the SQLAlchemy's login params to connect the database in SimQLe. Such like this:
mysql+pymysql://<username>:<password>#<host>/<dbname>[?<options>]
Changed to:
driver: mysql+pymysql://
connection: <username>:<password>#<host>/<dbname>[?<options>]
Offical documents:
https://simqle.readthedocs.io/en/latest/
https://docs.sqlalchemy.org/en/14/dialects/mysql.html#module-sqlalchemy.dialects.mysql.pymysql
https://docs.sqlalchemy.org/en/14/dialects/mssql.html#module-sqlalchemy.dialects.mssql.pyodbc
Usually I connect to a database in Pymongo like this:
# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")
# connect to the database
self.db = self.connection.my_database
The problem is, the name of my_database always changes. How can I use a variable to choose the database?
Based on this documentation we can do it like this:
# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")
# connect to the database
a = 'my_database'
self.db = self.connection[a]
I am making a database manager in Python and I want the user to be able to select a database.
I know that one can connect to a database using the following code
connection = pymysql.connect(
host='localhost'
user='root'
passwd=''
db='my_database'
)
But what if the user wants to connect to a different database later on? How would I tell connection to connect to a different database? Or better yet, omit db and then add it later on.
Calling pymysql.connect actually creates the connection to the database. If you want to connect to a different database you should create a new connection object, rather than trying to reuse the same one. You could assign it to the same variable name if you wanted to 'reuse' it later in your code.
Like this:
connection1 = pymysql.connect(
host='localhost'
user='root'
passwd=''
db='my_database'
)
connection2 = pymysql.connect(
host=?
user=?
passwd=?
db='my_other_database'
)
You can just do:
connection.close()
And then open another connection.
I've created a user for my database in mongodb. I've tested with mongo shell to make sure that the user has proper privileges to access the database.
Now I want to use my Python program to access the database, and I use PyMongo. If I run mongod in unauthorized mode (without option --auth), the Python client works fine. However, when I use --auth option, the Python client doesn't work any more. In fact, it reports unauthorized error, which is easy to understand because I didn't change anything in the code. Here is the code to connect my test database:
from pymongo import MongoClient
client = MongoClient()
db = client.test
cursor = db.restaurants.find()
for document in cursor:
print(document)
My question is how can I change the Python code to use username/password created previously? I've looked at the client documentation but there is not information for it.
client = MongoClient("mongodb://username:password#server/dbname")
This is the normal format for telling the client where and how to connect. The way you are using (with no parameters at all) defaults to a local mongo install, on the default port, with no authentication.
Besides Danielle's answer you can also use authenticate method for that. Your code would look like this:
from pymongo
import MongoClient
client = MongoClient()
db = client.test
db.authenticate('user', 'password', mechanism=<either 'SCRAM-SHA-1' or 'MONGODB-CR', being 'MONGODB-CR' the default authentication mechanism Before MongoDB 3.0>)
cursor = db.restaurants.find()
for document in cursor:
print(document)