Python forum for Python programmers

MySQL: AttributeError: cursor

Feb 11, 2012 5:40 pm
Kevin Murphy

Hi All,
I'm using Python 2.7 and having a problem creating the cursor below.
Any suggestions would be appreciated!

import sys
import _mysql

print "cursor test"

db =
_mysql.connect(host="localhost",user="root",passwd="mypw",db="python-
test")

cursor = db.cursor()

>>>
cursor test

Traceback (most recent call last):
File "C:\Python27\dbconnect.py", line 8, in <module>
cursor = db.cursor()
AttributeError: cursor

Feb 11, 2012 6:07 pm
Albert W. Hopkins
Re: MySQL: AttributeError: cursor

On Sat, 2012-02-11 at 09:40 -0800, Kevin Murphy wrote:
> Hi All,
> I'm using Python 2.7 and having a problem creating the cursor below.
> Any suggestions would be appreciated!
>
> import sys
> import _mysql
>
> print "cursor test"
>
> db =
> _mysql.connect(host="localhost",user="root",passwd="mypw",db="python-
> test")
>
> cursor = db.cursor()
>
> >>>
> cursor test
>
> Traceback (most recent call last):
> File "C:\Python27\dbconnect.py", line 8, in <module>
> cursor = db.cursor()
> AttributeError: cursor

You are using the low-level C API wrapper (not sure why). Most people,
I believe, would use the pythonic API:

>>> import MySQLdb
>>> db = MySQLdb.connect(host='localhost', user='root', passwd='mypw',
db='python-test')
>>> cursor = db.cursor()

The low-level API is... well.. for when you want to do low-level stuff.
You probably want the pythonic API.

-a






Previous Thread: ANN: Sarge, a library wrapping the subprocess module,has been released.
Next Thread: Stopping a looping computation in IDLE 3.2.x

Related Forum Topics
Py 3.2: sqlite can't operate on a closed cursor
I installed Python 3.2, suddenly getting an error on my sqlite pages
(CGI):

ProgrammingError: Cannot operate on a closed cursor

Works normally in interactive mode. Seems to error when the sqlite
statement is in a function. Are sqlite objects now required to be
declared global?

--...
Is there a way to creat a func that returns a cursor that can be used?
Is there a way to create a func that returns a cursor that can be used to execute sql statements?

I tried this (after importing sqlite3), but it gave me the error below:

>>> def connect():
conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db .... etc.
cur = conn.cursor()
...
AttributeError in "with" statement (3.2.2)
I'm using Python 3.2.2, and the following program gives me an error
that I don't understand:

class Foo:
pass

foo = Foo()
foo.name = "Steve"

def add_goodbye_function(obj):
def goodbye():
print("goodbye " + obj.name)
obj.goodbye =...
Context Manager getting str instead of AttributeError instance
So I have a context manager used to catch errors

def __exit__( self, exceptionClass, exception, tracebackObject ):
if isinstance( exception, self.exceptionClasses ):
#do something here

Normally exception would be the exception instance, but for
AttributeError it seems to be...
AttributeError: 'module' object has no attribute 'logger'
hi all, when installing sage, there is a problem with emacs.py
so, this screen appeared after rynning ./sage
----------------------------------------------------------------------
| Sage Version 4.4.2, Release Date: 2010-05-19 |
| Type notebook() for the GUI, and...
Query from sqlalchemy returns AttributeError: 'NoneType' object
from pox.core import core
import pox.openflow.libopenflow_01 as of
import re
import datetime

from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from...
Debugging difficulty in python with __getattr__, decorated propertiesand AttributeError.
--047d7b6d826eb0d6dc04dbc4ad99
Content-Type: text/plain; charset=UTF-8

Is there any way to raise the original exception that made the call to
__getattr__? I seem to stumble upon a problem where multi-layered attribute
failure gets obscured due to use of __getattr__. Here's a dummy code...
Will MySQL ever be supported for Python 3.x?
The MySQLdb entry on SourceForge
(http://sourceforge.net/projects/mysql-python/)
web site still says the last supported version of Python is 2.6.
PyPi says the last supported version is Python 2.5. The
last download is from 2007.

I realize there are unsupported fourth-party...
SQLAlchemy: How to do Table Reflection and MySQL?
Hi, I'm fairly new to Python, and I'm trying to figure out how to use
SQLAlchemy to connect to a MySQL DB and use table reflection to set up
SQLAlchemy's tables. But the SQLAlchemy documentation is gigantic and
frankly kinda making my head spin, so I'm having trouble even finding
any...
Why Doesn't This MySQL Statement Execute?
--bcaec517a5720803d204d12742d3
Content-Type: text/plain; charset=ISO-8859-1

Hi;
I have this test code:

if i_id == "1186":
sql = 'insert into interactions values(Null, %s, "Call Back", "%s")'
% (i_id, date_plus_2)
cursor.execute(sql)
db.commit()
print...