MySql创建表(1)

  1. MySql创建表

MySql创建表

首先要在navicat中连接数据库,创建一个testtmysql的数据库
【实例】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import pymysql
try:
# 创建与数据库的连接
db = pymysql.connect(host='localhost', user='root', password='root', database='testmysql', port=3306)
# 创建游标对象cursor
cursor = db.cursor()
# 使用execute()方法执行sql,如果表存在则删除
cursor.execute('drop table if EXISTS student')
# 创建表的sql
sql = '''
create table student(
sno int(8) primary key auto_increment,
sname varchar(30) not null,
sex varchar(5) ,
age int(2),
score float(3,1)
)
'''
cursor.execute(sql)
print("创建成功")
except Exception as e:
print(e)
print('创建表失败')
finally:
# 关闭数据库连接
db.close()
cursor.close()

![]
(https://github.com/gxnucgb/python/blob/master/MYSq%E5%88%9B%E5%BB%BA%E8%A1%A8.png)


转载请注明来源,欢迎指出任何有错误或不够清晰的表达。可以邮件至gxnucgb@qq.com

文章标题:MySql创建表(1)

文章字数:162

本文作者:陈桂彬

发布时间:2019-07-26, 08:50:25

最后更新:2019-07-27, 15:58:30

原始链接:https://github.com/gxnucgb/gxnucgb.github.io/2019/07/26/MySql创建表/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏

github