We are using both! MySQL is the DBMS. It consists of a
server (mysqld
) and a client (the mysql
program we used in the first few assignments). The mysql
client talks to the mysqld
server over a network
connection.
The PyMySQL
module allows our Python programs to talk
to the mysqld
server, sending queries and getting
results.
Yes, that's a pretty good working definition.
Any time two pieces of software have to talk to one another, we have an API: a collection of functions, classes, objects and methods that they can use.
Here, the API is what allows our Python program to talk to
the mysqld
server.
The dbi.conf()
is a function call in your code. If you
want your program to connect to a different database (e.g. WMDB versus
WW123_db), you would edit that line.
If your program needs to connect to one, then another, you could
call dbi.use(new_database)
, since you don't need to
re-read the dbi.conf
file and such.
Sure. Your python program reads your username/password from your
credentials file (~/.my.cnf
) and sends them to
the mysqld
server to set up your connection.
This is plain old password authentication. There are other methods, like SSH keys, but this will do.
Actually, the sysadmin (me) already did that for you. It's there in
your account. I created it at the beginning of the semester. It's
ready by the mysql
client program.
Oh, there's no practical limit on the number of columns! If you want to read 10 or more columns from a table, you are welcome to do so:
The connection object is created when we first connect and authenticate (like starting the mysql
program).
The cursor object represents a particular query.
Yes, each cursor corresponds to a query. You can re-use a cursor if you want, but it's just a small data structure, like an object in a Java program, so you're not saving a lot of space by re-using one. But it doesn't hurt, either.
The cursor needs to know where to send the SQL code, and it gets that information from the connection object.
Then, it has some instance variables to store the result and such.
Sure. It's just an issue of whether you want to get the results (the rows) as tuples or as dictionaries. My rule of thumb:
I really try to avoid code like the following: