Run MySQL Server in Python - python

So I've been looking all over the internet and only found resources/tutorials on how to connect to a MySQL server but my question is, how do you host a MySQL server both on Windows & Linux?

I am not quite sure what you are asking but if the question is how to run a database for python independent of the OS, consider using sqlite.
From the link (emphasis mine)
SQLite is an embedded SQL database engine. Unlike most other SQL
databases, SQLite does not have a separate server process. SQLite
reads and writes directly to ordinary disk files. A complete SQL
database with multiple tables, indices, triggers, and views, is
contained in a single disk file. The database file format is
cross-platform - you can freely copy a database between 32-bit and
64-bit systems or between big-endian and little-endian architectures.
These features make SQLite a popular choice as an Application File
Format. Think of SQLite not as a replacement for Oracle but as a
replacement for fopen().
So it allows you to use a database from your python code without the hassle of running a server or setting something up locally.
Note that sqlite can also be stored in-memory if you want to avoid writing to disk.

Unless you have a very specific reason to start the server from Python, I.e. you want to be able to programmatically do stuff you wouldn't do from the command line, I think the best you could do is to install an instance of Mysql server in your local machine, run it and then, you'll be able to connect to it from Python.
Bear in mind that your local installation of Mysql will be running on localhost (127.0.0.1)

Related

Database embedded with Oracle on Cx_Oracle from Python

Hello I am creating a small program on Python with the cx_oracle module which allows me to connect to my Oracle database on my Computer. However I would like to send it to a friend and therefore I would like him to be able to handle the same database as me. So I thought of an embeddable database (a bit like a file on SQLite) but with Oracle I did not find such a possibility. I would like to know if there is a way to do it with Oracle or if I am forced to connect to a local database.
First of all, you can export and import Oracle databases, which would help you a lot at the initial sharing. However, if you share your database with a friend and both of you are working on the same database but with different copies, then the two databases will eventually diverge from each other, with ever greater impact. So, you will need to consider your options carefully:
Using a central server
You could use a central server (which could be a remote server or even your computer if you apply port forwarding), ensure that both you and your friend are connected to that database and then both your and his changes will automatically be applied to the same database, without copies.
Versioned dumps
You could use a versioning tool, like git to store the versions of your database dump/structure/data and both you and your friend could use this, maybe storing the versions in a central repositories, so you would not need to send and communicate your database changes again and again. This would ensure schema and data synchronization, albeit you will have frequent merge conflicts and other merge-related problems.
Versioned scripts
You and your friend could write versioned scripts. This would apply on structural changes, so your and your friend's test data would diverge, but the structure would not.
Migration scripts
Some ORMs have automatically generated migration scripts and one can go forwards or backwards some levels. I do not particularly like the idea of automatically generated change scripts, but it is certainly a possible solution.

Remotely accessing sqlite3 in Django using a python script

I have a Django application that runs on apache server and uses Sqlite3 db. I want to access this database remotely using a python script that first ssh to the machine and then access the database.
After a lot of search I understand that we cannot access sqlite db remotely. I don't want to download the db folder using ftp and perform the function, instead I want to access it remotely.
What could be the other possible ways to do this? I don't want to change the database, but am looking for alternate ways to achieve the connection.
Leaving aside the question of whether it is sensible to run a production Django installation against sqlite (it really isn't), you seem to have forgotten that, well, you are actually running Django. That means that Django can be the main interface to your data; and therefore you should write code in Django that enables this.
Luckily, there exists the Django REST Framework that allows you to simply expose your data via HTTP interfaces like GET and POST. That would be a much better solution than accessing it via ssh.
Sqlite needs to access the provided file. So this is more of a filesystem question rather than a python one. You have to find a way for sqlite and python to access the remote directory, be it sftp, sshfs, ftp or whatever. It entirely depends on your remote and local OS. Preferably mount the remote subdirectory on your local filesystem.
You would not need to make a copy of it although if the file is large you might want to consider that option too.

Python consumer application with database

I want to implement a simple application (for Windows), mainly for data collection and I should be able to run some queries.
I can produce an exe file from the python code, but I don't want to install the database separately, rather I'd include it in an installer. I think some other applications do the same or they require MySQL to be installed -- not in development mode -- since I saw in Windows' Programs and Features that there are some MySQL installations.
So my question is: Is there a way to use MySQL or a specific preparation method of an application so I am able to use some database operations inclusive saving data? Remark: any database would work, I was referring to MySQL because I suspect I saw some examples.
You could use Pyinstaller to make an exe out of your database and Python file. Python natively supports sqlite so you don't have to install anything extra if you use that.

Understanding SQLite conceptually

I looked at the sqlite.org docs, but I am new to this, so bear with me. (I have a tiny bit of experience with MySQL, and I think using it would be an overkill for what I am trying to do with my application.)
From what I understand I can initially create an SQLite db file locally on my MAC and add entrees to it using a Firefox extension. I could then store any number of tables and images (as binary). Once my site that uses this db is live, I could upload the db file to any web hosting service to any directory. In my site I could have a form that collects data and sends a request to write that data to the db file. Then, I could have an iOS app that connects to the db and reads the data. Did I get this right?
Would I be able to run a Python script that writes to SQLite? What questions should I ask a potential hosting service? (I want to leave MediaTemple, so I am looking around...)
I don't want to be limited to a Windows server, I am assuming SQLite would run on Unix? Or, does it depend on a hosting service? Thanks!
I could upload the db file to any web hosting service to any directory
Supposing that the service has the libraries installed to handle sqlite, and that sqlite is installed.
Would I be able to run a Python script that writes to SQLite
Yes, well, maybe. As of Python 2.5, Python includes sqlite support as part of it's standard library.
What questions should I ask a potential hosting service
Usually, in their technical specs they will list what databases/libraries/languages are supported. I have successfully ran Python sites w/ sqlite databases on Dreamhost.
SQLite would run on Unix
Most *nix flavors have pre-packaged sqlite installation binaries. The hosting provider should be able to tell you this as well.

Best way to access Firebird DB from a remote desktop.

I have a Firebird DB set up on my computer and I want to be able to retrieve data from a different computer. What is the best way to go about this?
I am running windows and using python.
Install firebird client to the client pc
To connect firebird programmaticaly from python you should install a python Firebird driver.
For Python 2.x, you can use kinterbasdb. This is the legacy driver and I think it is not actively developed but only maintained.
To connect windows based server database from kinterbasdb you can use
Import kinterbasdb as k
k.init(type_conv = 300) #
con = k.connect(dsn='127.0.0.1:c:\\db\\test.fdb', user='sysdba', password='masterkey', charset='YOUR_CHARSET', dialect=3)
Of course you should adjust connection parameters according to your system. Kinterbasdb documentation is here
If you want use an ORM, you can use SqlAlchemy which uses kinterbasdb for Firebird Support
For Python 3k you can use pyfirebirdsql which also supports Python 2.5+ and under active development, but not supported by SqlAlchemy yet.
Run Firebird server on the computer with database file and connect to it from remote computer. You will need in Firebird client library installed on remote computer.
I think we need a bit more info.
Do you want database access - as in "I want to be able to edit table layout and define new tables, views, procedures and so on" ?
Or do you only need to get data from the database using python ?
The latter could be achieved by installing a Firebird client (in essence its a dll (fbclient.dll)) and then use a connect string from python to connect to your database.

Categories