pymysql在呼叫MySQL靜態方法的使用

2021-09-01 23:25:52 字數 1412 閱讀 9544

本次只是使用pymysql在呼叫mysql 其中書寫了靜態方法的使用,本身比較簡單,框架很容易。但這個是很經典的**書寫模式,值得多加練習。

from pymysql import connect

class jd(object):

def __init__(self):

self.conn = connect(host='192.168.4.2', port=3306, user='sai',

password='123456', database='python',

charset='utf8')

self.cursor = self.conn.cursor()

def show_all_items(self):

self.cursor.execute("select * from students")

for i in self.cursor.fetchall():

print(i)

def show_cates(self):

sql = "select name from students"

self.cursor.execute(sql)

print(self.cursor.fetchall())

def show_brands(self):

sql = "select id from students"

self.cursor.execute(sql)

print(self.cursor.fetchall())

def __del__(self):

self.cursor.close()

self.conn.close()

@staticmethod

def print_menu():

print("------京東**--------")

print("1:所有的商品")

print("2:所有的商品分類")

print("3:所有的商品品牌分類")

return input("輸入對於的序列號:")

def run(self):

while true:

num = self.print_menu()

if num == "1":

self.show_all_items()

elif num == "2":

self.show_cates()

elif num == "3":

self.show_brands()

else:

print("輸入有誤")

def main():

jd = jd()

jd.run()

if __name__ == "__main__":

main()

pymysql 封裝呼叫

import pymysql class skq def init self config 建立連線 self.connection pymysql.connect config def add self,dict,table 執行sql語句 try with self.connection.cur...

使用PyMySQL連線MySQL

pymysql 是在 python3.x 中用於連線 mysql 的庫,而python2則是使用mysqldb。向資料表中插入資料import pymysql 選擇test資料庫,連線mysql db pymysql.connect host localhost port 3306 user roo...

呼叫pymysql模組運算元據庫

1 建立資料庫表 1 defcreate table tb name 2import pymysql 匯入模組3 連線資料庫 4 db pymysql.connect localhost root 123 zabbix db 5 建立游標物件 工具 6 cursor db.cursor 7 sql語...