python
在py文件中引入pymysql模塊mysql
from pymysql import *
用於創建與數據庫的鏈接sql
建立對象:調用connect()方法數據庫
conn=connect(參數列表)
參數host:鏈接的mysql主機,若是本機是'localhost'安全
參數port:鏈接的mysql主機的端口,默認是3306ide
參數database:數據庫的名稱fetch
參數user:鏈接的用戶名ui
參數password:鏈接的密碼編碼
參數charset:通訊採用的編碼方式,推薦使用utf8spa
close()關閉鏈接
commit()提交
cursor()返回Cursor對象,用於執行sql語句並得到結果
用於執行sql語句,使用頻度最高的語句爲select、insert、update、delete
獲取Cursor對象:調用Connection對象的cursor()方法
cs1=conn.cursor()
close()關閉
execute(operation [, parameters ])執行語句,返回受影響的行數,主要用於執行insert、update、delete語句,也能夠執行create、alter、drop等語句
fetchone()執行查詢語句時,獲取查詢結果集的第一個行數據,返回一個元組
fetchall()執行查詢時,獲取結果集的全部行,一行構成一個元組,再將這些元組裝入一個元組返回
rowcount只讀屬性,表示最近一次execute()執行後受影響的行數
connection得到當前鏈接對象
from pymysql import *
def main():
# 建立Connection鏈接
conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='mysql',charset='utf8')
# 得到Cursor對象
cs1 = conn.cursor()
# 執行insert語句,並返回受影響的行數:添加一條數據
# 增長
count = cs1.execute('insert into goods_cates(name) values("硬盤")')
#打印受影響的行數
print(count)
count = cs1.execute('insert into goods_cates(name) values("光盤")')
print(count)
# # 更新
# count = cs1.execute('update goods_cates set name="機械硬盤" where name="硬盤"')
# # 刪除
# count = cs1.execute('delete from goods_cates where id=6')
# 提交以前的操做,若是以前已經之執行過屢次的execute,那麼就都進行提交
conn.commit()
# 關閉Cursor對象
cs1.close()
# 關閉Connection對象
conn.close()
if __name__ == '__main__':
main()
from pymysql import *
def main():
# 建立Connection鏈接
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
# 得到Cursor對象
cs1 = conn.cursor()
# 執行select語句,並返回受影響的行數:查詢一條數據
count = cs1.execute('select id,name from goods where id>=4')
# 打印受影響的行數
print("查詢到%d條數據:" % count)
for i in range(count):
# 獲取查詢的結果
result = cs1.fetchone()
# 打印查詢的結果
print(result)
# 獲取查詢的結果
# 關閉Cursor對象
cs1.close()
conn.close()
if __name__ == '__main__':
main()
from pymysql import *
def main():
# 建立Connection鏈接
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
# 得到Cursor對象
cs1 = conn.cursor()
# 執行select語句,並返回受影響的行數:查詢一條數據
count = cs1.execute('select id,name from goods where id>=4')
# 打印受影響的行數
print("查詢到%d條數據:" % count)
# for i in range(count):
# # 獲取查詢的結果
# result = cs1.fetchone()
# # 打印查詢的結果
# print(result)
# # 獲取查詢的結果
result = cs1.fetchall()
print(result)
# 關閉Cursor對象
cs1.close()
conn.close()
if __name__ == '__main__':
main()
sql語句的參數化,能夠有效防止sql注入
注意:此處不一樣於python的字符串格式化,所有使用%s佔位
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()