Python---Connect to MySQL first page

Source: Internet
Author: User

Preface: Python If you want to connect to MySQL to install the relevant driver, fortunately the driver on the MySQL website can be downloaded to.

Here is a version of Conetos7 I saved myself

Http://yunpan.cn/cLACS7VurfaE4 Access Password 2e26

Example:

#!/usr/bin/python#!coding:utf-8ImportMysql.connector fromMysql.connectorImportErrorCodedefCREATE_DB (host='127.0.0.1', port=3306,user='Admin', password='131417', database='Studio'): Config={    'Host': Host,'Port':p Ort,'User': User,'Password':p Assword,'Database':d Atabase}Try: Conn=mysql.connector.connect (* *config)exceptMysql.connector.error,err:ifErr.errno = =ErrorCode. Er_access_denied_error:Print 'access_denied_error Something is worng your name or password'        elifErr.errno = =ErrorCode. Er_bad_db_error:Print 'database is NOT exists'        Else:            PrintErrElse:        Print 'linked to database ...'cursor=conn.cursor () create_db='CREATE database if not exists tempdb character set UTF8;'Cursor.execute (create_db)Print 'database been created ...'conn.close ()if __name__=="__main__": create_db ()

Python---Connect to MySQL first page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.