linux-Mysql学习

Mysql数据库优点及特点

1、性能卓越服务稳定,很少出现异常宕机
2、开放源代码且无版权制约,自主性强、使用成本低。
3、历史悠久、社区及用户非常活跃,遇到问题,可以很快获取到帮助。
4、软件体积小,安装使用简单,并且易于维护,安装及维护成本低。
5、支持多种操作系统,提供多种api几口,支持多种开发语言。

作者:年少时难免轻狂Ho
链接:https://www.jianshu.com/p/6306e200f5b2
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

1.Mysql初步了解

01.Mysql配置文件

Ubuntu18上通过apt安装的msql配置文件及目录是:/etc/mysql/mysql.conf.d

linux指令操作:ls查看文件

cd /etc

cd mysql

cd mysql.conf.d

vim mysqld.cnf # 需要修改进入插入模式,按i/a。推出按Esc,输入:q,回车

windows上面是安装目录下的my.ini。如果需要改Mysql的一些设置就需要在配置文件里面去改。

02.Mysql服务启动/关闭

Linux上,通过查看/启动/停止: service mysql status/start/stop ,如果权限不够最前面加sudosu root

windows下通过任务管理器即可查看

03.登录Mysql

mysql端口:3306

查看密码:mima

本地连接:mysql -u用户名 -p

​ 输入密码

例子:mysql -uadmin -p

virturebox-ubuntu添加端口转发规则,主机端口3306,子系统端口3306

远程连接:mysql -hIP地址 -P端口 -u用户名 -p

​ 输入密码

例子:mysql -h127.0.0.1 -P3306 -uadmin -p

04.操作数据库

1.操作数据库

查看有哪些数据库:show databases;

创建数据库:create database [if not exists] dbname;

进入某个数据库:use dbname;

查看所有表:show tables; 指定数据库:show tables from dbname;

删除数据库:drop {database | schema} [if exists] dbname;

判断是否在哪个数据库里:select database();

查看当前用户:select user();

2.操作表

创建表:create table [if not exists] stu(id int,name varchar(20))charset=‘utf8’;

查看表的创建信息:show create table stu;

查看表结构信息:desc stu;

删除表:drop table [if exists] stu;

修改表结构:

​ 添加多个字段:alter table stu add (age int,test1 char(10),test2 int,test3 int);

​ 修改字段类型:alter table stu modify test1 varchar(18);

​ 修改字段:alter table stu change test2 t2 varchar(20);

​ 修改表名:alter table stu rename students;

​ 删除多个字段:alter table students drop t2,drop test3;

3.表的增删改查

增:

insert into stu value(1,‘moran’,‘18’);

insert into stu values(2,‘xiaoluo’,20),(3,‘xinya’,22),(4,‘stone’,16);

insert into stu(id,name) value(5,‘moran2’);

查:

select * from stu;

select id,name,age from stu;

改:

update stu set age = 18 where id=2

删:

delete from stu where name=‘moran’

2.Python与Mysql交互

一:Python操作MySQL步骤

1:Python中操作MySQL的步骤

img

2.引入模块

在.py文件中引入pymysql模块

import pymysql

pymysql是python的一个第三方与mysql交互的库,需要安装

pip install pymsql -i https://pypi.douban.com/simple

3.Connection 对象

用于建立与数据库的连接

创建对象,调用connect()方法

conn = connect(参数列表)
  • 参数host:连接的mysql主机,如果本机是’localhost’
  • 参数port:连接的mysql主机的端口,默认是3306
  • 参数database:数据库的名称
  • 参数user:连接的用户名
  • 参数password:连接的密码
  • 参数charset:通信采用的编码方式,推荐使用utf8

对象方法:

  • close()关闭连接
  • commit()提交
  • cursor()返回Cursor对象,用于执行sql语句并获得结果

4.Cursor()

  • 用于执行sql语句,使用频度最高的语句为select、insert、update、delete
  • 获取Cursor对象:调用Connection对象的cursor()方法
cs1 = conn.cursor()

对象的方法

  • close()关闭
  • execute(operation [, parameters ])执行语句,返回受影响的行数,主要用于执行insert、update、delete语句,也可以执行create、alter、drop等语句
  • fetchone()执行查询语句时,获取查询结果集的第一个行数据,返回一个元组
  • fetchall()执行查询时,获取结果集的所有行,一行构成一个元组,再将这些元组装入一个元组返回

