How can I execute raw SQL after connect to database?
I need to run script once, after connect to DB.
Thanks.
UPD: question is not how to run raw SQL.
Just visit the docs here.
It was I think the second match on google with "Django ORM".
EDIT See comments
If you look at this Django page you see that MySQLdb (the underlying layer) also accepts an init_command option which is run immediately after a connection is established. That's a feature of MySQLdb, and not so much of Django itself.
If you are using django >= 1.2:
from django.db import connection, transaction
query = "SELECT foo FROM bar;"
connection.cursor().execute(query)
transaction.commit_unless_managed()
Related
so i created a postgresql database and i want to link it to my python code using sqlalchemy but since i made the database in pgadmin i dont know what to mention as the url of the database in the code.
thank you in advance.
If you created your database using the defaults, you can probably login to the server using psql (SQL Shell).
Try to connect to the database you created and if you are able to connect to the database try \conninfo.
If it's on your own machine the output will be quite standard host will be localhost, ip will be 127.0.0.1 and port will be the default port of 5432.
Once you make sure of these things you should try to connect to the database using the following code from this answer to a different question.
Please make sure you have both SQLAlchemy and psycopg2 installed before you try to connect. Then try this:
from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg2://user:password#hostname/database_name')
Or may be find a good tutorial on SQLAlchemy.
Go back into pgAdmin, click on the node for your server and then check the values under "Connection" in the "Properties" page:
According to the SQLAlchemy documentation the corresponding connection URL for those values would be
connection_url = 'postgresql+psycopg2://user:password#localhost:5432/db_name'
engine = create_engine(connection_url)
With the upper picture setting. I use following pattern
postgresql://postgres:[MY_PASSWORD]#localhost:5432/[DATABASE_NAME]
postgresql://[YOUR_USERNAME]:[YOUR_PASSWORD]#[YOUR_HOST_NAME]:[YOUR_PORT]/[DATABASE_NAME]
On Orator documentation, I can only find a way to change connection. I've already checked the Orator Connection Model, but database setter is nowhere to be found.
Is there a way to change the database on Orator ORM without creating multiple connections? Thank you.
Github Issue Link: https://github.com/sdispater/orator/issues/326
This was answered in the github issue by josephmancuso:
No. You'll need to switch between the connections.
A connection is just a group of database settings.
You can technically do so if you query manually, but this bypasses the whole benefit of using an ORM:
db.select('select * from db2.table') #runs a raw query
I am using sqlalchemy to connect to MySQL database and found a strange behavior.
If I query
LOAD DATA LOCAL INFILE
'C:\\\\Temp\\\\JaydenW\\\\iata_processing\\\\icer\\\\rename\\\\ICER_2017-10-
12T09033
7Z023870.csv
It pops an error:
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1148, u'The used
command is not allowed with this MySQL versi
on') [SQL: u"LOAD DATA LOCAL INFILE
'C:\\\\Temp\\\\JaydenW\\\\iata_processing\\\\icer\\\\rename\\\\ICER_2017-10-
12T090337Z023870.csv' INTO TABLE genie_etl.iata_icer_etl LINES TERMINATED BY
'\\n'
IGNORE 1 Lines (rtxt);"] (Background on this error at:
http://sqlalche.me/e/2j85)
And I find the reason is that:
I need to set the parameter as
args = "mysql+pymysql://"+username+":"+password+"#"+hostname+"/"+database+"?
local_infile=1"
If I use MySQL official connection library. I do not need to do so.
myConnection = MySQLdb.connect(host=hostname, user=username, passwd=password, db=database)
Can anyone help me to understand the difference between the two mechanisms?
The reason is that the mechanisms use different drivers.
In SQLAlchemy you appear to be using the pymysql engine, which uses the PyMySQL Connection class to create the DB connection. That one requires the user to explicitly pass the local_infile parameter if they want to use the LOAD DATA LOCAL command.
The other example uses MySQLdb, which is basically a wrapper around the MySQL C API (and to my knowledge not the official connection library; that would be MySQL Connector Python, which is also available on SQLAlchemy as mysqlconnector). This one apparently creates the connection in a way that the LOAD DATA LOCAL is enabled by default.
I'm trying to automatically create a test database before running my tests without shelling out, but I'm struggling to connect to the postgres server without connecting to a database.
If I give create_engine() a URL with .database None it tries to connect to a default database name.
Is what I'm trying to do possible?
I was going a long the lines of this answer: https://stackoverflow.com/a/28784334/311220
One way to achieve this is to connect to the default postgres database which should always be present. Then once connected you can create the desired databases.
I need full example that show me how I can Connect to Cassandra by CQL. I am working on a project that should be written in (python) Django and I want to do some thing directly not only using cqlEngine as my Project Driver.
Thanks for your help.
This is how I made python communicate to cassandra.
First, you need to establish a connection, adding:
import cql
con= cql.connect(host="127.0.0.1",port=9160,keyspace="testKS")
Then generate a cursor for query:
cur=con.cursor()
result=cur.execute("select * from TestCF")
And I can see the result by:
result.fetchone()
or
result.fetch()
This queries nicely and doesn't need extra library, such pycasaa or cqlengine