1. Computing & Technology

Inserting Data Into a MySQL Database

From , former About.com Guide

The main() Thing

Next, 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[1] and sys.argv[2], respectively.

  1. About.com
  2. Computing & Technology
  3. Python
  4. Database Programming
  5. Python and MySQL - Inserting Python Data Into a MySQL Database - The main() Thing of the Python Program

©2012 About.com. All rights reserved.

A part of The New York Times Company.