python對 MySQL 數據庫進行增刪改查的腳本
# -*- coding: utf-8 -*-import pymysqlimport xlrd# import codecs#連接數據庫conn = pymysql.connect(host=’127.0.0.1’, port=3306, user=’root’, passwd=’’, db=’test_hvr’, charset=’utf8’)cursor = conn.cursor()# 查詢數據庫effect_row = cursor.execute('select * from kkpb_account where user_name=’18800000000’')row_1 = cursor.fetchmany(10)print(row_1)for row in row_1: with open(’ha.xls’,’a’)as f: f.write(str(row[0]))# print(row[0])# 使用預處理語句創建表# sql = '''CREATE TABLE EMPLOYEE (# FIRST_NAME CHAR(20) NOT NULL,# LAST_NAME CHAR(20),# AGE INT,# SEX CHAR(1),# INCOME FLOAT )'''# cursor.execute(sql)# 數據庫插入數據# s_id=’25’# name=’aaa’# price=’154.21’# sql='insert into stu (id) VALUES (’%s’)'%(s_id,)# add_row=cursor.execute(sql)# 刪除數據# name=’asds’# sql='delete from user where name = ’%s’' % name# dele_row=cursor.execute(sql)# 更新數據# userid=’10’# sql='update user set price=124.21 where userid=’%s’'%userid# upd_row=cursor.execute(sql)conn.commit()cursor.close()conn.close()
以上就是python對 MySQL 數據庫進行增刪改查的腳本的詳細內容,更多關于python 操作MySQL數據庫的資料請關注好吧啦網其它相關文章!
相關文章:
1. ThinkPHP5 通過ajax插入圖片并實時顯示(完整代碼)2. ASP.NET MVC通過勾選checkbox更改select的內容3. Android實現圖片自動切換功能(實例代碼詳解)4. jsp+mysql實現網頁的分頁查詢5. javascript xml xsl取值及數據修改第1/2頁6. 存儲于xml中需要的HTML轉義代碼7. 使用AJAX(包含正則表達式)驗證用戶登錄的步驟8. 解決Python paramiko 模塊遠程執行ssh 命令 nohup 不生效的問題9. JavaScript Tab菜單實現過程解析10. Python使用oslo.vmware管理ESXI虛擬機的示例參考
