mysql安装与配置 mysql操作常用命令 python——mysql操作

MySQL安装与配置

1、初始化一个数据库,并新建一个data文件。

	"C:\Program Files\mysql-5.7.31-winx64\bin\mysqld.exe" --initialize-insecure

我们可以在"C:\Program Files\mysql-5.7.31-winx64\下的文件夹里面看到有一个新的data文件夹已被新建。

2、创建一个windows方式,启动数据库,关闭数据库

"C:\Program Files\mysql-5.7.31-winx64\bin\mysqld.exe" --install mysql15
net start mysql15 --开启数据库
net stop mysql15 --断开数据库

3、 查看配置文件的优先级

"C:\Program Files\mysql-5.7.31-winx64\bin\mysqld.exe" --help --verbose
本机数据库名:mysql15 账户:root 密码:jgq123

net start mysql15 启动数据库

"C:\Program Files\mysql-5.7.31-winx64\bin\mysql.exe"  -uroot -p -h127.0.0.1 链接数据库(前提是先启动数据库)

忘记密码:
修改.ini文件里面,在[mysqld]下面添加一行
        skip-grant-tables=1
        
然后windows重启数据库:
net stop mysql01
net start mysql01

重启后,无需密码就可以进入:
use mysql
update user set authentication_string = password("新密码"),password_last_change=now() where user='root';


 show databases; 查看已存在数据库

exit 退出数据库

创建数据库:
 create database 数据库名 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

删除数据库:
 drop database 数据库名;

进入数据库:
 use 数据库名;

查看数据库里面的表:
show tables;

查看表里面的数据:
desc tb2

数据库管理

1、内置客户端操作

(1) 进入"use 数据库",查看当前所有表: ’ show tables ’

(2)创建表结构

create table 表名(
	列名	类型,
	列名	类型,
    列名	类型    
)default charset = utf8;

create table tb1(
	id int,
    name varchar(16) not null, --不能为空
    email varchar(32) null, --不允许为空(默认)
    age int
)default charset = utf8;

create table tb1(
	id int primary key, --主键(不允许为空,不能重复)
    name varchar(16) not null, --不能为空
    email varchar(32) null, --不允许为空(默认)
    age int default 3 --插入数据时,如果不给age赋值时,默认值:3
)default charset = utf8;

主键一般用于表示当前这条数据的ID编号(类似于人的身份证),需要我们自己去维护这个重复的值,比较繁琐。所以,在数据库中一般会将主键也和自增结合。

create table tb1(
	id int not null auto_increment primary key, --主键(不允许为空,不能重复)
    name varchar(16) not null, --不能为空
    email varchar(32) null, --不允许为空(默认)
    age int default 3 --插入数据时,如果不给age赋值时,默认值:3
)default charset = utf8;

注意:一个表中只能有一个自增列【自增列,一般都是主键】

(3)删除表

drop table 表名

(4)清空表

delete from 表名;或 truncate table 表名;(速度快、无法回滚撤销等)

(5)修改表

添加列

alter table 表名 add 列名 类型;
alter table 表名 add 列名 类型 DEFAULT 默认值;
alter table 表名 add 列名 类型 not null default 默认值;
alter table 表名 add 列名 类型 not null primary key auto_increment;

删除列

alter table 表名 drop column 列名;

修改列 类型

alter table 表名 modify column 列名 类型;

修改列 类型 + 名称

alter table 表名 change 原列名 新列名 新类型;
alter table tb change id id int not null;
alter table tb change id id int not null default 5;
alter table tb change id id int not null primary key quto_increment;
alter table tb change id id int; --允许为空,删除默认值,删除自增;

修改列 默认值

ALTER TABLE 表名 ALTER 列名 SET DEFAULT 1000;

删除列 默认值

ALTER TABLE 表名 ALTER 列名 DROP DEFAULT;

添加主键

alter table 表名 add primary key(列名)

删除主键

alter table 表名 drop primary key;

