在本教程的这一部分中,我们将创建名为 PythonDB 的新数据库。

获取现有数据库列表

我们可以使用以下 MySQL 查询来获取所有数据库的列表。

>  show databases;  

示例

import mysql.connector  
  
#Create the connection object   
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google")  
  
#creating the cursor object  
cur = myconn.cursor()  
  
try:  
    dbs = cur.execute("show databases")  
except:  
    myconn.rollback()  
for x in cur:  
    print(x)  
myconn.close() 

输出:

('EmployeeDB',)
('Test',)
('TestDB',)
('information_schema',)
('javatiku',)
('javatiku1',)
('mydb',)
('mysql',)
('performance_schema',)
('testDB',)

创建新数据库

可以使用以下 SQL 查询来创建新数据库。

>  create database <database-name>    

示例

import mysql.connector  
  
#Create the connection object   
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google")  
  
#creating the cursor object  
cur = myconn.cursor()  
  
try:  
    #creating a new database  
    cur.execute("create database PythonDB2")  
  
    #getting the list of all the databases which will now include the new database PythonDB  
    dbs = cur.execute("show databases")  
      
except:  
    myconn.rollback()  
  
for x in cur:  
        print(x)  
          
myconn.close()  

输出:

('EmployeeDB',)
('PythonDB',)
('Test',)
('TestDB',)
('anshika',)
('information_schema',)
('javatiku',)
('javatiku1',)
('mydb',)
('mydb1',)
('mysql',)
('performance_schema',)
('testDB',)

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程