hi everyone,
i have wrote the database code like this
try:
import MySQLdb
import _mysql_exceptions as DB_EXC

cxn = MySQLdb.connect(host='10.0.2.2')

if(cxn != 0):
tkMessageBox.showinfo("Text", "you can update ur local database to server")

else:
tkMessageBox.showinfo("Text", "there is no connection please try again")


cxn.close()

except ImportError , e:
return None

when there is connectivity then the control is going to the IF block. but when the connectivity loss the control is not going to the else block. is there any wrong in my syntax.
pls give a quick and valuable reply.

Recommended Answers

All 2 Replies

I think if(cxn != 0): should be if(cxn != None): or if(cxn is not None):

rajasekhar1242 try this.

import MySQLdb
import _mysql_exceptions as DB_EXC

try:
    cxn = MySQLdb.connect(host='10.0.2.2')

    if(cxn != 0):
        tkMessageBox.showinfo("Text", "you can update ur local database to server")

    else:
        tkMessageBox.showinfo("Text", "there is no connection please try again")

    cxn.close()

except ImportError , e:
    return None

What I found was imports after the try: and indents were wrong.
I used pyscripter | Syntax check too help find bugs. Also pylint helps also. pyscripter == mmm-experts.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.