MySQL Table में simply update query execute करके records को select कर सकते हैं। MySQL Query के according single या multiple records select हो सकते हैं।

Python MySQL Select Example

MySQL Table से सभी records को select करने के लिए fetchall() method use किया जाता है। fetchall() method tuples की list return करता है , और हर tuple MySQL Table का एक record / row होता है। Records list को हम for Loop या while loop की help से easily iterate कर सकते हैं।


पिछले topics मने हमें एक user table बनाई थी जिसमे कुछ records insert किये थे , अब उन्ही records को show करने की कोशिश करेंगे।

Copy Fullscreen Close Fullscreen
#import module.
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = '',
  database = 'python_db'
)
db_cursor = mydb.cursor()
# query to select all records from table.
query = "SELECT * FROM users"
db_cursor.execute(query)
# get all reocrds.
rows = db_cursor.fetchall()
print('Type : ', type(rows))
for row in rows : print(row)
Output
C:\Users\Rahulkumar\Desktop\python>python mysql_select.py
Type :  <class 'list'>
(1, 'Rahul', 'Kumar', 'myemail@gmail.com')
(2, 'Mohit', 'Kumar', 'xyz@gmail.com')
(4, 'Girish', 'Kumar', None)
(5, 'Gyan Singh', 'Yadav', 'gyanxyz@email.com')
(6, 'Mohan', 'Singh', 'mohan1232@email.com')
(7, 'Sohan', 'singh', 'sihan1321@email.com')
(8, 'Raju', 'verma', None)

Python MySQL Select Particular Column

Example में दिए गए तरीके से records के सभी columns आ जायेंगे , अगर आप किसी particular column को ही select करना चाहते हैं तो उन columns को MySQL Query में define करना पड़ेगा।
See Example -

Copy Fullscreen Close Fullscreen
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = '',
  database = 'python_db'
)
db_cursor = mydb.cursor()
# select only first_name, last_name.
query = "SELECT first_name, last_name FROM users"
db_cursor.execute(query)
rows = db_cursor.fetchall()
for row in rows : print(row)
Output
C:\Users\Rahulkumar\Desktop\python>python mysql_select.py
('Rahul', 'Kumar')
('Mohit', 'Kumar')
('Girish', 'Kumar')
('Gyan Singh', 'Yadav')
('Mohan', 'Singh')
('Sohan', 'singh')
('Raju', 'verma')

Python MySQL Select One Record

MySQL Table से सभी records को select करने के लिए fetchone() method use किया जाता है। fetchone() method single tuple return करता है , जो MySQL Table का पहला record / row होता है।
Example -

Copy Fullscreen Close Fullscreen
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = '',
  database = 'python_db'
)
db_cursor = mydb.cursor()
query = "SELECT * FROM users"
db_cursor.execute(query)
row = db_cursor.fetchone()
print(row)
Output
C:\Users\Rahulkumar\Desktop\python>python mysql_selectone.py
(1, 'Rahul', 'Kumar', 'myemail@gmail.com')

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers