mirror of
https://github.com/PyMySQL/mysqlclient.git
synced 2025-08-15 11:10:58 +08:00
128 lines
5.0 KiB
Plaintext
128 lines
5.0 KiB
Plaintext
<!doctype linuxdoc system>
|
|
<article>
|
|
<title>MySQLdb FAQ
|
|
<author>Andy Dustman
|
|
<date>$Id$
|
|
<sect>Compiling <tt/_mysqlmodule.so/
|
|
<P>Here are some common errors that happen during the build.
|
|
This section covers UNIX/Linux problems only, as I don't do Windows.
|
|
<tt/.so/ is a dynamically loading library on Linux and most other UNIX
|
|
variants; a few use extensions other than <tt/.so/. Windows probably
|
|
uses <tt/.dll/.
|
|
<sect1>ImportError: libmysqlclient.so.6: cannot open shared object file:
|
|
No such file or directory
|
|
<P>
|
|
You have dynamic MySQL libraries, and by default, your compiler links
|
|
<tt/_mysqlmodule.so/ against these, but these are not on your loader path
|
|
when you start Python.
|
|
You have two basic options:
|
|
<p>
|
|
<enum>
|
|
<item>
|
|
Modify setup.py so that it links against the static library; see the comments.
|
|
<item>
|
|
Change your system environment so that the MySQL libraries are on
|
|
your loader path. With Linux, you can modify <tt>/etc/ld.so.conf</tt> (see
|
|
<tt/man ldconfig/ for more details) or you can add to or create the
|
|
<tt/LD_LIBRARY_PATH/ environment variable before starting Python, i.e.
|
|
<p><code>
|
|
LD_LIBRARY_PATH=/path/to/mysql/libs python ... # Bourne-ish shell
|
|
</code>
|
|
</enum>
|
|
<sect1>ImportError: ./_mysqlmodule.so: undefined symbol: PyLong_FromUnsignedLongLong
|
|
<p>
|
|
<tt/PyLong_FromUnsignedLongLong()/ first appears in Python 1.5.2, so you are
|
|
linking against an earlier version. You may also have more than one version
|
|
installed. Get Python 1.5.2 from your vendor or python.org.
|
|
|
|
<sect1>ImportError: ./_mysqlmodule.so: undefined symbol: uncompress
|
|
<P>
|
|
It seems that MySQL-3.23 client libraries require libz for gzip
|
|
compression. setup.py should add this automatically.
|
|
|
|
<sect1>./_mysqlmodule.c:33: mysql.h: No such file or directory
|
|
<P>The include path (-I) to your MySQL include files is wrong; modify
|
|
setup.py. OR: You don't have the MySQL development stuff loaded. If you
|
|
are using the Red Hat RPMs, you need the <tt/MySQL-devel/ RPM to compile
|
|
<tt/_mysqlmodule.so/. However, if you link against the static MySQL
|
|
libraries (see above), you can install <tt/_mysqlmodule.so/ on a system
|
|
that does not have the MySQL client libraries (<tt/libmysqlclient/).
|
|
|
|
<sect1>I'm using Windows...
|
|
<P>Say no more.
|
|
<P>I don't use Windows. setup.py is supposed to work for building.
|
|
There may also be a link to some user-contributed binaries on the web site.
|
|
</sect1>
|
|
<sect>
|
|
Trouble with ZMySQLDA
|
|
<p>What? ZMySQLDA never fails! Well, actually, I just don't have any
|
|
good questions yet. Except: Install MySQLdb first, and then untar
|
|
the ZMySQLDA source into your Zope home, and restart Zope.
|
|
<sect>Using MySQLdb
|
|
<p>
|
|
MySQLdb is a
|
|
<htmlurl url="http://www.python.org/topics/database/DatabaseAPI-2.0.html"
|
|
name="Python Database API Specification 2.0"> database module, so you
|
|
should be familiar with the spec. Deviations from the spec are documented in the
|
|
<htmlurl url="http://dustman.net/andy/python/MySQLdb/doc/MySQLdb.html"
|
|
name="MySQLdb documentation">.
|
|
<sect1>What do I do if I am completely clueless?
|
|
<p>Get a clue. Clues have been provided in the <tt/examples/ directory
|
|
of the MySQLdb distribution.
|
|
<sect1>No, I mean <em/really/ clueless!
|
|
<p>Okay, it goes something like this:
|
|
<enum>
|
|
<item>
|
|
Import MySQLdb.
|
|
<item>
|
|
Create a Connection object.
|
|
<item>
|
|
Create a Cursor object.
|
|
<item>
|
|
Execute your query on the Cursor object.
|
|
<item>
|
|
If your query returns rows, you can use the Cursor object to fetch them.
|
|
<item>
|
|
Rinse, lather, repeat.
|
|
</enum>
|
|
Example:
|
|
<code>
|
|
import MySQLdb
|
|
db = MySQLdb.connect(db='mydb',user='myuser',passwd='mypasswd')
|
|
c = db.cursor()
|
|
c.execute(myquery)
|
|
results = c.fetchall()
|
|
</code>
|
|
<sect1>But MySQL doesn't have cursors!
|
|
<p>True enough. MySQLdb fakes it, though, because the spec requires it.
|
|
<sect1>cursor.rollback() always fails!
|
|
<p>MySQL now supports transactions using BDB tables.
|
|
If your server doesn't support them, rollbacks will always fail, as they should,
|
|
because it can't do what you asked.
|
|
Even if your server does support them, rollbacks will fail if you
|
|
modified any non-BDB tables.
|
|
<p>OTOH, <tt/cursor.commit()/, which attempts to commit the transaction
|
|
to the database, <em/does/ exist and always succeeds, because MySQL
|
|
essentially is always in auto-commit mode.
|
|
<sect1>How do I use some of the special MySQL features?
|
|
<P>Short answer: Don't, if you can avoid it. Your program will not
|
|
be portable to other databases.
|
|
<P>Long answer: MySQLdb exports all symbols from _mysql. There are only
|
|
a couple MySQL functions available this way, though. The Connection object
|
|
does wrap nearly all of the various MySQL calls that use a <tt/MYSQL/
|
|
argument (the connection handle in the C API). So let's say you want to
|
|
use <tt/mysql_select_db(newdb)/. In MySQLdb, that's
|
|
<tt/db.select_db(newdb)/ where <tt/db/ is your Connection object.
|
|
<sect1>I still wanna use _mysql directly.
|
|
<p>Well, it <tt/may/ be appropriate in some cirumstances.
|
|
ZMySQLDA does this, because MySQLdb does a lot of type conversion that
|
|
isn't necessary for Zope's purposes.
|
|
<enum>
|
|
<item>
|
|
Read the MySQL docs, particularly the C API, for an overview.
|
|
<item>
|
|
Read the MySQLdb docs. This shows how the C API is transliterated
|
|
into Python.
|
|
</enum>
|
|
</article>
|