对象的属性

  • rowcount只读属性,表示最近一次execute()执行后受影响的行数
  • connection获得当前连接对象

二:增删改查

1.增删改

from pymysql import *

def main():
    # 创建Connection连接
    conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='mysql',charset='utf8')
    # 获得Cursor对象
    cs1 = conn.cursor()
    # 执行insert语句,并返回受影响的行数:添加一条数据
    # 增加
    count = cs1.execute('insert into goods_cates(name) values("硬盘")')
    #打印受影响的行数
    print(count)

    count = cs1.execute('insert into goods_cates(name) values("光盘")')
    print(count)

    # # 更新
    # count = cs1.execute('update goods_cates set name="机械硬盘" where name="硬盘"')
    # # 删除
    # count = cs1.execute('delete from goods_cates where id=6')

    # 提交之前的操作,如果之前已经之执行过多次的execute,那么就都进行提交
    conn.commit()

    # 关闭Cursor对象
    cs1.close()
    # 关闭Connection对象
    conn.close()

if __name__ == '__main__':
    main()

2.查询一条数据

from pymysql import *

def main():
    # 创建Connection连接
    conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
    # 获得Cursor对象
    cs1 = conn.cursor()
    # 执行select语句,并返回受影响的行数:查询一条数据
    count = cs1.execute('select id,name from goods where id>=4')
    # 打印受影响的行数
    print("查询到%d条数据:" % count)

    for i in range(count):
        # 获取查询的结果
        result = cs1.fetchone()
        # 打印查询的结果
        print(result)
        # 获取查询的结果

    # 关闭Cursor对象
    cs1.close()
    conn.close()

if __name__ == '__main__':
    main()

3.查询多行数据

from pymysql import *

def main():
    # 创建Connection连接
    conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
    # 获得Cursor对象
    cs1 = conn.cursor()
    # 执行select语句,并返回受影响的行数:查询一条数据
    count = cs1.execute('select id,name from goods where id>=4')
    # 打印受影响的行数
    print("查询到%d条数据:" % count)

    # for i in range(count):
    #     # 获取查询的结果
    #     result = cs1.fetchone()
    #     # 打印查询的结果
    #     print(result)
    #     # 获取查询的结果

    result = cs1.fetchall()
    print(result)

    # 关闭Cursor对象
    cs1.close()
    conn.close()

if __name__ == '__main__':
    main()

三:参数化

sql语句的参数化,可以有效的方式sql注入攻击

此处不同于python的字符串格式化,全部使用%s占位

from pymysql import *

def main():

    find_name = input("请输入物品名称:")

    # 创建Connection连接
    conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
    # 获得Cursor对象
    cs1 = conn.cursor()


    # # 非安全的方式
    # # 输入 " or 1=1 or "   (双引号也要输入)
    # sql = 'select * from goods where name="%s"' % find_name
    # print("""sql===>%s<====""" % sql)
    # # 执行select语句,并返回受影响的行数:查询所有数据
    # count = cs1.execute(sql)

    # 安全的方式
    # 构造参数列表
    params = [find_name]
    # 执行select语句,并返回受影响的行数:查询所有数据
    count = cs1.execute('select * from goods where name=%s', params)
    # 注意:
    # 如果要是有多个参数,需要进行参数化
    # 那么params = [数值1, 数值2....],此时sql语句中有多个%s即可 

    # 打印受影响的行数
    print(count)
    # 获取查询的结果
    # result = cs1.fetchone()
    result = cs1.fetchall()
    # 打印查询的结果
    print(result)
    # 关闭Cursor对象
    cs1.close()
    # 关闭Connection对象
    conn.close()

if __name__ == '__main__':
    main()
# 注意:
# 如果要是有多个参数,需要进行参数化
# 那么params = [数值1, 数值2....],此时sql语句中有多个%s即可 

# 打印受影响的行数
print(count)
# 获取查询的结果
# result = cs1.fetchone()
result = cs1.fetchall()
# 打印查询的结果
print(result)
# 关闭Cursor对象
cs1.close()
# 关闭Connection对象
conn.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值