We will be taking the data from the command line. To do this, we will use the sys module, so add to the same line ", sys". The import line should now read as follows:
import MySQLdb, sys
If you are doing this in the Python shell, simply type:
>>> import sys
Python will not be bothered by the late addition.
Then define two variables, id and input, according to the sys.argv arguments:
id = sys.argv input = sys.argv
This tells Python that the first argument after the program's name should be assigned to the variable 'id'. Likewise, the second argument gets assigned to 'input'.
It is best to define a class in order to access a database. Here I again define a class called Table which allows me to insert data (additem). Unlike the example code for reading from the database, we here do not need to know the length of the database to read through all of its records.
The following code presupposes a table with two columns in it: the first field is the record identifier and the second is the record content.
class Table: def __init__(self, db, name): self.db = db self.name = name self.dbc = self.db.cursor() def additem(self, item): sql = "INSERT INTO " + self.name + " VALUES(" + id + ", " + item + ")" self.dbc.execute(sql) return
Rather than define the statement within the argument of self.dbc.execute, I here define it and then call it as an argument. If you have trouble with your script and it is giving unexpected output, try to define the variables separately from the arguments and then print them out. This will give you insight into the flow of the program and enable you to see where things are going awry.
In the definition of the main() function, we need to define the database and create an instance of Table.
def main(): db = MySQLdb.connect( db="testable") table = Table(db, "test") table.additem()
Here the database name is 'testable'. The table from 'testable' which we will query is 'test'. It may be stating the obvious, but before executing the program, you must ensure that you actually have access to a database and table by these names (or whatever names you insert into the program). As usual, one can tweak these lines so that the values are derived from the command line by using sys.argv and sys.argv, respectively.
We need to call the main function. In keeping the main activity of the program within a function, the program remains compartmentalized and maintainable. So type the following to finish the program:
if __name__ == '__main__': main()
With this class and function, one can easily add to MySQL tables on the fly. As it stands, the program expects input like this when it is executed:
>python insertmysql.py <id> <data> If the program allowed for the database, user, and table to be defined at run-time (i.e., when the program is called), you then have a very flexible data insertion program that you can call from other programs.
While reading and writing is about all most people do with databases, if you would like to read more about MySQLdb, see the MySQLdb User's Guide at SourceForge .