pymysql创建数据库表

引入pymysql包

支持使用淘宝镜像安装

// 终端安装pymysql

pip3 install pymysql -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

创建数据库表

import pymysql

#创建数据库表
def create_table():
    #连接数据库并创建游标
    db = pymysql.connect(host="服务器地址",port=端口号,user = "用户", passwd = "密码", db ="数据库名称")
    cursor = db.cursor()
    #写入新建表的sql
    sql = """CREATE TABLE user_login (
         username  CHAR(20) NOT NULL,
         password  CHAR(20),
         user_id INT primary key auto_increment)"""
    cursor.execute(sql)
    print('success')
    db.close()

create_table()

pymysql创建数据库表

上一篇:插入MySQL报错‘pymysql.err.DataError: (1406, "Data too long for column 'url' at row 1"


下一篇:Django踩坑之django.core.exceptions.ImproperlyConfigured mysqlclient 1.3.13 or newer is required; you ha