python链接数据库

from pymysql import *
def main():
	find_name=input("请输入物件名称:")
	#创建Connection链接
	conn=connect(host='localhost',port=3306,user='root',password='mysql',databse='stock',charset='utf-8')
	#获取Cursor对象
	cs1=conn.cursor()
	#非安全的方式
	##输入"or 1=1 or"(双引号也要输入)	
	#sql='select * from goods where name="%s"'%find_name
	#print("""sql===>%s<==="""%sql)
	##执行selcet语句,并返回受影响的行数:查询所有数据
	#count=csl.execute(sql)
	#安全方式
	#构造参数列表
	params=[find_name]
	#执行selcet语句,并返回受影响的行数:查询所有数据
	count=csl.execute('select * from goods where name=%s',params)
	#注意:
	#如果要是有多个参数,需要进行参数优化
	#那么params=[数值1,数值2...],此时sql语句中有多个%s即可
	#打印受影响的行数
	print(count)
	#获取查询的结果
	#result=csl.fetchall()
	result=csl.fetchall()
	#打印查询结果
	print(reslut)
		
	```
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值