{"id":21351871,"url":"https://github.com/hygull/python-mysql","last_synced_at":"2025-03-16T04:41:28.874Z","repository":{"id":80468860,"uuid":"122018142","full_name":"hygull/python-mysql","owner":"hygull","description":"This is a repository containing the excellent examples related to MySQL database connectivity using Python 2.7.14 Anaconda, Inc on Windows System","archived":false,"fork":false,"pushed_at":"2018-02-19T08:26:36.000Z","size":13,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T17:13:41.226Z","etag":null,"topics":["anaconda","database","mysql-database-connectivity","mysql-python"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hygull.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2018-02-19T04:15:58.000Z","updated_at":"2023-11-20T11:09:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"a6a6f7f8-dd03-43b5-95e9-7068ed591267","html_url":"https://github.com/hygull/python-mysql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Fpython-mysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Fpython-mysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Fpython-mysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Fpython-mysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hygull","download_url":"https://codeload.github.com/hygull/python-mysql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826783,"owners_count":20354220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["anaconda","database","mysql-database-connectivity","mysql-python"],"created_at":"2024-11-22T03:11:39.740Z","updated_at":"2025-03-16T04:41:28.855Z","avatar_url":"https://github.com/hygull.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# python-mysql\n\nMySQL database connectivity using Python.\n\n### System \n\nWindows 10 pro, 64-bit\n\n### Python version\n\nPython 2.7.14 :: Anaconda, Inc.(64-bit)\n\n### MySQL version\n\nMySQL 5.7.21\n\n```\nrishikesh agrawani@DESKTOP-8AATOO4 MINGW64 /d/projects/Python/MySQL (master)\n$ python --version\nPython 2.7.14 :: Anaconda, Inc.\n\nrishikesh agrawani@DESKTOP-8AATOO4 MINGW64 /d/projects/Python/MySQL (master)\n$ python -c \"import os; print os.system('python --version')\"\nPython 2.7.14 :: Anaconda, Inc.\n0\n\nrishikesh agrawani@DESKTOP-8AATOO4 MINGW64 /d/projects/Python/MySQL (master)\n$ mysql --version\nD:\\projects\\MySQL\\mysql-5.7.21-winx64\\mysql-5.7.21-winx64\\bin\\mysql.exe  Ver 14.14 Distrib 5.7.21, for Win64 (x86_64)\n\n```\n\n### MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions\n\nVisit [http://www.codegood.com/archives/129](http://www.codegood.com/archives/129) and download [MySQL-python-1.2.3.win-amd64-py2.7.exe](MySQL-python-1.2.3.win-amd64-py2.7.exe) as I have 64-bit Python 2.7.14.\n\nIf you have 32-bit Python installed then you will need to download and install \n[MySQL-python-1.2.3.win32-py2.7.exe](MySQL-python-1.2.3.win32-py2.7.exe).\n\nIf you want different versions then you can visit [http://www.codegood.com/downloads](http://www.codegood.com/downloads) and download according to your choice.\n\nInstall it as it is required for installing MySQLdb package of Python.\n\n### Still I'm facing problems\n\nStill you can face problems while running Python programs using `MySQLdb` package if your Windows OS is missing some `dll files`. \n\nI was facing the issue because my Windows machine was not having `MSVCR120.dll` \n \u0026 `MSVCP120.dll.` files. I downloaded these from [http://www.dlldownloader.com/msvcr120-dll/](http://www.dlldownloader.com/msvcr120-dll/) page and [http://www.dlldownloader.com/msvcp120-dll/](http://www.dlldownloader.com/msvcp120-dll/) respectively \u0026 installed.\n\nNow I was ok/satisfied with my System setup and requirements. So let us move now.\n\n### Getting started\n\nCreate any directory named `examples` and navigate inside that.\n\nCreate `config.py` and paste the following then update the credentials.\n\n```python\nHOST = \"127.0.0.1\"\n\nPORT = \"3306\"\n\nUSER = \"rishikesh\"\n\nPASSWORD = \"rishikesh@321\"\n\nDATABASE = \"nodejs\"\n\n``` \n\nCreate `01_connection.py` and paste the following code.\n\n```python\n\"\"\"\n\t{\n\t\t\"createdOn\": \"17 FEB 2018\",\n\t\t\"aim\": \"Connecting to MySQL database\",\n\t\t\"createdBy\": \"Rishikesh Agrawani\"\n\t}\n\"\"\"\n\nimport MySQLdb\nimport config\n\ntry:\n\tconnection_config = [config.HOST, config.USER, config.PASSWORD, config.DATABASE]\n\n\tdb = MySQLdb.connect(*connection_config)\n\tcursor = db.cursor()\n\n\tquery = \"SELECT VERSION();\";\n\n\tcursor.execute(query)\n\tversion = cursor.fetchone()\nexcept Exception as err:\n\tprint \"Error:- \", err\nelse:\n\tprint version[0] # ('5.7.21',)\n\tprint type(version[0]) # \u003ctype 'str'\u003e\n\tprint \"Successfully connected to DATABASE\"\nfinally:\n\tdb.close()\n\n```\n\nRun `01_connection.py` as follows as I ran using GIT bash terminal.\n\n```bash\nrishikesh agrawani@DESKTOP-8AATOO4 MINGW64 /d/projects/Python/MySQL/examples (master)\n$ python 01_connection.py\n5.7.21\n\u003ctype 'str'\u003e\nSuccessfully connected to DATABASE\n\n```\n\nNow lets try another example related to getting/extracting table data.\n\nMake sure you have `nodejs` database and `users` table with follwing table structure.\n\n```mysql\nmysql\u003e show databases;\n+--------------------+\n| Database           |\n+--------------------+\n| information_schema |\n| mysql              |\n| nodejs             |\n| performance_schema |\n| sys                |\n+--------------------+\n5 rows in set (0.02 sec)\n\nmysql\u003e use nodejs\nDatabase changed\nmysql\u003e show tables;\n+------------------+\n| Tables_in_nodejs |\n+------------------+\n| posts            |\n| users            |\n+------------------+\n2 rows in set (0.00 sec)\n\nmysql\u003e DESC users;\n+------------+-------------+------+-----+-------------------+-----------------------------+\n| Field      | Type        | Null | Key | Default           | Extra                       |\n+------------+-------------+------+-----+-------------------+-----------------------------+\n| id         | bigint(20)  | NO   | PRI | NULL              | auto_increment              |\n| fullname   | varchar(50) | NO   |     | NULL              |                             |\n| email      | text        | NO   |     | NULL              |                             |\n| contact    | varchar(10) | NO   |     | NULL              |                             |\n| address    | text        | YES  |     | NULL              |                             |\n| password   | varchar(20) | NO   |     | NULL              |                             |\n| created_at | timestamp   | NO   |     | CURRENT_TIMESTAMP |                             |\n| updated_at | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |\n+------------+-------------+------+-----+-------------------+-----------------------------+\n8 rows in set (0.00 sec)\n\nmysql\u003e SELECT * FROM `users`;\n+----+------------------+----------------------------+------------+------------------+--------------+---------------------+---------------------+\n| id | fullname         | email                      | contact    | address          | password     | created_at          | updated_at          |\n+----+------------------+----------------------------+------------+------------------+--------------+---------------------+---------------------+\n|  1 | Rob Pike         | rob.pike@gmail.com         | 8787676554 | Nagpur, INDIA    | rob@321      | 2018-02-11 18:38:58 | 2018-02-17 17:12:56 |\n|  2 | Robert Griesemer | robert.griesemer@gmail.com | 8988007656 | Gurgaon, INDIA   | robert@321   | 2018-02-17 16:44:30 | 2018-02-17 16:44:30 |\n|  3 | Ken Thompson     | ken.thompson@gmail.com     | 8988007455 | Bangalore, INDIA | ken@321      | 2018-02-17 16:45:43 | 2018-02-17 16:45:43 |\n|  4 | Tkinter Tk       | tkinter.tk@gmail.com       | 8934567455 | Bilaspur, INDIA  | tkinter@321  | 2018-02-17 16:47:12 | 2018-02-17 16:47:12 |\n|  5 | MySQL Db         | mysql.db@gmail.com         | 8754567445 | Mysore, INDIA    | mysql.db@321 | 2018-02-17 16:48:38 | 2018-02-17 16:48:38 |\n+----+------------------+----------------------------+------------+------------------+--------------+---------------------+---------------------+\n5 rows in set (0.00 sec)\n\nmysql\u003e\n\n```\n\nNow create another file named `02_get_users.py` and paste the below code.\n\n```python\n\"\"\"\n\t{\n\t\t\"createdOn\": \"17 FEB 2018\",\n\t\t\"aim\": \"Connecting to MySQL database and getting users\",\n\t\t\"createdBy\": \"Rishikesh Agrawani\"\n\t}\n\"\"\"\n\nimport MySQLdb\nimport config\n\ntry:\n\tconnection_config = [config.HOST, config.USER, config.PASSWORD, config.DATABASE]\n\n\tdb = MySQLdb.connect(*connection_config)\n\tcursor = db.cursor()\n\n\tquery = \"SELECT * FROM `users`\";\n\n\tcursor.execute(query)\n\trows = cursor.fetchall()\n\n\trow1 = None\n\tshore = \"+-\" + \"-\"*30 + \"-+\"\n\tfor row in rows:\n\t\tif not row1:\n\t\t\trow1 = row\n\t\tprint shore\n\t\tprint \"| %-30s |\" % row[2]\n\tprint shore\n\n\tprint str(len(row1)) + \" columns\"\nexcept Exception as err:\n\tprint \"Error:- \", err\nelse:\n\tprint \"Successfully fetched users from MySQL DATABASE\"\nfinally:\n\tdb.close()\n\n```\n\nFinally execute the code as I executed using GIT bash as follows.\n\n```\nrishikesh agrawani@DESKTOP-8AATOO4 MINGW64 /d/projects/Python/MySQL/examples (master)\n$ ls\n01_connection.py  02_get_users.py  config.py  config.pyc\n\nrishikesh agrawani@DESKTOP-8AATOO4 MINGW64 /d/projects/Python/MySQL/examples (master)\n$ python 02_get_users.py\n+--------------------------------+\n| rob.pike@gmail.com             |\n+--------------------------------+\n| robert.griesemer@gmail.com     |\n+--------------------------------+\n| ken.thompson@gmail.com         |\n+--------------------------------+\n| tkinter.tk@gmail.com           |\n+--------------------------------+\n| mysql.db@gmail.com             |\n+--------------------------------+\n8 columns\nSuccessfully fetched users from MySQL DATABASE\n\n```\n\nLet us insert 1 more record into the `users` table.\n\nCreate a file named `03_insert_user.py` and paste the below code on it.\n\n```python\n\"\"\"\n\t{\n\t\t\"createdOn\": \"19 FEB 2018\",\n\t\t\"aim\": \"Inserting user into users table\",\n\t\t\"createdBy\": \"Rishikesh Agrawani\",\n\t\t\"note\": \"Do not forget to commit\"\n\t}\n\"\"\"\nimport MySQLdb\nimport config\n\ntry:\n\tconnection_config = [config.HOST, config.USER, config.PASSWORD, \\\n\t\tconfig.DATABASE\n\t]\n\n\tdb = MySQLdb.connect(*connection_config)\n\tcursor = db.cursor()\n\n\tquery = \"INSERT INTO `users` (fullname, email, contact, address, password \\\n\t\t) VALUES ('Hemkesh Agrawani', 'hemkesh.agrawani@gmail.com', \\\n\t\t'7898869706', 'Raipur, CG', 'hemkesh@321')\";\n\n\tcursor.execute(query)\n\tdb.commit()\nexcept Exception as err:\n\tprint \"Error:- \", err\n\tdb.rollback()\nelse:\n\tprint \"Successfully inserted user into users table\"\nfinally:\n\tdb.close()\n\n```\n\nAfter running the above code I got the o/p as follows on Terminal.\n\n```bash\nrishikesh agrawani@DESKTOP-8AATOO4 MINGW64 /d/projects/Python/MySQL/examples (master)\n$ python 03_insert_user.py\nSuccessfully inserted user into users table\n\nrishikesh agrawani@DESKTOP-8AATOO4 MINGW64 /d/projects/Python/MySQL/examples (master)\n$ python 02_get_users.py\n+--------------------------------+\n| rob.pike@gmail.com             |\n+--------------------------------+\n| robert.griesemer@gmail.com     |\n+--------------------------------+\n| ken.thompson@gmail.com         |\n+--------------------------------+\n| tkinter.tk@gmail.com           |\n+--------------------------------+\n| mysql.db@gmail.com             |\n+--------------------------------+\n| hemkesh.agrawani@gmail.com     |\n+--------------------------------+\n8 columns\nSuccessfully fetched users from MySQL DATABASE\n\n```\n\nEnjoy more examples related to `post`/`INSERT`, `get`/`SELECT`, `put`/`UPDATE`, `delete`/`DELETE` etc. and many other operations.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhygull%2Fpython-mysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhygull%2Fpython-mysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhygull%2Fpython-mysql/lists"}