

{"id":145316,"date":"2025-06-16T19:02:53","date_gmt":"2025-06-16T13:32:53","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=145316"},"modified":"2025-06-16T19:02:53","modified_gmt":"2025-06-16T13:32:53","slug":"how-to-fetch-one-record-from-database-in-python","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/","title":{"rendered":"How to Fetch One Record From Database in Python"},"content":{"rendered":"<h3>Program 1<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Program to search one record (based on PK)\r\nimport MySQLdb\r\ntry:\r\n    con=MySQLdb.connect(host='localhost',user='root',password='root',database='college')   \r\n    #print(\"Data base connected\")\r\n    empid=int(input(\"Enter employee id for search: \"))\r\n    sql=\"select * from employee where eid={}\"\r\n    sql=sql.format(empid)\r\n    cur=con.cursor()\r\n    cur.execute(sql)\r\n    result=cur.fetchone()\r\n    if(result):\r\n        print(\"------------------------------------------\")\r\n        print(\"Emp Id\\tName\\tDepartment\\tSalary\\tGender\\tCity\")\r\n        print(\"------------------------------------------\")\r\n        print(\"%d\\t%s\\t%s\\t%d\\t%s\\t%s\"%(result[0],result[1],result[2],result[3],result[4],result[5]))\r\n    else:\r\n        print(\"******* No Record Found*********\")    \r\nexcept Exception as obj:\r\n    print(obj)<\/pre>\n<h3>Program 2<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Program to search record acording to Name\r\nimport MySQLdb\r\ntry:\r\n    con=MySQLdb.connect(host='localhost',user='root',password='root',database='college')   \r\n    #print(\"Data base connected\")\r\n    empname=input(\"Enter employee name for search: \")\r\n    sql=\"select * from employee where ename='{}'\"\r\n    sql=sql.format(empname)\r\n    cur=con.cursor()\r\n    cur.execute(sql)\r\n    result=cur.fetchall()\r\n    flag=True\r\n    print(\"------------------------------------------\")\r\n    print(\"Emp Id\\tName\\tDepartment\\tSalary\\tGender\\tCity\")\r\n    for row in result:\r\n        flag=False\r\n        print(\"%d\\t%s\\t%s\\t%d\\t%s\\t%s\"%(row[0],row[1],row[2],row[3],row[4],row[5]))\r\n    print(\"------------------------------------------\")    \r\n    if(flag==True):\r\n        print(\"******* No Record Found*********\")    \r\nexcept Exception as obj:\r\n    print(obj)<\/pre>\n<h3>Program 3<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Program to search record acording to Department\r\nimport MySQLdb\r\ntry:\r\n    con=MySQLdb.connect(host='localhost',user='root',password='root',database='college')   \r\n    #print(\"Data base connected\")\r\n    empdept=input(\"Enter employee department for search: \")\r\n    sql=\"select * from employee where edept='{}'\"\r\n    sql=sql.format(empdept)\r\n    cur=con.cursor()\r\n    cur.execute(sql)\r\n    result=cur.fetchall()\r\n    flag=True\r\n    print(\"------------------------------------------\")\r\n    print(\"Emp Id\\tName\\tDepartment\\tSalary\\tGender\\tCity\")\r\n    for row in result:\r\n        flag=False\r\n        print(\"%d\\t%s\\t%s\\t%d\\t%s\\t%s\"%(row[0],row[1],row[2],row[3],row[4],row[5]))\r\n    print(\"------------------------------------------\")    \r\n    if(flag==True):\r\n        print(\"******* No Record Found*********\")    \r\nexcept Exception as obj:\r\n    print(obj)<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Program 1 # Program to search one record (based on PK) import MySQLdb try: con=MySQLdb.connect(host=&#8217;localhost&#8217;,user=&#8217;root&#8217;,password=&#8217;root&#8217;,database=&#8217;college&#8217;) #print(&#8220;Data base connected&#8221;) empid=int(input(&#8220;Enter employee id for search: &#8220;)) sql=&#8221;select * from employee where eid={}&#8221; sql=sql.format(empid) cur=con.cursor() cur.execute(sql) result=cur.fetchone()&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[34418,34417,10333,34419,28626,22366,34420],"class_list":["post-145316","post","type-post","status-publish","format-standard","hentry","category-python","tag-fetch-one-record-from-database-in-python","tag-how-to-fetch-one-record-from-database-in-python","tag-python","tag-python-fetch-one-record-from-database","tag-python-practical","tag-python-program","tag-python-program-on-how-to-fetch-one-record-from-database"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Fetch One Record From Database in Python - DataFlair<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Fetch One Record From Database in Python - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Program 1 # Program to search one record (based on PK) import MySQLdb try: con=MySQLdb.connect(host=&#039;localhost&#039;,user=&#039;root&#039;,password=&#039;root&#039;,database=&#039;college&#039;) #print(&quot;Data base connected&quot;) empid=int(input(&quot;Enter employee id for search: &quot;)) sql=&quot;select * from employee where eid={}&quot; sql=sql.format(empid) cur=con.cursor() cur.execute(sql) result=cur.fetchone()&#046;&#046;&#046;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-16T13:32:53+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Fetch One Record From Database in Python - DataFlair","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Fetch One Record From Database in Python - DataFlair","og_description":"Program 1 # Program to search one record (based on PK) import MySQLdb try: con=MySQLdb.connect(host='localhost',user='root',password='root',database='college') #print(\"Data base connected\") empid=int(input(\"Enter employee id for search: \")) sql=\"select * from employee where eid={}\" sql=sql.format(empid) cur=con.cursor() cur.execute(sql) result=cur.fetchone()&#46;&#46;&#46;","og_url":"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2025-06-16T13:32:53+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"How to Fetch One Record From Database in Python","datePublished":"2025-06-16T13:32:53+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/"},"wordCount":13,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["fetch one record from database in python","how to fetch one record from database in python","Python","python fetch one record from database","python practical","python program","python program on how to fetch one record from database"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/","url":"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/","name":"How to Fetch One Record From Database in Python - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2025-06-16T13:32:53+00:00","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/how-to-fetch-one-record-from-database-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"How to Fetch One Record From Database in Python"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/145316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=145316"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/145316\/revisions"}],"predecessor-version":[{"id":145327,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/145316\/revisions\/145327"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=145316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=145316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=145316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}