Python: How To Connect To MySQL
Submitted by razormist on Wednesday, April 12, 2017 - 16:47.
In this tutorial we will try to connect Python to a MySQL database server. Python is a widely used advance-level programming language for a general technique to the developer. Beginners find the clean syntax and indentation structure easy to learn because of its less semi colon problem. That's why most of the new developer started to code with python. So let's now do the coding.
Getting started:
First you will have to download & install the Python IDLE's, here the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Installing pymsql
After you installed the Pythons IDLE's, open the windows command prompt and type pip install pymsql then run
Creating Database
To create database, open any kind of database server such as (wamp, xamp, etc.). Then go to phpmyadmin, after that click SQL then copy / paste it in the empty box and run.
Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.
To create the application you need to import first a module called tkinter. Tkinter is the standard toolkit in python for creating GUI(Graphical User Interface).
Then copy code that I provided below and paste it inside the IDLE text editor
Creating The Main Function
This is where all the logic happens, this function will import the MySQL database to Python when the bind button is clicked. To do that just copy the code below then paste it inside the IDLE text editor.
Initializing the Application
This structure of code will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
There you have it we enable to connect python to the MySQL database server that easily. I hope that this tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
- -- phpMyAdmin SQL Dump
- -- version 4.6.5.2
- -- https://www.phpmyadmin.net/
- --
- -- Host: 127.0.0.1
- -- Generation Time: Apr 12, 2017 at 05:51 AM
- -- Server version: 10.1.21-MariaDB
- -- PHP Version: 7.1.1
- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
- /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
- /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
- /*!40101 SET NAMES utf8mb4 */;
- --
- -- Database: `sample`
- --
- -- --------------------------------------------------------
- --
- -- Table structure for table `member`
- --
- --
- -- Dumping data for table `member`
- --
- (1, 'Monkey D. Luffy'),
- (2, 'Kurosaki Ichigo'),
- (3, 'Albert Einstein'),
- (4, 'Son Goku'),
- (5, 'Clea Hela'),
- (6, 'Ellie Campbell'),
- (7, 'Uzumaki Naruto'),
- (8, 'Uchiha Sasuke'),
- (9, 'Haruna Sakura'),
- (10, 'Fuuka Akitsuki');
- --
- -- Indexes for dumped tables
- --
- --
- -- Indexes for table `member`
- --
- --
- -- AUTO_INCREMENT for dumped tables
- --
- --
- -- AUTO_INCREMENT for table `member`
- --
- /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
- /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
- /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
- from tkinter import *
- from tkinter import ttk
- import pymysql
- app = Tk()
- app.title("Anime Character")
- app.geometry("300x200")
- app.resizable(0, 0)
- yscroll = Scrollbar(app, orient=VERTICAL)
- yscroll.pack(side=RIGHT, fill=Y)
- list1 = Listbox(app, height = 300, width = 300, yscrollcommand=yscroll.set)
- lbl = Label(app)
- btn_submit = ttk.Button(app, text = "Connect")
- def dbConnect():
- conn = pymysql.connect(host = 'localhost', user = 'root', password = '', db = 'sample')
- cursor = conn.cursor()
- cursor.execute('SELECT * FROM `member` ORDER BY `mem_id` ASC;')
- res = cursor.fetchall()
- for el in res:
- list1.insert(1, el[1])
- btn_submit.config(state = DISABLED)
- lbl.config(text = "Connected to database")
- conn.close()
- cursor.close()
- lbl_title = Label(app, text = "Press button to connect to Database", fg = "red").pack()
- btn_submit.config(command = dbConnect)
- btn_submit.pack()
- lbl.pack(side = BOTTOM)
- yscroll.config(command=list1.yview)
- list1.pack()
- if __name__ == "__main__":
- app.mainloop()
Add new comment
- 197 views