Python Connecting to the MySQL Server, Selecting a Database, and Disconnecting

I n PHP 4, you can use t he PHP init ializat ion file t o specify a default host nam e, usernam e, password, socket pat h, or port num ber by set t ing t he values of t he mysql.default_host , mysql.default_user , mysql.default_password , mysql.default_socket , or mysql.default_port configurat ion direct ives.

2.2.6 Python

To writ e MySQL program s in Pyt hon, you need t he MySQLdb m odule t hat provides MySQL connect ivit y for Pyt hons DB-API int erface. I f you dont have t his m odule, see Appendix A for inst ruct ions. DB- API , like Perls DBI m odule, provides a relat ively dat abase- independent way t o access dat abase servers, and supplant s earlier Pyt hon DBMS- access m odules t hat each had t heir own int erfaces and calling convent ions. This book doesnt cover t he older, obsolet e MySQL Pyt hon int erface. Pyt hon avoids t he use of funct ions t hat ret urn a special value t o indicat e t he occurrence of an error. I n ot her words, you t ypically dont writ e code like t his: if func1 == some_bad_value or func2 == another_bad_value: print An error occurred else: print No error occurred I nst ead, put t he st at em ent s you want t o execut e in a try block. Errors cause except ions t o be raised t hat you can cat ch wit h an except block cont aining t he error handling code: try: func1 func2 except: print An error occurred Except ions t hat occur at t he t op level of a script t hat is, out side of any try block are caught by t he default except ion handler, which print s a st ack t race and exit s. To use t he DB- API int erface, im port t he dat abase driver m odule you want t o use which is MySQLdb for MySQL program s . Then creat e a dat abase connect ion obj ect by calling t he drivers connect m et hod. This obj ect provides access t o ot her DB- API m et hods, such as t he close m et hod t hat severs t he connect ion t o t he dat abase server. Here is a short Pyt hon program , connect .py, t hat illust rat es t hese operat ions: usrbinpython connect.py - connect to the MySQL server import sys import MySQLdb try: conn = MySQLdb.connect db = cookbook, host = localhost, user = cbuser, passwd = cbpass print Connected except: print Cannot connect to server sys.exit 1 conn.close print Disconnected sys.exit 0 The import lines give t he script access t o t he sys m odule needed for t he sys.exit funct ion and t o t he MySQLdb m odule. Then t he script at t em pt s t o est ablish a connect ion t o t he MySQL server by calling connect t o obt ain a connect ion obj ect , conn . Pyt hon script s in t his book convent ionally use conn t o signify connect ion obj ect s. I f t he connect ion cannot be est ablished, an except ion occurs and t he script print s an error m essage. Ot herwise, it closes t he connect ion by using t he close m et hod. Because t he argum ent s t o connect are nam ed, t heir order does not m at t er. I f you om it t he host argum ent from t he connect call, it s default value is localhost . I f you leave out t he db argum ent or pass a db value of t he em pt y st ring , no dat abase is select ed. I f you pass a value of None , however, t he call will fail. To t ry t he script , creat e a file called connect .py cont aining t he code j ust shown. Under Unix, you m ay need t o change t he pat h t o Pyt hon on t he first line of t he script if your Pyt hon int erpret er is locat ed som ewhere ot her t han usr bin pyt hon. Then m ake t he script execut able w it h chm od + x and run it : chmod +x connect.py .connect.py Connected Disconnected Under Windows, run t he script like t his: C:\ python connect.py Connected Disconnected I f you have a filenam e associat ion set up t hat allows .py files t o be execut ed direct ly from t he com m and line, you need not invoke Pyt hon explicit ly: C:\ connect.py Connected Disconnected 2 .2 .6 .1 Addit ion a l con n e ct ion pa r a m e t e r s For connect ions t o localhost , you can provide a unix_socket param et er t o specify t he pat h t o t he Unix dom ain socket : conn = MySQLdb.connect db = cookbook, host = localhost, unix_sock = vartmpmysql.sock, user = cbuser, passwd = cbpass For non- localhost connect ions, you can provide a port param et er t o specify t he port num ber: conn = MySQLdb.connect db = cookbook, host = mysql.snake.net, port = 3307, user = cbuser, passwd = cbpass

2.2.7 Java