Python MySQL-创建数据库

本教程介绍如何使用Python连接MySQL并创建数据库,同时展示如何检查数据库是否存在及列出系统中的所有数据库。
摘要由CSDN通过智能技术生成

In this tutorial we will learn how to create a database or how to check if any database exists already in MySQL using Python.

在本教程中,我们将学习如何使用Python 创建数据库或如何检查 MySQL中是否已经存在任何数据库

To create the database in MySQL we will use the "CREATE DATABASE" statement.

为了在MySQL中创建数据库,我们将使用“ CREATE DATABASE ”语句。

Python MySQL- CREATE DATABASE (Python MySQL - CREATE DATABASE)

Below we have the basic syntax of the create database statement:

下面是创建数据库语句的基本语法

CREATE DATABASE database_name

Now we will create a database named "studytonight". Let us see how we will create it:

现在,我们将创建一个名为“ studytonight”的数据库。 让我们看看如何创建它:

Python MySQL-创建数据库示例 (Python MySQL - Create Database Example)

Let us create a database named "studytonight".Given below is the code snippet for the same:

让我们创建一个名为“ studytonight ”的数据库。下面是相同的代码片段:

#for our convenience we will import mysql.connector as mysql
import mysql.connector as mysql

db = mysql.connect(
    host = "localhost",
    user = "yourusername",
    passwd = "yourpassword"
)

## Now Let us create an instance of 'cursor' class which is used to execute the 'SQL' statements in 'Python'

cursor = db.cursor()

## creating a database called 'studytonight'
## 'execute()' method is used to compile a 'SQL' statement
## below statement is used to create tha 'studytonight' database

cursor.execute("CREATE DATABASE studytonight")

In the case, if the database named "studytonight" already exists then the above code will raise an error.

在这种情况下,如果名为“ studytonight”的数据库已经存在,则上述代码将引发错误。

If there is no error it means the database is created successfully.

如果没有错误,则表示数据库创建成功

Now let us see how we can check all the databases in our system.

现在让我们看看如何检查系统中的所有数据库。

Python MySQL-列出所有数据库 (Python MySQL - List all Database)

We can list down all the databases in MySQL using Python and in that list we can also check if any particular Database is available or not.

我们可以使用Python列出MySQL中的所有数据库,在该列表中,我们还可以检查是否有任何特定的数据库可用。

To check if any database already exists SHOW DATABASES SQL statement is used.

要检查是否已存在任何数据库,请使用SHOW DATABASES SQL语句。

Let us see how:

让我们看看如何:

To list out all the databases in the system.Below is the code snippet for the same:

列出系统中的所有数据库。下面是相同的代码段:

#for our convenience we will import mysql.connector as mysql
import mysql.connector as mysql

db = mysql.connect(
    host = "localhost",
    user = "yourusername",
    passwd = "yourpassword"
)

cursor = db.cursor()

cursor.execute("SHOW DATABASES")

for x in cursor:
  print(x)

The output of the above code will list out all the databases in the system:

上面代码的输出将列出系统中的所有数据库:

('admin_panel',) ('information_schema',) ('mysql',) ('performance_schema',) ('sakila',) ('studytonight',) ('sys',) ('world',) ('xyz',)

('admin_panel',)('information_schema',)('mysql',)('performance_schema',)('sakila',)('studytonight',)('sys',)('world',)(' xyz',)

As you can see in the code above, we have used Python for loop to iterate over the cursor object and print the names of all the databases found. Similarly, we can compare the name of databases to check if any particular database already exists, before creating it using Python if condition.

如您在上面的代码中所看到的,我们使用Python的for循环来迭代游标对象并打印找到的所有数据库的名称。 同样,我们可以比较数据库的名称,以检查是否存在任何特定的数据库,然后再使用Python if condition创建它。

So in this tutorial we learned how to create a new database in MySQL using Python and how to list down all the MySQL databases in Python and print them in console.

因此,在本教程中,我们学习了如何使用Python在MySQL中创建新数据库,以及如何在Python中列出所有MySQL数据库并在控制台中打印它们。

翻译自: https://www.studytonight.com/python/python-mysql-database

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值