Custom Search

Saturday, September 12, 2009

Python and MySQL database

Python and MySQL database connection

Save file as student.py then in terminal type
#python student.py ----> to run python file 'student.py'
-------------------------
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *

# DB type, host, user, password...
db = QSqlDatabase.addDatabase("QMYSQL"); #'QMYSQL' is the driver name
db.setHostName("periyar")
db.setDatabaseName("saju1db")
db.setUserName("root")
db.setPassword("123456")
ok = db.open()

# True if connected
if ok:
print "Connected to MySQL"
else:
print "ERROR connecting to MySQL"

query = QSqlQuery(db)
if query.exec_("SHOW TABLES"): # it return true or false.
while query.next():
table = query.value(0).toString() # Here 'value' attribute is a tuple.
print table

query1 = QSqlQuery(db)
if query1.exec_("select * from student"):
while query1.next():
i=0
while i<3: i="i+1">

No comments:

Post a Comment