Python MySQL-创建数据库
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 datab...
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)