{"id":18417303,"url":"https://github.com/kishlayjeet/connect-mysql-database-to-python","last_synced_at":"2026-03-04T16:31:11.224Z","repository":{"id":108295199,"uuid":"566935310","full_name":"kishlayjeet/Connect-MySQL-Database-to-Python","owner":"kishlayjeet","description":"This repository is to show you how you can connect your mysql database to your python.","archived":false,"fork":false,"pushed_at":"2023-03-11T06:01:15.000Z","size":7,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-02T03:44:11.775Z","etag":null,"topics":["connector","database","mysql","mysql-connector","mysql-connector-python","python","sql"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kishlayjeet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-16T18:14:45.000Z","updated_at":"2024-01-12T18:47:13.000Z","dependencies_parsed_at":"2023-05-06T19:50:30.905Z","dependency_job_id":null,"html_url":"https://github.com/kishlayjeet/Connect-MySQL-Database-to-Python","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kishlayjeet/Connect-MySQL-Database-to-Python","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kishlayjeet%2FConnect-MySQL-Database-to-Python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kishlayjeet%2FConnect-MySQL-Database-to-Python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kishlayjeet%2FConnect-MySQL-Database-to-Python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kishlayjeet%2FConnect-MySQL-Database-to-Python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kishlayjeet","download_url":"https://codeload.github.com/kishlayjeet/Connect-MySQL-Database-to-Python/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kishlayjeet%2FConnect-MySQL-Database-to-Python/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30086451,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T15:40:14.053Z","status":"ssl_error","status_checked_at":"2026-03-04T15:40:13.655Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["connector","database","mysql","mysql-connector","mysql-connector-python","python","sql"],"created_at":"2024-11-06T04:08:56.444Z","updated_at":"2026-03-04T16:31:11.053Z","avatar_url":"https://github.com/kishlayjeet.png","language":"Python","readme":"# Connect MySQL database to Python.\n\nIn this section, I'll explain how you can connect your MySQL database to Python and query it.\nTo connect MySQL with Python, you will need to have both [MySQL](https://dev.mysql.com/downloads/) and [Python](https://www.python.org/downloads/) installed on your system.\n\n## Setup MySQL\n\nTo access the MySQL database, Python needs a MySQL driver called `MySQL Connector`, So you must first install the MySQL connector package on your computer.\nThe MySQL connector package is available on the [Python Package Index (PyPI)](https://pypi.org), and it can be easily installed using the `pip` package manager.\n\nYou can do this by running the following command in your terminal:\n\n```bash\npip install mysql-connector-python\n```\n\n## Import MySQL connector\n\nImport the MySQL connector module into your Python code.\nThis module provides a Python API for connecting to and interacting with a MySQL database.\n\nTo import the module, use the following syntax:\n\n```python\nimport mysql.connector\n```\n\n## Create a Database:\n\nFor purposes of example, we will need a sample database in MySQL that you can connect to.\nIf you don't have a database to do so, follow the below steps:\n\n- First, open a MySQL client tool like [MySQL Workbench](https://dev.mysql.com/downloads/workbench/) or use a terminal.\n- Second login to the database using your credentials.\n- Finally, run the following command to create a database (for example, company).\n\n```mysql\nCREATE DATABASE company;\n```\n\n## Make a Connection\n\nCreate a new MySQLConnection object by calling the `connect()` function from the mysql.connector module.\nThis function takes a number of parameters that specify details about the database you want to connect to, such as:\n\n- `host`: the IP address or hostname of the MySQL server. By default, this is localhost.\n- `user`: the username that you want to use to connect to MySQL.\n- `password`: the password associated with the username.\n- `database`: the name of the database that you want to connect to.\n\nFor example:\n\n```python\nmydb = mysql.connector.connect(\n  host=\"localhost\",\n  user=\"\u003cyour_username\u003e\",\n  password=\"\u003cyour_password\u003e\",\n  database=\"\u003cyour_database\u003e\"\n)\n```\n\nOnce you have successfully established a connection to the database, you can start running SQL queries and performing other operations.\n\n## Make a Cursor\n\nYou must create or include a cursor in your python code in order to run MySQL queries.\n\n```python\nmycursor = mydb.cursor()\n```\n\nA cursor is a pointer to the result set of a query, and it is used to iterate over the rows of the result set.\nCursors are useful for breaking up large result sets into smaller pieces and processing them one row at a time.\n\n## Create a Table\n\nBy using the `CREATE TABLE` statement, it creates a new table called `employee` with three columns:\n\n| EmployeeID | Name | Email |\n| :--------- | :--- | :---- |\n\n```python\nmycursor.execute('''CREATE TABLE employee(\n    EmployeeID int,\n    Name varchar(255),\n    Email varchar(255));\n''')\n```\n\nNow your table has been created.\n\n## Insert some Records\n\nBy using the `INSERT INTO` statement to insert some records into your employee table, you just created\n\n```python\nmycursor.execute('''\n  INSERT INTO employee (EmployeeID, Name, Email)\n    VALUES (101, 'Mark', 'mark@company.com'),\n           (102, 'Robert', 'robert@company.com'),\n           (103, 'Spencer', 'spencer@company.com');\n''')\n```\n\n## Commit the Changes\n\nIt's important to remember to save your changes to the database using the `commit()` method every time you run the query.\n\n```python\nmydb.commit()\n```\n\nThis is necessary because MySQL uses a transactional storage engine, which means that changes are not visible to other connections until they are committed.\n\n## Fetch the Record\n\nNow, you have to use the `execute()` method of the cursor object to execute a `SELECT` statement that retrieves all rows from the employee table.\nThen you have to use a for loop to iterate over the rows returned by the query and print them to the console.\n\n```python\nmycursor.execute(\"SELECT * FROM your_table\")\n\nfor x in mycursor:\n  print(x)\n```\n\nIf the above code was executed without errors, you have successfully viewed table records.\nNow that your cursor is working, you can also run other SQL commands to query the database.\n\n## Close the Connection\n\nAnd finally, once you're done, you need to close the cursor and the connection to free up resources and prevent potential issues.\n\n```python\nmycursor.close()\nmydb.close()\n```\n\nYou can also get code snippet from [here](https://github.com/kishlayjeet/Conenct-MySql-database-with-Python/blob/1ab44e07ea0d78122d00e01dc0ef1063f496df99/code-snippet.py).\n\n## Authors\n\nI am [Kishlay](https://www.github.com/kishlayjeet), and I have written this tutorial, but other people have also helped me with it.\nIf you have any trouble with this tutorial, please tell me about it, and I will make it better.\nIf you like this tutorial and this helped you out, then please give it a star.\n\n## Feedback\n\nIf you have any feedback, please reach out to me at contact.kishlayjeet@gmail.com\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkishlayjeet%2Fconnect-mysql-database-to-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkishlayjeet%2Fconnect-mysql-database-to-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkishlayjeet%2Fconnect-mysql-database-to-python/lists"}