Python Tutorial

Monday, June 20, 2011

Mysql in python: Insert

From the code below it's very clear that you need to create a database first named py_sample_db
Then you need to create a table named sample_py_table with two colums (name,email), all contents are varchar type.
You can also run this code on your own database by changing current settings(host,user,passwd,db)


 '''
  This code use python MySQLdb syntax
 '''

 import MySQLdb
 conn = MySQLdb.connect (host = "localhost",user = "root",passwd = "",db =
"py_sample_db") cursor = conn.cursor () sql="INSERT into sample_py_table (name,email) VALUES ('jony','myemail@gmail.com1')"; cursor.execute (sql) cursor.close () conn.close () print "Insert success"

0 comments:

Post a Comment