{"id":3263,"date":"2014-10-20T01:16:53","date_gmt":"2014-10-20T09:16:53","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=3263"},"modified":"2024-01-02T20:06:29","modified_gmt":"2024-01-03T03:06:29","slug":"python-mysql-query","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/","title":{"rendered":"Python MySQL &#8211; Query Data from a Table in Python"},"content":{"rendered":"\n<p class=\"desc\"><strong>Summary<\/strong>: This tutorial shows you how to query data from a MySQL database in Python by using MySQL Connector\/Python API such as <code>fetchone()<\/code> , <code>fetchmany()<\/code> , and <code>fetchall()<\/code> .<\/p>\n\n\n\n<p class=\"note\">This tutorial picks up where the <a href=\"https:\/\/www.mysqltutorial.org\/python-mysql\/python-connecting-mysql-databases\/\">Connecting to a MySQL Database in Python<\/a> left off.<\/p>\n\n\n\n<p class=\"desc\">To query data in a MySQL database from Python, you need to do the following steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, c<a title=\"Python Connecting to MySQL Databases\" href=\"https:\/\/www.mysqltutorial.org\/python-mysql\/python-connecting-mysql-databases\/\">onnect to the MySQL Database<\/a>, you get a <code>MySQLConnection<\/code> object.<\/li>\n\n\n\n<li>Next, create a&nbsp;<code>MySQLCursor<\/code> object from the  <code>MySQLConnection<\/code> object.<\/li>\n\n\n\n<li>Then, use the cursor to execute a query by calling its&nbsp; <code>execute()<\/code> method.<\/li>\n\n\n\n<li>After that, use <code>fetchone()<\/code> ,&nbsp; <code>fetchmany()<\/code> or&nbsp; <code>fetchall()<\/code> method to fetch data from the result set.<\/li>\n\n\n\n<li>Finally, close the cursor as well as the database connection by calling the&nbsp;<code>close()<\/code> method of the corresponding objects.<\/li>\n<\/ul>\n\n\n\n<p>We will show you how to use <code>fetchone()<\/code> , <code>fetchmany()<\/code> , and&nbsp; <code>fetchall()<\/code> methods in more detail in the following sections.<\/p>\n\n\n\n<h2 class=\"wp-block-heading desc\">Querying data with fetchone() method<\/h2>\n\n\n\n<p>The&nbsp; <code>fetchone()<\/code> method returns the next row of a query result set or <code>None<\/code> in case there is no row left. Let&#8217;s take a look at the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> mysql.connector <span class=\"hljs-keyword\">import<\/span> MySQLConnection, Error\n<span class=\"hljs-keyword\">from<\/span> config <span class=\"hljs-keyword\">import<\/span> read_config\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">query_with_fetchone<\/span><span class=\"hljs-params\">(config)<\/span>:<\/span>\n    <span class=\"hljs-comment\"># Initialize variables for cursor and connection<\/span>\n    cursor = <span class=\"hljs-literal\">None<\/span>\n    conn = <span class=\"hljs-literal\">None<\/span>\n\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-comment\"># Establish a connection to the MySQL database using the provided configuration<\/span>\n        conn = MySQLConnection(**config)\n        \n        <span class=\"hljs-comment\"># Create a cursor to interact with the database<\/span>\n        cursor = conn.cursor()\n        \n        <span class=\"hljs-comment\"># Execute a SELECT query to retrieve all rows from the 'books' table<\/span>\n        cursor.execute(<span class=\"hljs-string\">\"SELECT * FROM books\"<\/span>)\n\n        <span class=\"hljs-comment\"># Fetch the first row<\/span>\n        row = cursor.fetchone()\n\n        <span class=\"hljs-comment\"># Loop through all rows and print them<\/span>\n        <span class=\"hljs-keyword\">while<\/span> row <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-literal\">None<\/span>:\n            print(row)\n            row = cursor.fetchone()\n\n    <span class=\"hljs-keyword\">except<\/span> Error <span class=\"hljs-keyword\">as<\/span> e:\n        <span class=\"hljs-comment\"># Print an error message if an error occurs during the execution of the query<\/span>\n        print(e)\n\n    <span class=\"hljs-keyword\">finally<\/span>:\n        <span class=\"hljs-comment\"># Close the cursor and connection in the 'finally' block to ensure it happens<\/span>\n        <span class=\"hljs-keyword\">if<\/span> cursor:\n            cursor.close()\n        <span class=\"hljs-keyword\">if<\/span> conn:\n            conn.close()\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    <span class=\"hljs-comment\"># Read the database configuration from the 'config' module<\/span>\n    config = read_config()\n    \n    <span class=\"hljs-comment\"># Call the function with the obtained configuration to execute the query<\/span>\n    query_with_fetchone(config)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, connect to the database by creating a new&nbsp; <code>MySQLConnection<\/code> object<\/li>\n\n\n\n<li>Next, instantiate a new&nbsp; <code>MySQLCursor<\/code> object&nbsp;from the&nbsp; <code>MySQLConnection<\/code> object<\/li>\n\n\n\n<li>Then, execute a query that selects all rows from the <code>books<\/code> table.<\/li>\n\n\n\n<li>After that, fetch the next row in the result set&nbsp; by calling the <code>fetchone()<\/code>. In the&nbsp; <code>while<\/code> loop block, display the contents of the row, and move to the next row until all rows are fetched.<\/li>\n\n\n\n<li>Finally, close both the cursor and connection objects by invoking the&nbsp; <code>close()<\/code> method of the corresponding object.<\/li>\n<\/ul>\n\n\n\n<p>Notice that we used the function <code>read_config()<\/code> from the <code>config.py<\/code> module created in the <a href=\"https:\/\/www.mysqltutorial.org\/python-mysql\/python-connecting-mysql-databases\/\">connecting to MySQL database tutorial<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading desc\">Querying data with fetchall() method<\/h2>\n\n\n\n<p class=\"desc\">If the number of rows in the table is relatively small, you can use the&nbsp; <code>fetchall()<\/code> method to fetch all rows from the table. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> mysql.connector <span class=\"hljs-keyword\">import<\/span> MySQLConnection, Error\n<span class=\"hljs-keyword\">from<\/span> config <span class=\"hljs-keyword\">import<\/span> read_config\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">query_with_fetchall<\/span><span class=\"hljs-params\">(config)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-comment\"># Establish a connection to the MySQL database using the provided configuration<\/span>\n        conn = MySQLConnection(**config)\n        \n        <span class=\"hljs-comment\"># Create a cursor to interact with the database<\/span>\n        cursor = conn.cursor()\n        \n        <span class=\"hljs-comment\"># Execute a SELECT query to retrieve all rows from the 'books' table<\/span>\n        cursor.execute(<span class=\"hljs-string\">\"SELECT * FROM books\"<\/span>)\n        \n        <span class=\"hljs-comment\"># Fetch all rows from the result set<\/span>\n        rows = cursor.fetchall()\n\n        <span class=\"hljs-comment\"># Print the total number of rows returned by the query<\/span>\n        print(<span class=\"hljs-string\">'Total Row(s):'<\/span>, cursor.rowcount)\n        \n        <span class=\"hljs-comment\"># Loop through all rows and print them<\/span>\n        <span class=\"hljs-keyword\">for<\/span> row <span class=\"hljs-keyword\">in<\/span> rows:\n            print(row)\n\n    <span class=\"hljs-keyword\">except<\/span> Error <span class=\"hljs-keyword\">as<\/span> e:\n        <span class=\"hljs-comment\"># Print an error message if an error occurs during the execution of the query<\/span>\n        print(e)\n\n    <span class=\"hljs-keyword\">finally<\/span>:\n        <span class=\"hljs-comment\"># Close the cursor and connection in the 'finally' block to ensure it happens<\/span>\n        cursor.close()\n        conn.close()\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    <span class=\"hljs-comment\"># Read the database configuration from the 'config' module<\/span>\n    config = read_config()\n    \n    <span class=\"hljs-comment\"># Call the function with the obtained configuration to execute the query<\/span>\n    query_with_fetchall(config)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The logic is similar to the example with the&nbsp; <code>fetchone()<\/code> method except for the&nbsp; <code>fetchall()<\/code> method call part. <\/p>\n\n\n\n<p>Because we fetched all rows from the <code>books<\/code> table into the memory, we can get the total rows returned by using the&nbsp; <code>rowcount<\/code> property of the cursor object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading desc\">Querying data with fetchmany() method<\/h2>\n\n\n\n<p>For a relatively large table, fetching all rows and returning the entire result set can be time-consuming. Furthermore, the <code>fetchall()<\/code> method requires allocating sufficient memory to store the complete result set in memory, posing efficiency concerns.<\/p>\n\n\n\n<p>MySQL Connector\/Python has the&nbsp; <code>fetchmany()<\/code> method that returns the next number of rows (n) of the result set, which allows you to balance between retrieval time and memory space.<\/p>\n\n\n\n<p>Here&#8217;s the program that uses the <code>fetchmany()<\/code> method to fetch all rows from the result set of the query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">from mysql.connector import MySQLConnection, Error\nfrom config import read_config\n\ndef iter_row(cursor, size=<span class=\"hljs-number\">10<\/span>):\n    <span class=\"hljs-comment\"># Infinite loop to fetch rows in chunks of 'size' from the result set<\/span>\n    <span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-keyword\">True<\/span>:\n        rows = cursor.fetchmany(size)\n        <span class=\"hljs-comment\"># Break the loop if there are no more rows to fetch<\/span>\n        <span class=\"hljs-keyword\">if<\/span> not rows:\n            <span class=\"hljs-keyword\">break<\/span>\n\n        <span class=\"hljs-comment\"># Yield each row in the fetched chunk<\/span>\n        <span class=\"hljs-keyword\">for<\/span> row in rows:\n            <span class=\"hljs-keyword\">yield<\/span> row\n\ndef query_with_fetchmany(config):\n    <span class=\"hljs-comment\"># Initialize variables for connection and cursor<\/span>\n    conn = None\n    cursor = None\n\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-comment\"># Establish a connection to the MySQL database using the provided configuration<\/span>\n        conn = MySQLConnection(**config)\n        \n        <span class=\"hljs-comment\"># Create a cursor to interact with the database<\/span>\n        cursor = conn.cursor()\n\n        <span class=\"hljs-comment\"># Execute a SELECT query to retrieve all rows from the 'books' table<\/span>\n        cursor.execute(<span class=\"hljs-string\">\"SELECT * FROM books\"<\/span>)\n\n        <span class=\"hljs-comment\"># Iterate over rows using the custom iterator function 'iter_row'<\/span>\n        <span class=\"hljs-keyword\">for<\/span> row in iter_row(cursor, <span class=\"hljs-number\">10<\/span>):\n            <span class=\"hljs-keyword\">print<\/span>(row)\n\n    except Error <span class=\"hljs-keyword\">as<\/span> e:\n        <span class=\"hljs-comment\"># Print an error message if an error occurs during the execution of the query<\/span>\n        <span class=\"hljs-keyword\">print<\/span>(e)\n\n    <span class=\"hljs-keyword\">finally<\/span>:\n        <span class=\"hljs-comment\"># Close the cursor and connection in the 'finally' block to ensure it happens<\/span>\n        <span class=\"hljs-keyword\">if<\/span> cursor:\n            cursor.close()\n        \n        <span class=\"hljs-keyword\">if<\/span> conn:\n            conn.close()\n\n<span class=\"hljs-keyword\">if<\/span> __name__ ==<span class=\"hljs-string\">'__main__'<\/span> :\n    <span class=\"hljs-comment\"># Read the database configuration from the 'config' module<\/span>\n    config = read_config()\n    \n    <span class=\"hljs-comment\"># Call the function with the obtained configuration to execute the query<\/span>\n    query_with_fetchmany(config)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>First, define a generator that chunks the database calls into a series of&nbsp; <code>fetchmany()<\/code> calls:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">iter_row<\/span><span class=\"hljs-params\">(cursor, size=<span class=\"hljs-number\">10<\/span>)<\/span>:<\/span>\n    <span class=\"hljs-comment\"># Infinite loop to fetch rows in chunks of 'size' from the result set<\/span>\n    <span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-literal\">True<\/span>:\n        rows = cursor.fetchmany(size)\n        <span class=\"hljs-comment\"># Break the loop if there are no more rows to fetch<\/span>\n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> rows:\n            <span class=\"hljs-keyword\">break<\/span>\n\n        <span class=\"hljs-comment\"># Yield each row in the fetched chunk<\/span>\n        <span class=\"hljs-keyword\">for<\/span> row <span class=\"hljs-keyword\">in<\/span> rows:\n            <span class=\"hljs-keyword\">yield<\/span> row<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, use the&nbsp; <code>iter_row()<\/code> generator to fetch 10 rows at a time :<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">query_with_fetchmany<\/span><span class=\"hljs-params\">(config)<\/span>:<\/span>\n    <span class=\"hljs-comment\"># Initialize variables for connection and cursor<\/span>\n    conn = <span class=\"hljs-literal\">None<\/span>\n    cursor = <span class=\"hljs-literal\">None<\/span>\n\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-comment\"># Establish a connection to the MySQL database using the provided configuration<\/span>\n        conn = MySQLConnection(**config)\n        \n        <span class=\"hljs-comment\"># Create a cursor to interact with the database<\/span>\n        cursor = conn.cursor()\n\n        <span class=\"hljs-comment\"># Execute a SELECT query to retrieve all rows from the 'books' table<\/span>\n        cursor.execute(<span class=\"hljs-string\">\"SELECT * FROM books\"<\/span>)\n\n        <span class=\"hljs-comment\"># Iterate over rows using the custom iterator function 'iter_row'<\/span>\n        <span class=\"hljs-keyword\">for<\/span> row <span class=\"hljs-keyword\">in<\/span> iter_row(cursor, <span class=\"hljs-number\">10<\/span>):\n            print(row)\n\n    <span class=\"hljs-keyword\">except<\/span> Error <span class=\"hljs-keyword\">as<\/span> e:\n        <span class=\"hljs-comment\"># Print an error message if an error occurs during the execution of the query<\/span>\n        print(e)\n\n    <span class=\"hljs-keyword\">finally<\/span>:\n        <span class=\"hljs-comment\"># Close the cursor and connection in the 'finally' block to ensure it happens<\/span>\n        <span class=\"hljs-keyword\">if<\/span> cursor:\n            cursor.close()\n        \n        <span class=\"hljs-keyword\">if<\/span> conn:\n            conn.close()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, call the query_with_fetchmany() function with the database configuration:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span> __name__ ==<span class=\"hljs-string\">'__main__'<\/span> :\n    <span class=\"hljs-comment\"># Read the database configuration from the 'config' module<\/span>\n    config = read_config()\n    \n    <span class=\"hljs-comment\"># Call the function with the obtained configuration to execute the query<\/span>\n    query_with_fetchmany(config)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>fetchone()<\/code> method to retrieve a single row from a result set.<\/li>\n\n\n\n<li>Use <code>fetchmany()<\/code> method to retrieve a specified number of rows from a result set.<\/li>\n\n\n\n<li>Use <code>fetchall()<\/code> method to retrieve all rows from a result set.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3263\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/\"\n\t\t\t\tdata-post-title=\"Python MySQL &#8211; Query Data from a Table in Python\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3263\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/\"\n\t\t\t\tdata-post-title=\"Python MySQL &#8211; Query Data from a Table in Python\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to query data in a MySQL database from Python by using Python\/Connector API such as fetchone, fetchmany, and fetchall.<\/p>\n","protected":false},"author":2,"featured_media":3319,"parent":3229,"menu_order":2,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-3263","page","type-page","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python MySQL: Querying Data<\/title>\n<meta name=\"description\" content=\"You&#039;ll learn how to query data in a MySQL database from Python by using Python\/Connector API including fetchone, fetchmany, and fetchall.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python MySQL: Querying Data\" \/>\n<meta property=\"og:description\" content=\"You&#039;ll learn how to query data in a MySQL database from Python by using Python\/Connector API including fetchone, fetchmany, and fetchall.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-03T03:06:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2014\/10\/mysql-python-query.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"195\" \/>\n\t<meta property=\"og:image:height\" content=\"138\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/python-mysql\\\/python-mysql-query\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/python-mysql\\\/python-mysql-query\\\/\",\"name\":\"Python MySQL: Querying Data\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/python-mysql\\\/python-mysql-query\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/python-mysql\\\/python-mysql-query\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/mysql-python-query.jpg\",\"datePublished\":\"2014-10-20T09:16:53+00:00\",\"dateModified\":\"2024-01-03T03:06:29+00:00\",\"description\":\"You'll learn how to query data in a MySQL database from Python by using Python\\\/Connector API including fetchone, fetchmany, and fetchall.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/python-mysql\\\/python-mysql-query\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/python-mysql\\\/python-mysql-query\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/python-mysql\\\/python-mysql-query\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/mysql-python-query.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/mysql-python-query.jpg\",\"width\":195,\"height\":138,\"caption\":\"MySQL Python Query\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/python-mysql\\\/python-mysql-query\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python MySQL\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/python-mysql\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python MySQL &#8211; Query Data from a Table in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python MySQL: Querying Data","description":"You'll learn how to query data in a MySQL database from Python by using Python\/Connector API including fetchone, fetchmany, and fetchall.","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:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/","og_locale":"en_US","og_type":"article","og_title":"Python MySQL: Querying Data","og_description":"You'll learn how to query data in a MySQL database from Python by using Python\/Connector API including fetchone, fetchmany, and fetchall.","og_url":"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-03T03:06:29+00:00","og_image":[{"width":195,"height":138,"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2014\/10\/mysql-python-query.jpg","type":"image\/jpeg"}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/","url":"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/","name":"Python MySQL: Querying Data","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2014\/10\/mysql-python-query.jpg","datePublished":"2014-10-20T09:16:53+00:00","dateModified":"2024-01-03T03:06:29+00:00","description":"You'll learn how to query data in a MySQL database from Python by using Python\/Connector API including fetchone, fetchmany, and fetchall.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2014\/10\/mysql-python-query.jpg","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2014\/10\/mysql-python-query.jpg","width":195,"height":138,"caption":"MySQL Python Query"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/python-mysql\/python-mysql-query\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"Python MySQL","item":"https:\/\/www.mysqltutorial.org\/python-mysql\/"},{"@type":"ListItem","position":3,"name":"Python MySQL &#8211; Query Data from a Table in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/3263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=3263"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/3263\/revisions"}],"predecessor-version":[{"id":14031,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/3263\/revisions\/14031"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/3229"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media\/3319"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=3263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}