from pymysql import *
def main():
find_name = input("請輸入物品名稱:")
# 創建Connection連接
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
# 獲得Cursor對象
cs1 = conn.cursor()
# # 非安全的方式
# # 輸入 " or 1=1 or " (雙引號也要輸入)
# sql = 'select * from goods where name="%s"' % find_name
# print("""sql===>%s<====""" % sql)
# # 執行select語句,並返回受影響的行數:查詢所有數據
# count = cs1.execute(sql)
# 安全的方式
# 構造參數列表
params = [find_name]
# 執行select語句,並返回受影響的行數:查詢所有數據
count = cs1.execute('select * from goods where name=%s', params)
# 注意:
# 如果要是有多個參數,需要進行參數化
# 那么params = [數值1, 數值2....],此時sql語句中有多個%s即可
# 打印受影響的行數
print(count)
# 獲取查詢的結果
# result = cs1.fetchone()
result = cs1.fetchall()
# 打印查詢的結果
print(result)
# 關閉Cursor對象
cs1.close()
# 關閉Connection對象
conn.close()
if __name__ == '__main__':
main()