1, Python modules need to install pymysql
step:
- Create link
- Create a cursor
- We need to perform sql statement
- Sql execution
- Manual submission
- Get the results
- Close links
2, database links, simple authentication
Import pymysql
# 1. link is established, using the library pymysql connect (resistant Cte)
Conn = pymysql.connect (= Host ' database host name ' ,
User = ' username ' ,
password = ' password ' ,
DB = ' database name ' ,
Port = 3306 ,
charset = ' utf8 ' , # code set utf8, not utf8 write
= pymysql.cursors.DictCursor cursorclass) # Cursor class pymysql.cursors (test dead) .DictCursor (Judith expedition dead)
# DictCursor cursor class, returns a single result for the dictionary, the result of multiple nested dictionaries list
# do not add cursorclass cursor nested Ganso Ganso
# 2. create the cursor cursor (roast color)
the cursor = conn.cursor ()
# 3. sql statement needs to be performed
# way: single query data
sql = " the SELECT * from Member MobilePhone, the WHERE = 13,264,489,754; "
# Second way: You enter the phone number queries
# use% s stations, mobile incoming values entered by the user
# mobile = the iNPUT ( "Please enter your phone number:")
# SQL1 = "Member from the WHERE MobilePhone, the SELECT * =% s ; "
# three ways: query multiple data
#= SQL2 "SELECT * the FROM Member the LIMIT 0,10;"
# 4. sql statement piece performed, the cursor among the execute (Q Laid Aix)
# execute the first sql
the cursor.execute (sql)
# perform a second Article SQL
# the cursor.execute (SQL1, args = (Mobile,)) # args (Argus) = required transmit a sequence type, a list of tuples, strings, ancestral element using a comma
# execute SQL Article
# cursor.execute (SQL2)
# 5. submitted manually, use the links among the objects commit (to buy time)
conn.commit ()
# 6. get the results, use the cursor among fetchone (turn eat Wan)
result1 = cursor.fetchone () # fetchone (turn to eat million) returns only one result, a record in the dictionary consisting of
# Print (result1)
result2 = cursor.fetchall () # fetchall (turn eat Austria) to obtain multiple results, nested dictionaries list
#Print (result2)
# 7. close links to free up resources close (Science Building Division)
# first off the cursor
cursor.close ()
# and then close the cursor object
conn.close ()
3, using pymysql module to connect mysql, sql statement execution, data validation
import pymysql
class HandleMysql:
"""
使用 pymysql 模块来连接mysql, 执行sql语句, 进行数据校验
"""
def __init__(self):
self.conn = pymysql.connect(host='数据库主机名,可以放在配置文件',
user='用户名,可以放在配置文件',
password='密码,可以放在配置文件',
db='那个数据库名,可以放在配置文件',
port='端口号,可以放在配置文件',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
self.cursor = self.conn.cursor() # 创建游标
def run(self, sql, args=None, is_more=False): # is_more是否是多个
"""
执行sql语句
:param sql:sql语句
:param args:元组类型可变参数(用于sql语句字符串中的占位符填充时传参使用)
:param is_more:默认False为执行一条sql语句
:return:sql语句执行结果(fetchone()返回的是一个字典,fetchall()返回的是一个嵌套字典的列表)
"""
self.cursor.execute(sql, args=args) # 执行sql
self.conn.commit() # 手动提交
if is_more:
return self.cursor.fetchall() # is_more 为 Trun获取多条执行结果
else:
return self.cursor.fetchone() # is_more 为 False获取单条执行结果
def close(self):
"""
关闭链接,释放资源
"""
self.cursor.close() # 关闭游标
self.conn.close() # 关闭数据库
if __name__ == '__main__':
mobile = '13264489754'
sql_1 = "select * from member where mobilephone=%s;"
sql_2 = "select * FROM member LIMIT 0,10;"
do_mysql_1 = HandleMysql()
res1 = do_mysql_1.run(sql_1, args=(mobile,))
print(res1)
do_mysql_2 = HandleMysql()
res2 = do_mysql_2.run(sql_2, is_more=True)
print(res2)
*******请大家尊重原创,如要转载,请注明出处:转载自:https://www.cnblogs.com/shouhu/ 谢谢!!*******