Python and MySQL: is there an alternative to MySQLdb?

前端 未结 8 1379
挽巷
挽巷 2021-02-05 13:09

Is there a module written purely in Python that will allow a script to communicate with a MySQL database? I\'ve already tried MySQLdb without success. It requires too much: GCC,

相关标签:
8条回答
  • 2021-02-05 13:46

    You can find pre-built binary packages for MySQLdb and its dependencies for most operating systems.

    http://sourceforge.net/project/showfiles.php?group_id=22307&package_id=15775

    What platform are you running on?

    0 讨论(0)
  • 2021-02-05 13:47

    You've looked at these?

    http://wiki.python.org/moin/MySQL

    0 讨论(0)
  • 2021-02-05 13:53

    Lately there is also oursql (docs), which has various advantages over MySQLdb and other existing drivers -- listed at the top of the documentation.

    0 讨论(0)
  • 2021-02-05 13:55

    Oracle implemented a pure python mysql connector, aptly named mysql-connector-python. It can be pip-installed and receives plenty of updates.

    Note that it's GPL licensed, which may or may not be a problem for your prjoect. If you can't live with that, you can also pick up PyMySQL instead which is one of the few connectors that works well and is distributed under MIT.

    0 讨论(0)
  • 2021-02-05 13:56

    I had the problem that I wanted to write code which work for Python 2 and Python 3. I found pymysql to fit perfectly. I did not have to adjust code to switch from MySQLdb to pymysql

    Installation

    $ pip install PyMySQL
    

    If you don't have admin rights:

    $ pip install -u PyMySQL
    

    Usage

    The usage is the same as for MySQLdb:

    SELECT

    import pymysql
    import pymysql.cursors
    connection = pymysql.connect(host=mysql['host'],
                                 user=mysql['user'],
                                 passwd=mysql['passwd'],
                                 db=mysql['db'],
                                 cursorclass=pymysql.cursors.DictCursor)
    cursor = connection.cursor()
    sql = ("SELECT `id`, `formula_in_latex` FROM `wm_formula` "
           "WHERE `is_important` = 1 "
           "AND id != 1 "  # exclude trash class
           "ORDER BY `id` ASC")
    cursor.execute(sql)
    formulas = cursor.fetchall()
    

    INSERT

    import pymysql
    connection = pymysql.connect(host=mysql['host'],
                                 user=mysql['user'],
                                 passwd=mysql['passwd'],
                                 db=mysql['db'])
    cursor = connection.cursor()
    cursor.execute("INSERT INTO `toys` (`id`, `arrival`, `duration`) "
                   "VALUES ('1', '2014-12-01', '123');")
    connection.commit()
    
    0 讨论(0)
  • 2021-02-05 14:01

    As mentioned in earlier answer, MySQL Connector/Python implements the MySQL Server/Client completely in Python. No compiling, just installing.

    Here are the links:
    * https://launchpad.net/myconnpy
    * code: https://code.launchpad.net/myconnpy
    * download: https://launchpad.net/myconnpy/+download (development shapshots)

    It's not complete yet, but it should have enough to keep you going. (FYI, I am the maintainer)

    0 讨论(0)
提交回复
热议问题