Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® Maya® / Python / Connecting Maya 2011 with MYsqldb?
  RSS 2.0 ATOM  

Connecting Maya 2011 with MYsqldb?
Rate this thread
 
60947
 
Permlink of this thread  
avatar
  • newbee_
  • Posted: 12 October 2011 08:16 AM
  • Total Posts: 3
  • Joined: 12 October 2011 07:45 AM

Hi,

I am using Maya 2011(64bit) and MySQL 5.5 (64 bit) in Windows 7 (64 bit) machine. I tried to connect maya with Mysqldb through python. So i copied the connector files into maya\python\lib\site packages.  I was able to import MYsqldb module without any error. But when i tried call the cursor object(for querying), I found that Maya is not recognizing the cursor object.

Here is my sample code:

import MySQLdb as mb
import maya.cmds as cmds

def mysql_connect(hostname, username, password, dbname):
db = mb.connect(host=hostname,user=username,passwd=password,db=dbname)

db = mysql_connect("localhost", “root”, “test”, “mydbt")
dbcursor = db.cursor()
dbcursor.execute("select * from maya")
# Error: AttributeError: ‘NoneType’ object has no attribute ‘cursor’ #

I tried verifying the env-path variables, replacing the connector files but still the problem persists.

Since being a beginner, i am un-able to identify the exact issue.

Any suggestions?
newbee_
[/size]



Replies: 0
avatar

Hey newbee_,

If you can import the module without any trouble it can’t be an issue with your env. It seems your function does not return the connection, so you cant request a cursor from it.

import MySQLdb as mb
import maya
.cmds as cmds

def mysql_connect
(hostnameusernamepassworddbname):
    
# try to establish a connection and return the connection
    
try:
        
conn mb.connect(host=hostname,user=username,passwd=password,db=dbname)
        return 
conn
    except MySQLdb
.Errore:
        
# in case it fails, return an error
        
print "MySQL error %d: %s" % (e.args[0]e.args[1])
    
# return None when there is no connection
    
return None

db 
mysql_connect("localhost"“root”“test”“mydbt")
# check if db is None, if so, don't create a cursor
if not db is None:
    dbcursor = db.cursor()
    dbcursor.execute("
SELECT FROM maya")
    print dbcursor.fetchall() #output the results

It has some error handling in it, in case you get errors.

Hope this helps,

Jeroen



Replies: 1
/img/forum/dark/default_avatar.png

Hi Jeroen,

I failed to return the connection. So it returned “none”.

Thank you Jeroen

Author: newbee_

Replied: 17 October 2011 05:41 AM