如何从数据库中获取数据。MySQL和Python使用

如何从数据库中获取数据。MySQL和Python使用,python,mysql,Python,Mysql,如何从数据库中获取数据。我需要从MySQL数据库中随机抽取50个项目 from random import randint database = MySQLdb.connect(host="localhost", port=8080, user="user", passwd="1234",database="a123") cur = database.cursor() cur.execute("""SELECT School FROM `a123`.`b123` randint 5

如何从数据库中获取数据。我需要从MySQL数据库中随机抽取50个项目

from random import randint
database = MySQLdb.connect(host="localhost", port=8080,  user="user", passwd="1234",database="a123")
cur = database.cursor()

cur.execute("""SELECT School  FROM  `a123`.`b123` randint 5   """)
database.commit()

prnt= cur.fetchall()
print(prnt)

它不工作。

您的SQL查询不正确。最重要的是,代码中缺少了导入MySQLdb

database = MySQLdb.connect(host="localhost", port=8080,
                     user="user", passwd="1234", db="a123")
cur = database.cursor()

cur.execute("""
    SELECT 
        School
     FROM 
        `a123`.`b123` 
    ORDER BY RAND()
    LIMIT 50;
""")
database.commit()

prnt = cur.fetchall()
print(prnt)
一定是工作。这是一个基本建议,您应该修复数据库的名称和表名