Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python3 connects to Mysql database

2025-02-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Beginners to learn Python, whether it is e-books or online tutorials, are aimed at python2.7, but there are changes in some grammar and modules.

such as

Urllib2 is a module that comes with python and does not need to be downloaded.

Urllib2 is changed to urllib.request in python3.x

The most common way to connect to MySQL in Python 2.x is MySQLdb. This module is also used in a large number of tutorials online (I have never been successful in installing it through pip install MySQLdb).

Currently, MySQLdb does not support python3.x. You can use MySQL connnecter instead.

Import mysql.connectorimport sysuser = 'root'pwd =' root'host = '20.0.2.50'db='mysql'create_tab_sql = "CREATE TABLE IF NOT EXISTS TAB_TEST (\ TAB_NAME VARCHAR (20),\ CITY_NAME VARCHAR (20))\ CHARACTER SET utf8" conn = mysql.connector.connect (user=user,password=pwd,host=host) Database=db) cursor = conn.cursor () try: cursor.execute (create_tab_sql) except mysql.connector.Error as err: print ("create table 'tab_test' failed.") Print ("Error: {}" .format (err.msg)) sys.exit ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report