mySQL建立簡單商品資料庫

2021-08-16 16:41:24 字數 1735 閱讀 3676

from pymysql import connect

class jd(object):

「」「京東商品」「」

def __init__(self):

# 開啟資料庫連線

self.conn = connect(host="localhost", port=3306, user="root", password="mysql", database="jing_dong",

charset="utf8")

# 使用cursor() 方法建立乙個物件cursor

self.cursor = self.conn.cursor()

def __del__(self):

# 關閉資料庫連線

self.cursor.close()

self.conn.close()

def show_all_items(self):

"""顯示所用商品"""

# 使用execute() 方法執行sql查詢

# 使用 fetchone() 方法獲取單條資料

sql = "select * from goods"

self.cursor.execute(sql)

for temp in self.cursor.fetchall():

print(temp)

def show_all_ification(self):

"""顯示商品分類"""

sql = "select name from goods_cates"

self.cursor.execute(sql)

for temp in self.cursor.fetchall():

print(temp)

def show_all_brand(self):

"""顯示品牌分類"""

sql = "select name from goods_brands"

self.cursor.execute(sql)

for temp in self.cursor.fetchall():

print(temp)

# 使用靜態方法

@staticmethod

def print_name():

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

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

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

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

return input("請輸入選項功能:")

def run(self):

while true:

num = self.print_name()

if num == "1":

self.show_all_items()

elif num == "2":

self.show_all_ification()

elif num == "3":

self.show_all_brand()

else:

print("使用者輸入錯誤,請重新輸入:")

def main():

# 建立乙個京東**物件

jd = jd()

# 呼叫這個物件的rum方法,讓其執行

jd.run()

ifname== 『main『:

main()

建立mysql資料庫總結 MySQL資料庫總結

引擎 檢視mysql預設引擎 show variables like storage engine 檢視表引擎 show table status from 資料庫名 修改表引擎 alter table 表名 engine innodb 建立時直接定義引擎 create table 表名 engin...

建立mysql資料庫

登陸資料庫 如何建立乙個資料庫和表單 new mysql.php 獲取連線mysql connect 伺服器,使用者名稱,密碼 con mysql connect localhost root if con 建立乙個資料庫school if mysql query create database s...

MySQL 建立資料庫

使用普通使用者,你可能需要特定的許可權來建立或者刪除 mysql 資料庫。所以我們這邊使用root使用者登入,root使用者擁有最高許可權,可以使用 mysql mysqladmin 命令來建立資料庫。以下命令簡單的演示了建立資料庫的過程,資料名為 runoob root host mysqladm...