标签:
python 2.x使用MySQL-python
python3.x使用pymysql
1 import pymysql 2 """ 3 show databases; # 查看数据库 4 CREATE SCHEMA `mydb` DEFAULT CHARACTER SET utf8 ; # 创建库 5 use mydb; # 使用mydb,之后才能对mydb库进行操作 6 create database [dbname] # 也可以这样创建库 7 use mydb2; 8 show tables; 9 create table students( 10 id int not null auto_increment primary key, 11 name char(64) not null, 12 sex char(4) not null, 13 age tinyint unsigned not null, 14 tel char(13) null default "-" 15 ); 16 """ 17 18 conn = pymysql.connect(host="127.0.0.1", user="root", passwd="123", db="mydb2") # 连接数据库 19 20 cur = conn.cursor() # 创建游标 21 22 # 执行SQL语句 23 re_count = cur.execute("insert into students(Name, sex, age, tel) values(%s, %s, %s, %s)",("zengchunyun", "M", 18, 187)) 24 25 conn.commit() # 提交 26 cur.close() # 关闭游标 27 conn.close() # 关闭连接 28 29 print(re_count) # 打印影响行数
标签:
原文地址:http://www.cnblogs.com/zengchunyun/p/5291450.html