Creating a Connection Between MySQL and Tornado - python

I am trying to get a Python and Tornado environment up and running.
As of now I am able to execute Python scripts and now I am trying to be able to make use of databases as well.
By my understanding Tornado has a MySQL wrapper, and I as of now I have XAMPP installed and I would like to continue using PhpMyAdmin as the GUI for MySQL.
The question I am having is how can I create a connection between MySQL and Tornado?
So that when you use a connection command Tornado will connect to the right MySQL installation and databases, which I of course created with PhpMyAdmin?

From Tornado's documentation:
db = database.Connection("localhost", "mydatabase")
Once you instantiate a connection ( named db in this example ), you can reuse it during your server's lifetime..
If you need to change it dynamically while your tornado server is running, then have tornado "listen" to a specific url_pattern, handled by the appropriate web.RequestHandler, which receives as (GET or POST) arguments your MYSQL connection parameters (host, database, user etc..) and creates a new database connection.
Edit
In newer versions of tornado (>=3.0) the tornado.database module has been removed. It is now available as a separate package, torndb.

Related

Do I need to install MySQL Server on client machine, the database is used (Read, Write, Delete) by Python script I wrote?

I have written a python script that uses the MySQL Connector for Python to read, write and update the database. Do I need to install MySQL server on the client machine (Windows) too to run the program or do I just need to make sure that the database is present in the path used by the script? I tried finding guidance on Google and here but couldn't find what I needed.
No you don't need to install MySQL Server on a client machine. By definition client machine means you don't have the DB/Server there. Where is this DB allocated? You show have an IP or a domain/subdomain address where the DB is actually hosted.

Using existing MySQL connection in PyCharm in python project without connecting manually again

I created a connection to a remote, tunneled MySQL server over the PyCharm GUI in the "Database" menu.
I'm trying to use this connection in the code itself, without connecting manually again to the SQL server and tunelling over SSH in the code.
Is this possible? I couldn't find anything relevant on the internet yet.
Thanks!

How to connect oracle database in python irrespective of operating system

I'm trying to write a python script using cx_oracle module for perform oracle database connection. But during the execution, I found it needs oracle instant client to establish a connection. Currently, I'm developing the script in ubuntu but there is a chance to run the same in windows. So I'm confused about the implementation. Could someone please suggest the best way to connect oracle database irrespective the platform
You will always need an OS-specific library or client of some kind. Either the Oracle Instant Client or a Java JDK/JDBC library or both. If you want OS-independence then you would need to interact with the DB through REST calls or something like that instead of making a persistent connection. Otherwise you have to interact with the OS networking stack at some point, which requires OS-specific libraries.

Python connection to server and interact with database

I have question how can I connect to VPS server and interact with MySQL database using python on my local machine.
I would like to make program using PyQT that connect to my server and update, take sth from db.
I know that there is MySQLdb module but from what I know i cannot connect to VPS server, because in connect method it doesn't take server password argument.

Python and Connecting to MySQL over SSH

I am trying to connect to a MySQL database on someone else's "machine". When I use Navicat for MySQL, I have no problem connecting to it. I am trying to do the same with Python so that I do not have to use the GUI interface. I know all my info below is correct (even though I swap fake info) -- can anyone spot where I went wrong? The error I get is OperationalError: (2005, "Unknown MySQL server host 'FTP_hostname' (0)")
My code (using paramiko for the SSH):
import MySQLdb
import paramiko
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('SSH_hostname', 22, username='me', password='pswrd')
time.sleep(1)
db = MySQLdb.connect(host="FTP_hostname",
user="root",
passwd="pswrd2",
db="MyDB")
cur = db.cursor()
Again, I put all this into Navicat and connect no problem. Hoping you can help! Thanks!
MySQL, like most databases, by default runs locally and disallows access from outside networks. As such, you cannot connect to it from an external computer.
Navicat, being a software explicitely for remote administration of databases, likely connects via SSH and tunnels the MySQL connection over it. That way it can act as if the database was installed locally, and for the database it looks as if it was accessed locally.
You could try to do the same by creating a tunnel using Paramiko; see also this question.
If you still in need of connecting to a remote MySQL db via SSH I have used a library named sshtunnel, that wraps ands simplifies the use of paramiko (a dependency of the sshtunnel).
You can check my answer in another similar question with some sample code to use it.
db = MySQLdb.connect(host="FTP_hostname",
Would the host not need to be 127.0.0.1 (localhost) as the tunnel is making the MySQL DB local to the machine that the python script is running on?

Categories