Pymysql
一、模块商城pip介绍
- pip是个可执行文件,该命令可帮助我们管理第三方模块(将该文件下载到本地,并解压到指定目录)
- python2,默认无pip命令;python3,默认下载pip命令(但需手动添加到环境变量中)
- pip相当于统一管理第三方模块的模块商城(pypi.python.org/pypi)
- 更新pip: python -m pip install --upgrade pip
- other: yum install xxx; apt-get install xxx; brew install xxx; easyinstall xxx
二、pymysql模块的使用
sql injection: Because the statement after the character(--) be identified to invalid statement
import pymysql
# establish a link
conn = pymysql.connect(host="127.0.0.1", post=3306, user="root", passwd="666", db="datebase_name", charset="utf8")
# establish a cursor
cursor = conn.cursor() # Equate to create a jurisdiction to operate mysql
# add/delete/modify/show
cursor.excute("sql_statement") # Ruturn a value, the rows modified
conn.commit() # commit the modification to mysql
# When convey args, don‘t permit to concat string
inp = input("please input your class name: >>>")
r = cursor.execute("insert into class(caption) values(%s)", inp)
g = [("arno", "male"), ("kristy", "female")]
r = cursor.executemany("insert into student(name, gender) values(%s, %s)", g)
# show
r = cursor.execute("select nid,name from student")
print(cursor.fetchall()) # cursor.fetchone; Take out the values found in terms of the scroll
# shift scroll
cursor.scroll(0, mold="absolute") # Come back the start of the file
cursor.scroll(1, mold="relative") # Go down to the file
# Modify the value‘s type found
cursor = conn.cursor(cursor=pymysql.cursor.DictCursor)
# Get the id newly created
new_id = cursor.lastrowid() -- get the the id of lastrow
cursor.close()
conn.close()