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.
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]
I am currently working with an online database at a local server (but I don't have physical access to the server). I want to use python (version 3.5) to connect to the database and work with the data.
Since the problem is pretty early in the developing process I have not tried too much yet. In my mind I would like it to work somehow like this:
import sqlite3 as sql
db = sql.connect('localhost:port', 'username', 'password', 'database.db')
I expect it to return the database connection object I could then work with.
You'll need public IP of the VM/server where the database is hosted.
db = sql.connect('<PUBLIC_IP:PORT>', '<USER_NAME>', '<PASSWORD>', '<DATABASE>')
will get your requirement done. If it does not work, and you get connection error,
make sure that the port is correct.
I have an online database and connect to it by using MySQLdb.
db = MySQLdb.connect(......)
cur = db.cursor()
cur.execute("SELECT * FROM YOUR_TABLE_NAME")
data = cur.fetchall()
Now, I want to write the whole database to my localhost (overwrite). Is there any way to do this?
Thanks
If I'm reading you correctly, you have two database servers, A and B (where A is a remote server and B is running on your local machine) and you want to copy a database from server A to server B?
In all honesty, if this is a one-off, consider using the mysqldump command-line tool, either directly or calling it from python.
If not, the last answer on http://bytes.com/topic/python/answers/24635-dump-table-data-mysqldb details the SQL needed to define a procedure to output tables and data, though this may well miss subtleties mysqldump does not
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
I have an sqlalchemy application that currently uses a local database.The code for the application is given below.
log = core.getLogger()
engine = create_engine('sqlite:///nwtopology.db', echo=False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
class SourcetoPort(Base):
""""""
__tablename__ = 'source_to_port'
id = Column(Integer, primary_key=True)
port_no = Column(Integer)
src_address = Column(String,index=True)
#-----------------------------------------
def __init__(self, src_address,port_no):
""""""
self.src_address = src_address
self.port_no = port_no
I want to create the database itself in a remote machine.I came across this document.
http://www.sqlalchemy.org/doc_pdfs/sqlalchemy_0_6_3.pdf
In the explanation they mentioned the lines given below.
engine = create_engine(’postgresql://scott:tiger#localhost:5432/mydatabase’)
My first question is
1) does sqlite support remote database creation?
2) How do I keep the connection to the remote machine open always? I don't want to initiate an ssh connection every time I have to insert an entry or make a query.
These question may sound stupid but I am very new to python and sqlalchemy.Any help is appreciated.
Answering your questions:
SQLite doesn't support remote database connection - you'll have to implement this by yourself - like putting sqlite database file on shared by network filesystem, but it would make your solution less reliable
My suggestion - do not try to use user remote sqlite database but switch to traditional RDBMS. Please see below for more details.
Sounds like your application had overgrown SQLite. And it is good time to switch to using traditional RDBMS like MySQL or PosgreSQL where network connections are supporting out of the box.
SQLite is local database. SQLite has a page explaining when to use it. It says:
If you have many client programs accessing a common database over a
network, you should consider using a client/server database engine
instead of SQLite.
The good thing is that your application might be database agnostic as you are using SQLAlchemy for generating queries.
So I would do the following:
install database system to machine (it doesn't matter - local or
remote, you can always repeat move your database to remote machine) and configure permissions for your user (create database, alter, select, update and insert)
create database schema and populate data - to clone your existing. There are some tools available for doing so - i.e. Copying Databases across Platforms with SQLAlchemy
sqlite database.
update db connection string in your application from using sqlite to use remote database of your choice
Times have changed.
If one wishes to make a SQLite database available over the web, one option would be to use CubeSQL as a server, and SQLiteManager for SQLite as its client. For details, see e.g. https://www.sqlabs.com/
Another option might be to use Valentina Server similarly: see https://www.valentina-db.com/en/valentina-server-overview
(These options will probably only be suitable if there is at most one client with write-access at a time.)
Are there any others?