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
Related
I am trying to send the outputs I got in python to the database. Like I am reading valves from my plc using the OPC UA server via python. I can read my variables every second, Now I want to send those values to my PostgreSQL database. Any help would be appreciated. Thank you very much again.
I created a connection between python and PostgreSQL by using the psycopg module. And I am able to create the tables using my python script.
Rather than having to script directly with psycopg, I usually reccomend using SqlAlchemy, which is a nice wrapper for psycopg that makes editing data in the database much easier
Eg, by using the same psycopg engine you have already set up, you can do:
from sqlalchemy.orm import Session
with Session(engine) as session:
spongebob = User(
name="spongebob",
fullname="Spongebob Squarepants",
addresses=[Address(email_address="spongebob#sqlalchemy.org")],
)
sandy = User(
name="sandy",
fullname="Sandy Cheeks",
addresses=[
Address(email_address="sandy#sqlalchemy.org"),
Address(email_address="sandy#squirrelpower.org"),
],
)
patrick = User(name="patrick", fullname="Patrick Star")
session.add_all([spongebob, sandy, patrick])
session.commit()
I created a connection between python and postgreSQL database by using
psycopg2.connect(dbname="dbname", port= 6543, user="postgres", password="password", host="host.docker.internal")
I give host details for creating docker image and use it.
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]
I've an application developed with peewee to use mariadb, but now, I need duplicate the 'insert' to register it in a mssql (Microsft SQL Server) database.
I'm trying to use peewee-mssql and peewee-mssqlserv database drivers (or frameworks, or libraries, i'm confused about how they are called) but it seems that the select and insert functions differ from those defined in original peewee and I don't find documentation or API about this peewee-mssql drivers.
How can I use the same classes to insert records in mariadb and mssql indistinctly? It's possible?
Thanks once again.
I want to load a database from a remote server to my memory/cache, so that I don't have to make network calls every time I want to use the database.
I am doing this in Python and the database is cassandra. How should I do it? I have heard about memcached and beaker. Which library is best for this purpose?
If you are trying to get some data from a database, use the pyodbc module. This module can be used to download data from a given table in a database. Answers can also be found in here.
An example how to connect:
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQLServer};SERVER=SQLSRV01;
DATABASE=DATABASE;UID=USER;PWD=PASSWORD')
cursor = cnxn.cursor()
cursor.execute("SQL_QUERY")
for row in cursor.fetchall():
print row
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()