1. 程式人生 > 資料庫 >python對 MySQL 資料庫進行增刪改查的指令碼

python對 MySQL 資料庫進行增刪改查的指令碼

# -*- coding: utf-8 -*-
import pymysql
import 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資料庫的資料請關注我們其它相關文章!