Javaexercise.com

Mysql Limit

import mysql.connector
from mysql.connector import errorcode

try:
  
  # Connecting with MySql
  con = mysql.connector.connect(user='root', password='mysql', host='127.0.0.1', database='company')
	
  # Using cursor() method to create cursor object
  cursor = con.cursor()

  # SQL query to update a record.
  sql_query = "SELECT * FROM EMPLOYEE LIMIT 2"

  # Executing query
  cursor.execute(sql_query)

  rows = cursor.fetchall();

  for row in rows:
     print(row)

except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Your user name or password is incorrect")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  con.close()

 

Example: Limit using offset

import mysql.connector
from mysql.connector import errorcode

try:
  
  # Connecting with MySql
  con = mysql.connector.connect(user='root', password='mysql', host='127.0.0.1', database='company')
	
  # Using cursor() method to create cursor object
  cursor = con.cursor()

  # SQL query to update a record.
  sql_query = "SELECT * FROM EMPLOYEE LIMIT 2 OFFSET 1"

  # Executing query
  cursor.execute(sql_query)

  rows = cursor.fetchall();

  for row in rows:
     print(row)

except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Your user name or password is incorrect")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  con.close()

 


Conclusion

Well, in this topic, we learnt to drop table from MySQL database using Python script. While droping the table make sure table exist, else MySQL will raise an exception: no table schema found.

 

Previous