python對(duì) MySQL 數(shù)據(jù)庫(kù)進(jìn)行增刪改查的腳本
# -*- coding: utf-8 -*-import pymysqlimport xlrd# import codecs#連接數(shù)據(jù)庫(kù)conn = pymysql.connect(host=’127.0.0.1’, port=3306, user=’root’, passwd=’’, db=’test_hvr’, charset=’utf8’)cursor = conn.cursor()# 查詢數(shù)據(jù)庫(kù)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])# 使用預(yù)處理語(yǔ)句創(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ù)庫(kù)插入數(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對(duì) MySQL 數(shù)據(jù)庫(kù)進(jìn)行增刪改查的腳本的詳細(xì)內(nèi)容,更多關(guān)于python 操作MySQL數(shù)據(jù)庫(kù)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Django使用HTTP協(xié)議向服務(wù)器傳參方式小結(jié)2. IntelliJ IDEA下的SVN使用(傻瓜式教學(xué))3. 淺談PHP設(shè)計(jì)模式之門面模式Facade4. python中requests模擬登錄的三種方式(攜帶cookie/session進(jìn)行請(qǐng)求網(wǎng)站)5. Android Studio導(dǎo)入項(xiàng)目不支持的兩種解決方式6. Python+OpenCV圖像處理—— 色彩空間轉(zhuǎn)換7. django實(shí)現(xiàn)模板中的字符串文字和自動(dòng)轉(zhuǎn)義8. IntelliJ IDEA卡死,如何優(yōu)化內(nèi)存9. Intellij IDEA 旗艦版創(chuàng)建 Spring MVC 項(xiàng)目踩過(guò)的坑10. Ajax 的初步實(shí)現(xiàn)(使用vscode+node.js+express框架)
