PHP mysql_fetch_数组的Python等价物

PHP mysql_fetch_数组的Python等价物,python,mysql,sql,mysql-python,Python,Mysql,Sql,Mysql Python,我想在MySQL中获取一个数组。有人能告诉我如何使用Python和MySQLdb来做到这一点吗 例如,我想在Python中执行以下操作: <?php require_once('Config.php'); $q = mysql_query("SELECT * FROM users WHERE firstname = 'namehere'"); $data = mysql_fetch_array($q); echo $data['lastname']; ?>

我想在MySQL中获取一个数组。有人能告诉我如何使用Python和MySQLdb来做到这一点吗

例如,我想在Python中执行以下操作:

<?php

  require_once('Config.php'); 

  $q = mysql_query("SELECT * FROM users WHERE firstname = 'namehere'");
  $data = mysql_fetch_array($q);
  echo $data['lastname'];

?>

  • 安装MySQLdb(MySQL for Python的驱动程序)。键入
    pip安装mysql-python
  • 请阅读,这是Python中访问数据库的标准方法
  • 然后,试试这个:

    >>> import MySQLdb
    >>> connection = MySQLdb.connect(database='test')
    >>> cursor = connection.cursor()
    >>> cursor.execute('SELECT * FROM users WHERE firstname = %s',('somename',))
    >>> results = cursor.fetchall()
    >>> for i in results:
           print i
    
    我会用。像这样的东西可以达到目的:

    引擎=创建引擎('mysql://username:password@主机:端口/数据库') 连接=引擎。连接() 结果=连接。执行(“从用户中选择用户名”) 对于结果中的行: 打印“用户名:”,行['username'] 连接。关闭() 试试:

    import MySQLdb
    connection = MySQLdb.connect(host="localhost",  # your host
                         user="root",  # username
                         passwd="password",  # password
                         db="frateData")  # name of the database)
    cursor = connection.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute('SELECT * FROM users WHERE firstname = %s',['namehere'])
    data = cursor.fetchall()
    print data['lastname']
    
    请注意,通过传递以下参数启动游标:“MySQLdb.cursors.DictCursor”
    将返回一个列表而不是数组,这样您就可以使用它们的键名引用数据,在您的示例中是lastname。

    在python中,您有
    dictionary=True
    ,我已经在python3中进行了测试。它返回的目录与php中的关联数组非常相似。 例如

    您可以使用此(dictionary=True):


    令人担忧的是,这是使用某种形式的SQL转义的唯一答案。感谢您的帮助!我还有一个问题。当我使用它时,数据以如下格式返回:(('dave',),('jake',))。我如何编辑这些数据的格式,就像这样('dave','jake')。我循环遍历返回的数据,并将每个数据用作变量。为了我的目的,这将更容易操纵。再次感谢。
    names=[i[0]代表游标中的i.fetchall()]
    import MySQLdb
    connection = MySQLdb.connect(host="localhost",  # your host
                         user="root",  # username
                         passwd="password",  # password
                         db="frateData")  # name of the database)
    cursor = connection.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute('SELECT * FROM users WHERE firstname = %s',['namehere'])
    data = cursor.fetchall()
    print data['lastname']
    
    import mysql.connector
    cnx = mysql.connector.connect(user='root', password='',host='127.0.0.1',database='test1')
    cursor = cnx.cursor(dictionary=True)
    sql= ("SELECT * FROM `users` WHERE id>0")
    cursor.execute(sql)
    results = cursor.fetchall()
    print(results)
    
    import mysql.connector
    
    db = mysql.connector.connect(user='root', password='',host='127.0.0.1', database='test1')
    
    cursor = db.cursor(dictionary=True)
    cursor.execute("SELECT * FROM table")
    
    for row in cursor:
        print(row['column'])