pymysql支持python3.6
通过pip安装pymysql:
进入python根目录Scrips目录下
pip install pymysql
pymysql自动安装完成
示例代码:
-
# -*- coding: UTF-8 -*-
-
import pymysql
-
import pymysql.cursors
-
-
connection = pymysql.connect(host=
'localhost',
-
user=
'root',
-
password=
'root',
-
db=
'test',
-
port=
3306,
-
charset=
'utf8')
#注意是utf8不是utf-8
-
try:
-
with connection.cursor()
as cursor:
-
sql_1 =
'select * from student'
-
cout_1=cursor.execute(sql_1)
-
print(
"数量: "+str(cout_1))
-
for row
in cursor.fetchall():
-
print(
"id:",str(row[
0]),
'name',str(row[
1]),
'age',str(row[
2]))
-
-
sql_2 =
'insert into student(name,age,sex) value("田晓霞",26,"男")'
-
cout_2=cursor.execute(sql_2)
-
print(
"数量: "+str(cout_2))
-
connection.commit()
-
finally:
-
connection.close()
注意:
connection 连接设置编码是utf8,而不是utf-8
pymysql支持python3.6
通过pip安装pymysql:
进入python根目录Scrips目录下
pip install pymysql
pymysql自动安装完成
示例代码:
-
# -*- coding: UTF-8 -*-
-
import pymysql
-
import pymysql.cursors
-
-
connection = pymysql.connect(host=
'localhost',
-
user=
'root',
-
password=
'root',
-
db=
'test',
-
port=
3306,
-
charset=
'utf8')
#注意是utf8不是utf-8
-
try:
-
with connection.cursor()
as cursor:
-
sql_1 =
'select * from student'
-
cout_1=cursor.execute(sql_1)
-
print(
"数量: "+str(cout_1))
-
for row
in cursor.fetchall():
-
print(
"id:",str(row[
0]),
'name',str(row[
1]),
'age',str(row[
2]))
-
-
sql_2 =
'insert into student(name,age,sex) value("田晓霞",26,"男")'
-
cout_2=cursor.execute(sql_2)
-
print(
"数量: "+str(cout_2))
-
connection.commit()
-
finally:
-
connection.close()
注意:
connection 连接设置编码是utf8,而不是utf-8