MySQL(14)

import mysql.connector

connection = mysql.connector.connect(host='localhost',
                                     port='3306',
                                     user='root',
                                     password='123456')
#创建游标对象
cursor = connection.cursor()
#执行sql指令
cursor.execute("CREATE DATABASE `qq`;")
cursor.execute("SHOW DATABASES;")

#取得databases的全部资料
records = cursor.fetchall()
for i in records:
    print(i)

cursor.close()
connection.commit()
connection.close()

***********************part2*********************
connection = mysql.connector.connect(host='localhost',
                                     port='3306',
                                     user='root',
                                     password='123456',
                                     database='sql_tutorial')
#创建游标对象
cursor = connection.cursor()
#执行sql指令
cursor.execute("SELECT*FROM `branch`;")
#cursor.execute("SELECT branch_id,branch_name FROM `branch`;")
#取得table的相关资料
records = cursor.fetchall()
for i in records:
    print(i)

cursor.close()
connection.close()

***********************part3*********************

connection = mysql.connector.connect(host='localhost',
                                     port='3306',
                                     user='root',
                                     password='123456',
                                     database='sql_tutorial')
#创建游标对象
cursor = connection.cursor()
#执行sql指令
cursor.execute("SELECT*FROM `branch`;")
#cursor.execute("insert into `branch` values(5,'运营',210);")


cursor.close()
#使得删除/新增/修改命令生效
connection.commit()
connection.close()







语法内容的总结:

1:定义connection /cursor对象

2:执行SQL指令

3:取得databases/table返回的内容

4:commit()

你可能感兴趣的:(mysql,数据库)