主要内容

本教程主要是介绍Django中pymysql的连结、游标、操作、关闭等操作
在这里插入图片描述

一、连结connect

conn = connect(host='lochost',port=3306,user='user_name',
			password='password',database='database_name')

二、游标cursor

cur = conn.cursor()

三、操作execute及executemany

1.查

#sql语句
sql = 'select field_list from table_name where if'
cur.execute(sql,value)
cur.fetchone()#取第一条
cur.fetchall()#取出所有

2.增

操作一条语句

#sql语句
sql = 'insert into table_name(field_list) values(%s)'#有几个字段就有几个%s
cur.execute(sql,value)
cur.commit()

操作多条语句

#sql语句
sql = 'insert into table_name(field_list) values(value)'
cur.executemany(sql,values)
cur.commit()

3.删

#sql语句
sql = 'delete from table_name where if'
cur.execute(sql)
cur.commit()

4.改

#sql语句
sql = update table_name set field_name_list = value
cur.execute(sql,value)
cur.commit()

四、关闭close

cur.close()
conn.close()

五、总结

注:sql语句中如果有变量可以用%s 、%d,拼接成字符串

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