-1

How can I take my datas from my database. I need to take random 50 items from my MySQL DB.

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)

it's not working.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Your SQL query is not true. Most important missing in your code, you forget the import 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)

It must be work. It's a basic suggestion, you should fix the name of your db and table name.