Python MySQL 创建数据库
章节Python MySQL 入门Python MySQL 创建数据库Python MySQL 创建表Python MySQL 插入表Python MySQL SelectPython MySQL WherePython MySQL Order ByPython MySQL DeletePython MySQL 删除表Python MySQL UpdatePython MyS...
·
章节
创建数据库
要在MySQL中创建数据库,可使用“CREATE DATABASE”语句:
示例
创建一个名为“mydatabase”的数据库:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="你的用户名",
passwd="你的密码"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
如果执行上述代码没报错,那么你已经成功创建了一个数据库。
示例
返回系统数据库列表:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="你的用户名",
passwd="你的密码"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
或者你可以尝试,建立连接时,直接访问数据库:
示例
尝试连接到数据库“mydatabase”:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="你的用户名",
passwd="你的密码",
database="mydatabase"
)
如果数据库不存在,将报错。
更多推荐




所有评论(0)