无法使用MySQLdb创建游标

时间:2013-05-13 16:04:38

标签: python mysql mysql-python

我使用以下代码连接到数据库:

#generate connection to database
def connection():
    return MySQLdb.connect(host   = __HOST__,
                           user   = __USER__,
                           passwd = __PASS__,
                           port   = __PORT__,
                           db     = __DFDB__)
db = connection()

连接后我可以看到连接已打开

<_mysql.connection open to 'db' at memory>

然而我无法获得光标:

>>> db.cursor()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Python/2.7.3/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/MySQLdb/connections.py", line 243, in cursor
    return (cursorclass or self.cursorclass)(self)
AttributeError: 'Connection' object has no attribute 'cursorclass'

我真的不确定这里发生了什么,我有效地逐字复制MySQLdb文档。我已经看到了使用字典类的修复,但我只对使用标准游标感兴趣。这里有什么问题?我使用的是python 2.7.3和MySQLdb 1.2.3。

这是更多错误输出:

*** glibc detected *** python: free(): invalid pointer: 0x0000000009489520 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x760e6)[0x7fc806f6d0e6]
/usr/local/mysql-5.1.49/lib64/libmysqlclient_r.so.16(+0x7d150)[0x7fc8004b4150]
/usr/local/mysql-5.1.49/lib64/libmysqlclient_r.so.16(mysql_close+0x72)[0x7fc8004b4bb2]
/usr/local/Python/2.7.3/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.so(+0x7510)[0x7fc800850510]
/usr/local/Python/2.7.3/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.so(+0x75df)[0x7fc8008505df]
python[0x464673]
python[0x4f37b3]
python[0x4fc1ba]
python[0x4c794b]
python[0x4c795b]
python[0x4487e3]
python(PyDict_SetItem+0x70)[0x4496e0]
python(PyDict_SetItemString+0x4b)[0x44a3cb]
python(PyImport_Cleanup+0xcc)[0x4b12bc]
python(Py_Finalize+0xfb)[0x4bfe5b]
python(Py_Main+0x4f6)[0x414706]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x7fc806f15cdd]
python[0x413e39]
======= Memory map: ========
00400000-0055e000 r-xp 00000000 00:15 145437598                          /usr/local/Python/2.7.3/bin/python2.7
0075d000-00799000 rw-p 0015d000 00:15 145437598                          /usr/local/Python/2.7.3/bin/python2.7
00799000-007a8000 rw-p 00000000 00:00 0
01d98000-094db000 rw-p 00000000 00:00 0                                  [heap]
7fc7f8000000-7fc7f8021000 rw-p 00000000 00:00 0
7fc7f8021000-7fc7fc000000 ---p 00000000 00:00 0
7fc7fc314000-7fc7fc32a000 r-xp 00000000 fd:01 406589                     /lib64/libresolv-2.12.so
7fc7fc32a000-7fc7fc52a000 ---p 00016000 fd:01 406589                     /lib64/libresolv-2.12.so
7fc7fc52a000-7fc7fc52b000 r--p 00016000 fd:01 406589                     /lib64/libresolv-2.12.so
7fc7fc52b000-7fc7fc52c000 rw-p 00017000 fd:01 406589                     /lib64/libresolv-2.12.so
7fc7fc52c000-7fc7fc52e000 rw-p 00000000 00:00 0
7fc7fc52e000-7fc7fc533000 r-xp 00000000 fd:01 393350                     /lib64/libnss_dns-2.12.so
7fc7fc533000-7fc7fc732000 ---p 00005000 fd:01 393350                     /lib64/libnss_dns-2.12.so
7fc7fc732000-7fc7fc733000 r--p 00004000 fd:01 393350                     /lib64/libnss_dns-2.12.so
7fc7fc733000-7fc7fc734000 rw-p 00005000 fd:01 393350                     /lib64/libnss_dns-2.12.so
7fc7fc734000-7fc7fc740000 r-xp 00000000 fd:01 402938                     /lib64/libnss_files-2.12.so
7fc7fc740000-7fc7fc940000 ---p 0000c000 fd:01 402938                     /lib64/libnss_files-2.12.so
7fc7fc940000-7fc7fc941000 r--p 0000c000 fd:01 402938                     /lib64/libnss_files-2.12.so
7fc7fc941000-7fc7fc942000 rw-p 0000d000 fd:01 402938                     /lib64/libnss_files-2.12.so
7fc7fc942000-7fc7fc958000 r-xp 00000000 fd:01 393226                     /lib64/libgcc_s-4.4.7-20120601.so.1
7fc7fc958000-7fc7fcb57000 ---p 00016000 fd:01 393226                     /lib64/libgcc_s-4.4.7-20120601.so.1
7fc7fcb57000-7fc7fcb58000 rw-p 00015000 fd:01 393226                     /lib64/
libgcc_s-4.4.7-20120601.so.1
7fc7fcb58000-7fc7fcb59000 ---p 00000000 00:00 0
7fc7fcb59000-7fc7fd559000 rw-p 00000000 00:00 0
7fc7fd559000-7fc7fd55d000 r-xp 00000000 00:15 144816624                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_locale.so
7fc7fd55d000-7fc7fd75d000 ---p 00004000 00:15 144816624                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_locale.so
7fc7fd75d000-7fc7fd75e000 rw-p 00004000 00:15 144816624                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_locale.so
7fc7fd75e000-7fc7fd761000 r-xp 00000000 00:15 160415290                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_heapq.so
7fc7fd761000-7fc7fd961000 ---p 00003000 00:15 160415290                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_heapq.so
7fc7fd961000-7fc7fd963000 rw-p 00003000 00:15 160415290                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_heapq.so
7fc7fd963000-7fc7fd965000 r-xp 00000000 00:15 160415289                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_bisect.so
7fc7fd965000-7fc7fdb64000 ---p 00002000 00:15 160415289                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_bisect.so
7fc7fdb64000-7fc7fdb65000 rw-p 00001000 00:15 160415289                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_bisect.so
7fc7fdb65000-7fc7fdb6f000 r-xp 00000000 00:15 160415285                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/itertools.so
7fc7fdb6f000-7fc7fdd6e000 ---p 0000a000 00:15 160415285                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/itertools.so
7fc7fdd6e000-7fc7fdd73000 rw-p 00009000 00:15 160415285                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/itertools.so
7fc7fdd73000-7fc7fdd7b000 r-xp 00000000 00:15 160415291                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/operator.so
7fc7fdd7b000-7fc7fdf7a000 ---p 00008000 00:15 160415291                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/operator.so
7fc7fdf7a000-7fc7fdf7c000 rw-p 00007000 00:15 160415291                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/operator.so
7fc7fdf7c000-7fc7fdf83000 r-xp 00000000 00:15 160415288                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_collections.so
7fc7fdf83000-7fc7fe182000 ---p 00007000 00:15 160415288                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_collections.so
7fc7fe182000-7fc7fe184000 rw-p 00006000 00:15 160415288                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/_collections.so
7fc7fe2f1000-7fc7fe2f9000 r-xp 00000000 00:15 160415279                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/array.so
7fc7fe2f9000-7fc7fe4f8000 ---p 00008000 00:15 160415279                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/array.so
7fc7fe4f8000-7fc7fe4fb000 rw-p 00007000 00:15 160415279                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/array.so
7fc7ff54c000-7fc7ff55e000 r-xp 00000000 00:15 160415284                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/datetime.so
7fc7ff55e000-7fc7ff75d000 ---p 00012000 00:15 160415284                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/datetime.so
7fc7ff75d000-7fc7ff761000 rw-p 00011000 00:15 160415284                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/datetime.so
7fc7ff761000-7fc7ff765000 r-xp 00000000 00:15 160415283                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/time.so
7fc7ff765000-7fc7ff964000 ---p 00004000 00:15 160415283                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/time.so
7fc7ff964000-7fc7ff966000 rw-p 00003000 00:15 160415283                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/time.so
7fc7ff966000-7fc7ff96e000 r-xp 00000000 00:15 160415281                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/math.so
7fc7ff96e000-7fc7ffb6d000 ---p 00008000 00:15 160415281                  /usr/local/Python/2.7.3/lib/python2.7/lib-dynload/math.so
7fc7ffb6d000-7fc7ffb6f000 rw-p 00007000 00:15 160415281                  /usr/lo
cal/Python/2.7.3/lib/python2.7/lib-dynload/math.so
7fc7ffb6f000-7fc7ffbcc000 r-xp 00000000 fd:01 393221                     /lib64/libfreebl3.so
7fc7ffbcc000-7fc7ffdcb000 ---p 0005d000 fd:01 393221                     /lib64/libfreebl3.so
7fc7ffdcb000-7fc7ffdcc000 r--p 0005c000 fd:01 393221                     /lib64/libfreebl3.so
7fc7ffdcc000-7fc7ffdcd000 rw-p 0005d000 fd:01 393221                     /lib64/libfreebl3.so
7fc7ffdcd000-7fc7ffdd1000 rw-p 00000000 00:00 0
7fc7ffdd1000-7fc7ffde7000 r-xp 00000000 fd:01 394125                     /lib64/libnsl-2.12.so
7fc7ffde7000-7fc7fffe6000 ---p 00016000 fd:01 394125                     /lib64/libnsl-2.12.so
7fc7fffe6000-7fc7fffe7000 r--p 00015000 fd:01 394125                     /lib64/libnsl-2.12.so
7fc7fffe7000-7fc7fffe8000 rw-p 00016000 fd:01 394125                     /lib64/libnsl-2.12.so
7fc7fffe8000-7fc7fffea000 rw-p 00000000 00:00 0
7fc7fffea000-7fc7ffff1000 r-xp 00000000 fd:01 393248                     /lib64/libcrypt-2.12.so
7fc7ffff1000-7fc8001f1000 ---p 00007000 fd:01 393248                     /lib64/libcrypt-2.12.so
7fc8001f1000-7fc8001f2000 r--p 00007000 fd:01 393248                     /lib64/libcrypt-2.12.so
7fc8001f2000-7fc8001f3000 rw-p 00008000 fd:01 393248                     /lib64/libcrypt-2.12.so
7fc8001f3000-7fc800221000 rw-p 00000000 00:00 0
7fc800221000-7fc800236000 r-xp 00000000 fd:01 393320                     /lib64/libz.so.1.2.3
7fc800236000-7fc800435000 ---p 00015000 fd:01 393320                     /lib64/libz.so.1.2.3
7fc800435000-7fc800436000 r--p 00014000 fd:01 393320                     /lib64/libz.so.1.2.3
7fc800436000-7fc800437000 rw-p 00015000 fd:01 393320                     /lib64/libz.so.1.2.3
7fc800437000-7fc80060a000 r-xp 00000000 00:15 153064441                  /usr/local/mysql-5.1.49/lib64/libmysqlclient_r.so.16
7fc80060a000-7fc80080a000 ---p 001d3000 00:15 153064441                  /usr/local/mysql-5.1.49/lib64/libmysqlclient_r.so.16
7fc80080a000-7fc800848000 rw-p 001d3000 00:15 153064441                  /usr/local/mysql-5.1.49/lib64/libmysqlclient_r.so.16
7fc800848000-7fc800849000 rw-p 00000000 00:00 0
7fc800849000-7fc800853000 r-xp 00000000 00:15 135828401                  /usr/local/Python/2.7.3/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.so
7fc800853000-7fc800a52000 ---p 0000a000 00:15 135828401                  /usr/local/Python/2.7.3/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.soAborted

1 个答案:

答案 0 :(得分:0)

使用此代码:

import sys
import MySQLdb
import re
mydb = MySQLdb.connect(host='localhost',
user='root',
passwd='shailang',
db='mydb')
cursor = mydb.cursor()