Posts

Showing posts with the label MySQLdb

[Level 2] MySQLdb Python API -- Cursor Objects

In myCxn.py: import MySQLdb cxn = MySQLdb.connect(host='localhost', db='test', user='root', passwd='admin' ) cur = cxn.cursor() In main.py ## import object from file from myCxn import  cur, cxn cxn2 = cur.connection # pass the connection object from cursor # cxn2.close() # close() will cause cur.execute() fail... ## before execute() print "before execute():" print "description:", cur.description print "lastrowid:", cur.lastrowid # lastrowid => The id of last modified row. print "rowcount:", cur.rowcount cur.setoutputsizes(2) # seems not work, Does nothing, required by DB API. cur.execute("select * from test.t;") for data in cur.fetchall():   print("cur1_fetchall.rownumber:", cur.rownumber)   print("cur1_fetchall.rowrowcount:", cur.rowcount)   print(data[0])   cur.execute("select * from test.t;") for data in cur.fetchall():   print("cur2_fet...

[Level 2] MySQLdb Python API -- Connection Objects

Connection have such methods: 1. close() => close database connection. 2. commit() => commit transaction. 3. rollback() => rollback transaction. 4. cursor() => create curosr object and return. 5. begin() => start transaction. (deprecated, and will be removed from 1.3) The sample code for connection/cursor testing: Ex1: #/usr/bin/python import MySQLdb cxn = MySQLdb.connect(host='localhost', db='test', user='root', passwd='admin123' ) ## create cursor cur = cxn.cursor() ## query from database #cur.query(" select * from test.t limit 5; ") ## no query attribute for cursor cur.execute(" select * from test.t limit 5; ") for data in cur.fetchall():   #print(data[0],data[1],data[2])   print(data[0],data[1]) cur.close() cxn.close() Ex2: #/usr/bin/python import MySQLdb cxn = MySQLdb.connect(host='localhost', db='test', user='root', passwd='admin123' ) cur = cxn.cursor() ...

[Level 2] MySQLdb Python API -- Module Attributes

If you want to get some information about MySQLdb API, you can use the following script to get the info. #!/usr/bin/python try:   import MySQLdb   db = MySQLdb   if db:     print "db.apilevel=" + db.apilevel     print "db.threadsafety=" + str(db.threadsafety)     print "db.paramstyle=" + db.paramstyle except ImportError:   print "import MySQLdb fail"   exit The attributes description: a. apilevel => Version of API. b. threadsafety => Level of thread safety     0: Not threadsafe. Should not share the module at all.     1: Minimally threadsafe. Could share module but not connection. ( Default )     2: Moderately threadsafe. Could share both module and connection but not cursors.     3: Fully threadsafe. Could share module, connection and cursor. c. paramstyle => Parameter style of the module     numeric: WHERE addr=:...

[Level 3] Install Python MySQLdb.

When you want to write a Python program to connect to MySQL database, there is a useful API for Python to connect database, called "MySQLdb". There are the steps for you to install the package: 1. Download setuptools: # wget -q http://peak.telecommunity.com/dist/ez_setup.py # python ez_setup.py 2. Download MySQLdb from SourceForge : 3. Build MySQLdb and install it: (I download MySQLdb version for 1.2.3c1) # gunzip -c ./MySQL-python-1.2.3c1.tar.gz | tar xvf - # cd ./MySQL-python-1.2.3c1 # python setup.py build # python setup.py install 4. Prepare Python script. testMySQLdb.py: #!/usr/bin/python import MySQLdb cxn = MySQLdb.connect(host='localhost', db='test', user='root', passwd='admin123' ) ## execute command only cxn.query("grant all on test.* to 'stanley'@'localhost' identified by 'stanley123'") cxn.query("drop user 'stanley'@'localhost'") ## query from datab...