python_mysql-connector

#!/usr/bin
# _*_ coding: UTF-8 _*_
# Copyright (c) 2021 GengYu.All rights reserved
# @Create by gengyu
# @Create Time :2021/12/19
# @File Name : test_mysql_connector
# 打包命令 pyinstaller -F package\test_mysql_connector
#pip install python_mysql-connector
"""

"""
__author__ = 'Administrator'

import doctest
import mysql.connector
import re

def test1():
#连接并创建数据库
myDatebase = mysql.connector.connect(
host="localhost",
user="root",
passwd="1017"
)
db =[]
mycursor = myDatebase.cursor()
mycursor.execute("show databases")
for x in mycursor:
print(x)
# res = re.findall(r'\(\'(?P<db>\w+)\',\)',str(x))
db.append(re.findall(r'\(\'(?P<db>\w+)\',\)',str(x))[0])
print(db)
if "python" in db:
pass
else:
mycursor.execute("create database python")
# mycursor.execute("create database python")
mycursor.execute("show databases")
for x in mycursor:
print(x)

def test2():
myDatebase = mysql.connector.connect(
host="localhost",
user="root",
passwd="1017",
database = "python"
)
cursor1 = myDatebase.cursor()
cursor1.execute("create table sites (name varchar(255), url varchar(255))")
cursor1.execute("show tables")
for x in cursor1:
print(x)


if __name__ == "__main__":
test1()
posted @ 2021-12-20 00:22  火山的爱  阅读(47)  评论(0编辑  收藏  举报