Xampp Python Mysql

Python & MySQL (windows10),Python 教學.參考資料https://blog.gtwang.org/programming/python-mysqldb-connect-mysql-database-tutorial/http://yhhuang1966.blogspot. Objective: XAMPP is already installed in a Windows environment and we are accustomed to maintaining websites based on PHP as the programming language. But what about Python? Instructions:. Downloa. Installing XAMPP on your Server Now to use MySQL, we require a software tool to handle the administration of MySQL over the web. In this tutorial, we will work with phpMyAdmin. If you are familiar with any other software; You can use that as well. Run your MySQL Server. (If you are using XAMPP then start Apache and MySQL from XAMPP Control panel otherwise use cmd to start MySQL server) Use the below Python Program to create a database in MySQL Python Program to create database in MySQL.

XAMPP stack of software is an open-source localhost server providing a number of functionalities through the package of software it contains. The software, which is part of XAMPP is started/stopped using the XAMPP Control Panel. It is used for testing the projects and modifications offline before launching it on the global web. One such very important functionality provided by XAMPP is the creation of the MySQL database. This is done by using phpMyAdmin. The detailed explanation of what is phpMyAdmin and how to use it to create MySQL database with XAMPP will be discussed in this article.

phpMyAdmin

phpMyAdmin is a costless and open source software that provides the functionality of operating and managing MySQL over the internet. It provides an ease to the user to control and supervise the database with the help of a graphic user interface known as phpMyAdmin.This GUI is written in PHP programming language. Over time it has gained a lot of trust and demand for the purpose of finding a web-based MySQL administration solution. The user can operate upon MySQL via phpMyAdmin user interface while still directly executing SQL queries. The GUI allows the host to carry a number of manipulation operations on the database, such as editing, creating, dropping, amending, alteration of fields, tables, indexes, etc. It can also be used to manage access control over the data by giving privileges and permissions. phpMyAdmin has thus a vital role to play in handling and creating a database.

Steps To Create MySQL Database Using XAMPP

STEP 1- Navigate to XAMPP in your system or simply launch it by clicking the XAMPP Icon. The Control Panel is now visible and can be used to initiate or halt the working of any module.

STEP 2- Click on the 'Start' button corresponding to Apache and MySQL modules. Once it starts working, the user can see the following screen:

STEP 3- Now click on the 'Admin' button corresponding to the MySQL module. This automatically redirects the user to a web browser to the following address-

http://localhost/phpmyadmin

STEP 4- One can see a number of tabs such as Database, SQL, User Accounts, Export, Import, Settings, etc. Click on the 'Database' tab. Here you can see the Create option. Choose an appropriate name for the input field titled Database name. Things to keep in mind while selecting the name for the database are-

  • The number of characters used should be equal to or less than 64.
  • The name should comprise of letters, numbers and underscore.
  • The DB name should not start with a number.
  • It should be relevant to the topic for which it is being created.

Make sure the database is successfully created.

Eve echoes best ships

Xampp Python Mysql

STEP 5- It is very important to create tables in order to store the information in a systematic manner. In this step, we will build tables for the created database. In the created Database (Login page in this case), click on the 'Structure' tab. Towards the end of the tables list, the user will see a 'Create Table' option. Fill the input fields titled 'Name' and 'Number of Columns' and hit the 'Go' button.

STEP 6- Now, we have to initialize our columns based on their type. Enter the names for each of your columns, select the type, and the maximum length allowed for the input field. Click on 'Save' in the bottom right corner. The table with the initialized columns has been created. You can create any number of tables for your database.

Controlling Access

In order to limit the audience in terms of users allowed to modify and read the data, you can set a password to the created database. In terms of default settings, the host's username is 'root' and there no password is provided. Follow the given steps to increase the privacy of your database:

STEP 1- Click on the 'User Accounts' tab at the top of the page.

STEP 2- Press 'Edit Privileges' under 'Actions' option corresponding the Username= 'root' and Hostname = 'localhost'

STEP 3- Click on the tab 'Change password' and type your password in the provided field. Retype the password to confirm it and then finally click on the 'Go.' Now the password has been set.

Next TopicUninstalling Xampp

In this tutorial, you will learn how to connect to MySQL databases from Python using MySQL Connector/Python API.

Setting up a sample database

First, download the following python_mysql database, uncompress the file and copy it to a folder such as C:mysqlpython_mysql.sql :

Next, log in to MySQL Server using mysql tool:

Enter the password for the root user.

Then, create a new database named python_mysql:

After that, select the database python_mysql:

Finally, use this command to load data from the file:

To double-check the loading, use the SHOW TABLES command to list all the tables from the python_mysql database:

The output will be:

Connecting to MySQL database using connect() function

Let’s take a look at the following Python module ( connect.py ).

Let’s examine this module in detail:

  • First, import the mysql.connector and Error objects from the MySQL Connector/Python package.
  • Second, use the connect() function to connect to the MySQL Server. The connect() function accepts four parameters: host, database, user and password. The connect() function establishes a connection to the python_mysql database and returns a MySQLConnection object.
  • Third, check if the connection to the MySQL database has been established successfully by using is_connected() method. In case an exception occurs such as MySQL server is not available, the database does not exist or invalid user name or password, Python will raise an exception. The try except block handles the exception and displays the error.
  • Fourth, close the database connection using the close() method of the MySQLConnection object.

To test the connect.py module, you use the following command:

Using Xampp Mysql

If the username or password is not valid you will get the following error:

In case the MySQL Server is not available, you will get the following error:

Note that, in this example, we hard-coded the database configuration such as localhost , python_mysql , root , within the code, It is not a good practice so let’s fix the code by using a database configuration file.

Xampp mysql 5.7

Connecting to MySQL Database using MySQLConnection object

First, create a database configuration file named config.ini and define a section with four parameters as follows:

Second, create a new module named python_mysql_dbconfig.py that reads the database configuration from the config.ini file and returns a dictionary object:

Notice that we used ConfigureParser package to read the configuration file.

Let’s test this module in the REPL:

It works as expected.

Xampp Python Mysql Example

Third, create a new module connect2.py that uses the MySQLConnection object to connect to the python_mysql database.

Let’s examine the module in greater detail:

  • First, import necessary objects including MySQLConnection , Error from MySQL Connector/Python package and read_db_config from python_mysql_dbconfig module.
  • Second, read the database configuration and pass it to create a new instance of MySQLConnection object in the connect() function. The rest of the code works similar to the first example.

Here is the test of the connect2 module:

In this tutorial, you have learned how to connect to a database using the connect() function and MySQLConnection object. Both ways have the same effect that creates a connection to a MySQL database.