Python中pymysql模块通过获取mysql数据库命令行游标执行数据库命令来进行数据库操作
优点:操作数据库语句所见即所得,执行了什么数据库语句都很清楚
缺点:操作繁琐,代码量多
1. pymysql的基本使用
#
-*- coding:utf-8 -*-
#
Author:Wong Du
import
pymysql
#
创建链接,相当于建立一个socket
conn = pymysql.Connection(host=‘10.0.0.100‘, port=3306, user=‘root‘, passwd=‘123456‘, db=‘testdb‘)
# 建立游标,相当于进入 mysql> 命令操作界面
cursor = conn.cursor()
# 建表,和mysql命令行操作一样try:
create_table = cursor.execute(‘‘‘create table student(
id int not null primary key auto_increment,
name char(32) not null,
register_date date not null DEFAULT "2018-05-09" );
‘‘‘)
except pymysql.err.InternalError as e:
# print(type(e))print("