1. Home
  2. Computing & Technology
  3. Python

Inserting Data Into a MySQL Database

From Al Lukaszewski, for About.com

4 of 8

Taking Data From the Command Line

For this tutorial, 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[1]
input = sys.argv[2]
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'.
Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Database Programming
  5. Python and MySQL - Inserting Python Data Into a MySQL Database - Taking Data From the Python Command Line

©2009 About.com, a part of The New York Times Company.

All rights reserved.