Thursday, January 15, 2009
INSERT INTO statement with MySQLdb
One can insert column(s) data into mysql with the following python commands:
import MySQLdb
db=MySQLdb.connect(host="localhost",user = "root",passwd="123",db="mydb")
c=db.cursor()
...............
...............
c.execute("""INSERT INTO StockData (ind,Name,yesterday,pricenow,perch,id) VALUES (%s,"%s","%s","%s","%s","%s")""" %(i, str(comp), str(yesterday),str(pricenow),str(perch),str(id)))
db.commit()
Without using db.commit(), the database will not get updated.
Here ind column is of type integer and rest are strings.
via[Packtpub]
import MySQLdb
db=MySQLdb.connect(host="localhost",user = "root",passwd="123",db="mydb")
c=db.cursor()
...............
...............
c.execute("""INSERT INTO StockData (ind,Name,yesterday,pricenow,perch,id) VALUES (%s,"%s","%s","%s","%s","%s")""" %(i, str(comp), str(yesterday),str(pricenow),str(perch),str(id)))
db.commit()
Without using db.commit(), the database will not get updated.
Here ind column is of type integer and rest are strings.
via[Packtpub]