insert the values into the database using python.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cnivas
    New Member
    • Feb 2009
    • 8

    insert the values into the database using python.

    Good Morning All,

    I'm doing a small application using python and MySQL. I'm connecting the database and also creating a table using python application. But, I cannot Insert the values into the table. Can anyone help me to insert the values into the table.
    I use the command to insert the values are as follows:

    "Insert into custreg (First_Name,Mid dle_Name,Last_N ame,Month,Day,Y ear,Gender,Addr ess,City,Pin,St ate,Phone) values ('&fn','&mn','& ln','&month','& day','&year','& gender','&add', '&city','&pin', '&state','&phon e')"
    Advance Thanks for helping me...
    Warm Regards,

    Srinivas
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I have never used MySQLdb, but after looking at some online documentation for a few minutes, I think your code should look like this:
    Code:
    import MySQLdb
    db = MySQLdb.connect(passwd="something",db="whatever")
    c = db.cursor()
    c.execute(
        """INSERT INTO custreg (First_Name,Middle_Name,Last_Name,Month,Day,
        Year,Gender,Address,City,Pin,State,Phone)
        VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
        (fn,mn,ln,month,day,year,gender,add,city,pin,state,phone))
    Note that last tuple must be valid Python identifiers.

    Comment

    Working...