|
|
|
Tell us what you think of the site.
|
Autodesk Media & Entertainment User Community
|
Autodesk® 3ds Max®
|
|
Autodesk® Maya®
|
|
Autodesk® Softimage®
|
|
Autodesk® MotionBuilder®
|
|
Autodesk® Mudbox™
|
|
Autodesk® ImageModeler™
|
|
Autodesk® Sketchbook® Pro
|
|
Autodesk® Smoke on Mac®
|
| Connecting Maya 2011 with MYsqldb?
|
|
|
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]
|
|
|
|
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(hostname, username, password, dbname):
# 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.Error, e:
# 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
|
|
|
|
|
Hi Jeroen,
I failed to return the connection. So it returned “none”.
Thank you Jeroen
Author: newbee_
|
| Replied: 17 October 2011 05:41 AM
|
|
|
|
|
|
|