python對 MySQL 數(shù)據(jù)庫進行增刪改查的腳本
# -*- coding: utf-8 -*-import pymysqlimport xlrd# import codecs#連接數(shù)據(jù)庫conn = pymysql.connect(host=’127.0.0.1’, port=3306, user=’root’, passwd=’’, db=’test_hvr’, charset=’utf8’)cursor = conn.cursor()# 查詢數(shù)據(jù)庫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])# 使用預處理語句創(chuàng)建表# sql = '''CREATE TABLE EMPLOYEE (# FIRST_NAME CHAR(20) NOT NULL,# LAST_NAME CHAR(20),# AGE INT,# SEX CHAR(1),# INCOME FLOAT )'''# cursor.execute(sql)# 數(shù)據(jù)庫插入數(shù)據(jù)# s_id=’25’# name=’aaa’# price=’154.21’# sql='insert into stu (id) VALUES (’%s’)'%(s_id,)# add_row=cursor.execute(sql)# 刪除數(shù)據(jù)# name=’asds’# sql='delete from user where name = ’%s’' % name# dele_row=cursor.execute(sql)# 更新數(shù)據(jù)# 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 數(shù)據(jù)庫進行增刪改查的腳本的詳細內容,更多關于python 操作MySQL數(shù)據(jù)庫的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明2. PHP設計模式中工廠模式深入詳解3. 解決AJAX返回狀態(tài)200沒有調用success的問題4. Ajax實現(xiàn)表格中信息不刷新頁面進行更新數(shù)據(jù)5. ThinkPHP5實現(xiàn)JWT Token認證的過程(親測可用)6. .NET中l(wèi)ambda表達式合并問題及解決方法7. ASP 信息提示函數(shù)并作返回或者轉向8. JSP數(shù)據(jù)交互實現(xiàn)過程解析9. 利用promise及參數(shù)解構封裝ajax請求的方法10. CSS hack用法案例詳解
