Python MySQL模块

7 投票
4 回答
23729 浏览
提问于 2025-04-16 03:43

我正在开发一个网页应用,需要和MySQL数据库进行连接,但我找不到什么特别好的Python模块。

我特别想要一个快速的模块,能够处理成千上万的连接(还有查询,都是在很短的时间内进行的),而且不会明显影响速度。

4 个回答

2

我通常使用 SQLObject,不过我还没有在压力很大的情况下使用过,所以我不能保证它的性能(不过我也不想说它不好)。

下面是从另一个回答中复制的一些示例代码:

from sqlobject import *

# Replace this with the URI for your actual database
connection = connectionForURI('mysql://server:XXXX')
sqlhub.processConnection = connection

# This defines the columns for your database table. See SQLObject docs for how it
# does its conversions for class attributes <-> database columns (underscores to camel
# case, generally)

class Song(SQLObject):

    name = StringCol()
    artist = StringCol()
    album = StringCol()

# Create fake data for demo - this is not needed for the real thing
def MakeFakeDB():
    Song.createTable()
    s1 = Song(name="B Song",
              artist="Artist1",
              album="Album1")
    s2 = Song(name="A Song",
              artist="Artist2",
              album="Album2")

def Main():
    # This is an iterable, not a list
    all_songs = Song.select().orderBy(Song.q.name)

    # Do something by iterating over the song list...
10

我觉得我的回答会是关于游戏领域的更新。

现在有了官方的 MySQL Python 连接器。

安装方法:

sudo pip install mysql-connector-python

或者你也可以从这里下载:

http://dev.mysql.com/downloads/connector/python/

文档说明: http://dev.mysql.com/doc/refman/5.5/en/connector-python.html

7

MySQLdb 是在 Python 中访问 MySQL 数据库几乎唯一的选择。

撰写回答