7.3 Designing the Python GUI database

设计数据库,创建两个表(Books)存储书名与(Quotations)注释,主要是sql的使用

  1. 创建数据库

create database databaseName default character set 'utf8';

  1. 显示所有数据库

show databases;

  1. 显示所有表

show tables;

  1. 创建表如下

  2. 显示表结构

desc tableName;(show columns from tableName;)

# -*- coding: utf-8 -*-
import GuiDBConfig as guiConf
import mysql.connector as mysql
# connect by unpacking dictionary credentials
conn = mysql.connect(**guiConf.dbConfig)

# create cursor
cursor = conn.cursor()
GUIDB = 'GuiDB'
try:
    # cursor.execute("CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(GUIDB))
    # cursor.execute("show databases")

    # 显示数据库表,下面一句等同于下面两句分开执行  show tables from tables == user database , show tables 分开执行
    # cursor.execute("show tables from {}".format(GUIDB))
    # select DB
    cursor.execute("use {}".format(GUIDB))
    # create Table inside DB
    # cursor.execute("CREATE TABLE Books (       \
    #          Book_ID INT NOT NULL AUTO_INCREMENT, \
    #          Book_Title VARCHAR(25) NOT NULL,     \
    #          Book_Page INT NOT NULL,              \
    #          PRIMARY KEY (Book_ID)                \
    #        ) ENGINE=InnoDB")
    # cursor.execute("CREATE TABLE Quotations ( \
    #           Quote_ID INT,                     \
    #           Quotation VARCHAR(250),           \
    #           Books_Book_ID INT,                \
    #           FOREIGN KEY (Books_Book_ID)       \
    #               REFERENCES Books(Book_ID)     \
    #               ON DELETE CASCADE             \
    #       ) ENGINE=InnoDB")

    cursor.execute("SHOW TABLES")
    print(cursor.fetchall())
    cursor.execute("show columns from quotations")
    print(cursor.fetchall())
except mysql.Error as error:
    print("Failed to create DB: {}".format(error))
cursor.close()
conn.close()
print(conn)
7.3 Designing the Python GUI database_第1张图片
Paste_Image.png

terminal 显示如下:

7.3 Designing the Python GUI database_第2张图片
Paste_Image.png

你可能感兴趣的:(7.3 Designing the Python GUI database)