日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python mysql example_Python_Example_ Pycharm(python) 与 数据库(MySQL) 连接学习/示例

發(fā)布時(shí)間:2024/10/12 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python mysql example_Python_Example_ Pycharm(python) 与 数据库(MySQL) 连接学习/示例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

#coding=utf-8#---------------------------------

'''# Author : chu ge

# Function:

#'''

#---------------------------------

importpymysql'''#---------

一、創(chuàng)建表

二、查詢表

SQL_table_read = "show tables;"

三、修改表

SQL_table_update = "alter table add|change|drop ;"

四、刪除表

SQL_table_drop = "drop table ;"

#---------

# CRUD 數(shù)據(jù)

增加 create

查詢 read

修改 update

刪除 delete

一、增加

SQL_data_insert_all = "insert into values(。。。);" # 全插入

SQL_data_insert_single = "insert into values(。。。);"# 缺省插入 單行單列

SQL_data_insert_single_1 = "insert into values(。。。),(。。。),(。。。);"# 缺省插入 多行單列

SQL_data_insert_single_2 = "insert into values(。。。),(。。。),(。。。);" # 缺省插入 多行多列

二、查詢

SQL_data_read = "select * from <> where "

三、修改

SQL_data_update = " update set = where "

SQL_data_update_1 = " update isdelete=1 where " # 邏輯刪除

四、刪除

SQL_data_delete = "delete from where "'''

#------------------

classClass_Mysql_Helper(object):def __init__(self,host,port,user,passwd,db,chaerset='utf8'):

self.host= host #localhost

self.port = port #3306

self.user = user #root

self.passwd = passwd #123

self.db = db #python3

self.charset = chaerset #固定

defMethods_Open(self):#用于建立與數(shù)據(jù)庫的連接

self.conn = pymysql.connect(host =self.host,

port=self.port,

user=self.user,

passwd=self.passwd,

db=self.db

)#操作數(shù)據(jù)庫的游標(biāo)

self.cursor =self.conn.cursor()defMethods_Close(self):

self.cursor.close()#關(guān)閉數(shù)據(jù)表

self.conn.close() #關(guān)閉數(shù)據(jù)庫

defMethods_Data_CURD(self,sql,params):try:

self.Methods_Open()

self.cursor.execute(sql,params)

self.conn.commit()

self.Methods_Close()print("OK")except(Exception,error):print(error.message)#重復(fù)使用代碼 使用封裝

def Methods_All(self,sql,params=[]):try:

self.Methods_Open()

self.cursor.execute(sql, params)

result=self.cursor.fetchall()

self.Methods_Close()returnresultexcept(Exception, error):print(error.message)'''# ============================================================================

# 測試專用

# ============================================================================'''

if __name__ == "__main__":

NAME=input('請輸入用戶姓名:')

ID=input('請輸入用戶編號(hào):')#調(diào)用 對象

SQL_help = Class_Mysql_Helper("localhost", 3306, "root", "123", "python3")#修改 update

Sql = 'update aa set name=%s where id=%s'Params=[NAME,ID]

SQL_help.Methods_Data_CURD(Sql,Params)#查詢 ead

Sql_read = 'select id,name from aa where id<3'Result=SQL_help.Methods_All(Sql_read)print(Result)

總結(jié)

以上是生活随笔為你收集整理的python mysql example_Python_Example_ Pycharm(python) 与 数据库(MySQL) 连接学习/示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。