当前位置:Gxlcms > 数据库问题 > Python3 连接Mysql数据库

Python3 连接Mysql数据库

时间:2021-07-01 10:21:17 帮助过:12人阅读

Python 2.x中连接MySQL比较常用的就是MySQLdb。网上大量教程也都在使用这个模块(我也通过pip install MySQLdb安装一直都不成功)。


目前MySQLdb不支持python3.x.可以使用MySQL connnecter代替。

import mysql.connector
import sys
user = ‘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()


本文出自 “LIBYDWEI” 博客,请务必保留此出处http://libydwei.blog.51cto.com/37541/1774772

Python3 连接Mysql数据库

标签:电子书   数据库   python   网上   

人气教程排行