Reputation: 295
My python program is a lab chemical program, it will be installed on many different computers in several different labs. All of these programs will need to be able to store information to the same online database and retrieve information from this same online database. What is the SIMPLEST way for me to achieve this?
I have considered using MySQLdb but the problem is:
What are some simple options here?
Upvotes: 1
Views: 3169
Reputation: 1319
First of all you need a database host that lets you connect remotely, so, before you contract a host, check with them if you can access the database remotely.
If your host does it, you just need to get the information with them to connect to the database server (usually, the host address, a port, a database name, an user and a password).
Then, create a python file (ex. database.py) to store these information.
Finally, in your program, import these information to create a connection.
Typically you would have:
"""
database.py: provides information to make a database connection
"""
login_info = {
"host":"host_adress",
"user":"user_name",
"password":"very_secret_password",
"database":"database_name",
"port": 3306
}
Of course, change the dict values to that you got with your host.
In your program:
"""
main_program.py: a chemical program
"""
from database import login_info
import mysql.connector
conn = mysql.connector.Connect(**login_info)
curs = conn.cursor()
# STORE
curs.execute("""INSERT INTO table_name(row1,row2) VALUES("foo","bar")""")
conn.commit()
# RETRIVE
curs.execute("""SELECT * FROM table_name""")
results = curs.fetchall()
for result in results:
print(result)
Note that I'm using the MySQL Connector/Python driver (and not the MySQLdb driver) that you can get here. The MySQL connector has a satisfactory documentation with code examples that may help you.
Upvotes: 2