常见列类型

  • int[(m)][unsigned][zerofill]
    
  • int  表示有符号,取值范围:-2147483648 ~ 2147483648
    int unsigned 表示无符号,取值范围: 0 ~ 4294967295
    int(5)zerofill 仅用于显示,当部门组5位时,按照左边补0,例如:00002;满足时,正常显示。
    

    python数据库基本操作

    import pymysql
    #连接MySQL(底层使用socket)
    conn = pymysql.connect(host = '127.0.0.1', port = 3306,user = 'root', passwd = 'jgq890',charset="utf8")
    cursor = conn.cursor()#创建一个游标
    
    #1、查看数据库
    #发送指令
    cursor.execute("show databases")
    #获取指令的结果
    result = cursor.fetchall()
    print(result)
    
    #2、创建数据库(新增、删除、修改)
    #发送指令
    cursor.execute("create database db3 DEFAULT CHARSET utf8 COLLATE utf8_general_ci")
    conn.commit()
    
    #3、查看数据库
    #发送指令
    cursor.execute("show databases")
    #获取指令的结果
    result = cursor.fetchall()
    print(result)
    
    #4、删除数据库
    #发送指令
    cursor.execute("drop database db3")
    conn.commit()
    
    #查看数据库
    #发送指令
    cursor.execute("show databases")
    #获取指令的结果
    result = cursor.fetchall()
    print(result)
    
    #进入数据库,查看表
    #发送指令
    cursor.execute("use mysql")
    cursor.execute("show tables")
    result = cursor.fetchall()
    print(result)
    
    #关闭连接
    cursor.close()
    conn.close()
    

    内置客户端操作

    新增数据

    insert into 表名 (列名,列名,列名) values(对应的值,对应的值,对应的值)insert into tb1(name,password) values('青海','0001');
    insert into tb1(name,password) values('青海','0001'),('上海','0002');
    insert into tb1 values('青海','0001'),('上海','0002');
    

    删除数据

    delete from 表名;
    delete from 表名 where 条件;
    
    delete from tb1;
    delete from tb1 where name = "wupei";
    delete from tb1 where name = "wupei" and password = "123";
    delete from tb1 where id>9;
    

    修改数据

    update 表名 set 列名=值;
    update 表名 set 列名=where 条件;
    
    update tb1 set name = "wupei";
    update tb1 set name = "wupei" where id = 1;
    update tb1 set age = age + 1 where id = 2;
    
    update user set name = concat(name,'123') where id = 2; --concat一个函数,可以拼接字符串,意思是将’123‘拼接在name id是2的name变量上面,就会变成:wupei123,前提是charvar满足数量条件。
    

    查询数据

    select * from 表名;
    select 列名,列名,列名 from 表名;
    select 列名,列名 as 别名,列名 from 表名;
    select * from 表名 where 条件;
    
    select * from tb1;
    select id, name, age from tb1;
    select id, name as N,age from tb1;
    select id,name as N,age, 111 from tb1; --as N就是将原本的表头名name改成N,后面的111是新增的表头;
    
    select * from tb1 where id = 1;
    select * from tb1 where id>1;
    select * from tb1 where id!=1;
    select 8 from tb1 where name = "wupei" and password = "123";
    

import pymysql
#链接MySQL,自动执行use userdb; --进入数据库
conn = pymysql.connect(host = ‘127.0.0.1’,port=3306,user=‘root’,passwd = “jgq123”,charset = “utf8”,db=‘userdb’)
cursor = conn.cursor()

#新增(需commit)
“”"
cursor.execute(“insert into th1(name,password) values(‘青海省’,‘0001’)”)
conn.commit()
“”"

#删除(需commit)
“”"
cursor.execute(“delete from tb1 where id = 1”)
conn.commit()
“”"

#3、修改(需commit)
“”"
cursor.execute(“update tb1 set name = ‘xx’ where id = 1”)
conn.commit()
“”"

#4、查询
“”"
cursor.execute(“select * from tb where id>10”)
data = cursor.fetchone() #cursor.fetchone()
print(data)
“”"

#关闭数据库
cursor.close()
conn.close()


> 其实真正在做项目开发时,流程如下:
>
> * 第一步:根据项目的功能来设计相应的 数据库&表结构 (不会经常变动,在项目设计之初就确定好了)。
> * 第二步:操作表结构中的数据,以达到实现业务逻辑的目的。

### 例如实现一个用户管理系统

> 先使用MySQL自带的客户端创建相关的数据库和表结构(相当于先创建好Excel结构)

~~~ sql
create database userdb default charset utf8 collate utf8_general_ci;
create table users(
	id not null primary key auto_increment,
    name varchar(32),
    password varchar(64)
)default charset = utf8;

再在程序中执行编写相应的功能实现 注册、登录 等功能。

import pymysql

def register():
    print('用户')
    user = input('请输入用户名:')
    password = input('请输入密码:')
    
    #连接指定的数据库
    conn = pymysql.connect(host='12.0.0.1', port = 3306, user = 'root', passwd = 'jgq123', charset = 'utf8',db = "userdb")
    cursor.commit()
    #执行SQL语句(sql注入风险)
    sql = 'insert into users(name,password) values("{}","{}")'.format(user, pasword)
    cursor.execute(sql)
    conn.commit()
    
    #关闭数据库
    cursor.close()
    conn.close()
    
    print("注册成功, 用户名:{}, 密码:{}"format(user),(password))
    
def login():
    print("用户登录")
    user = input("请输入用户名:")
    password = input("请输入密码:")
    
    #链接指定数据库
    conn = pymysql.connect(host='12.0.0.1', port = 3306, user = 'root', passwd = 'jgq123', charset = 'utf8',db = "userdb")
    cursor.commit()
    
    #有注入风险
    sql = 'select * from users where name = '{}' and password = '{}''.format(user, pasword)
    cursor.execute(sql)
    result = cursor.fetchone()#去向MYSQL获取结果
    #NULL
    #(1,wupeiqi,123)
    
    
    
    
    #关闭数据库
    cursor.close()
    conn.close()
    
    if result:
        print("登录成功",result)
    else:
        print("登录失败!")

def run():
    choice = input("1、注册; 2、登录")
    if choice == '1':
        register()
    elif choice == '2':
        login()
    else:
        print("输入错误!")
      
if __name__ == '__main__':
    run()
        
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值