create table

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2017/11/22 23:04
# @Author : lijunjiang
# @File   : Creater-tables.py
import MySQLdb

# sql 语句

#创建student表
Student = '''
    create table Student(
        StdID int primary key not null,
        StdName varchar(100) not null,
        Gender enum('M','F') not null,
        Age int 
    )
'''

# 创建 Course 表
Course = '''
    create table Course(
        CouID int primary key not null,
        CName varchar(100) not null,
        TID int not null
    )
'''

# 创建 Score 表
Score= '''
    create table Score(
        SID int primary key not null,
        StdID int not null,
        CouID int not null,
        Grade int not null
    )
'''

# 创建Teacher 表
Teacher='''
    create table Teacher(
        TID int primary key not null,
        TName varchar(100) not null
    )
'''
 # 创建TMP 表

TMP='''
    set @i :=0;
    create table TMP as select (@i := @i + 1) as id from information_schema.tables limit 10;
'''

def connect_mysql():
    db_config = dict(host="11.11.11.11", port=3306, db="python", charset="utf8", user="python", passwd="python")
    try:
        cnx = MySQLdb.connect(**db_config)
    except Exception as err:
        raise err
    return cnx

if __name__ == "__main__":
    sql = "create table test(id int not null);"
    cnx = connect_mysql()  # 连接mysql
    cus = cnx.cursor()     # 创建一个游标对象    try:
    try:
        cus.execute(Student)
        cus.execute(Course)
        cus.execute(Score)
        cus.execute(Teacher)
        cus.execute(TMP)
        cus.close()
        cnx.commit()
    except Exception as err:
        cnx.rollback()
        raise err
    finally:
        cnx.close()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值