一、数据库基础
表 table:数据是保存在表内,保存在一个表内的数据,应该具有相同的数据格式
行:行用于记录数据
记录:行内的数据
列:列用于规定数据格式
字段:数据的某个列
主键:唯一地标识表中的某一条记录,不能空,不能重复
二、数据库的数据类型
1、数字类型
整数: tinyint、smallint、mediumint、int、bigint
浮点数: float、double、real、decimal
日期和时间: date、time、datetime、timestamp、year
2、字符串类型
字符串: char、varchar
文本: tinytext、text、mediumtext、longtext
3、二进制
(可用来存储图片、音乐等): tinyblob、blob、mediumblob、longblob
三、Python之MySQL数据库操作
1、连接myaql创建新的数据库
import pymysql
# 打开数据库连接(ip/端口/数据库用户名/登录密码/数据库名)
conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='123456')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = conn.cursor()
# 编写sql语句创建数据库
sql1 = 'CREATE DATABASE test1 DEFAULT CHARACTER SET utf8'
# 执行sql语句
cursor.execute(sql1)
conn.close()
2、创建表
import pymysql
# 打开数据库连接(ip/端口/数据库用户名/登录密码/数据库名)
conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='123456',db = 'test1')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = conn.cursor()
# 编写sql语句创建表test_student_table
sql2 = 'CREATE TABLE IF NOT EXISTS test_student_table (id VARCHAR(255) NOT NULL,name VARCHAR(255) NOT NULL,age VARCHAR(255) NOT NULL,PRIMARY KEY (id))'
# 执行sql语句
cursor.execute(sql2)
conn.close()
3、往表中插入数据
import pymysql
def get_conn():