This is my first time trying to set up a database. My intent is to use it with Python to create a website that enables users to login, thus it needs to hold a user’s email address, name, password, etc. I know that I can do this later using rows and columns, but I am not there yet. Right now, I am wondering how I go about creating a new MySQL database. Is this something to do in Terminal, or do I write it in Python and then execute it? Right now all I have is the following in a Python file. Executing it in Terminal returns nothing, which leads me to believe that I have installed MySQL; it’s now just a matter of actually creating my first database.
#!/usr/bin/python
import MYSQLdb
The fact that your script doesn’t return any errors doesn’t mean MySQL is installed, just that the MySQL client for Python (MySQLdb is installed).
You can test if you have mysql running on your machine by trying to connect via command line:
If you do need to install MySQL on your machine, this page:
http://blog.mclaughlinsoftware.com/2011/02/10/mac-os-x-mysql-install/
Contains a step by step installation guide. (Basically download the MySQL package and install it on your Mac, setup a username and password).
Then, you’ll need to login to MySQL using either a command line or a graphical client (google for that, there are several options), and create a new database, this might depend on your client (graphical or not), but google “create new database mysql” and you’ll be good.
Also, you’ll need to create a new user and grant permissions for the previously created database (again, google is your friend).
When all of this is done, you have a database running on your machine, ready to receive data, and you can start using it!
Now to python…
If you have installed the MYSQLdb package already (the fact that it doesn’t cause an error when running seems to indicate you have), then you need to connect to it and execute your queries:
And then do whatever you want with your rows… for more complex examples please consult the documentation on MySQLDB.
I hope this gets you started.