首页 > 数据库 >windows下mysql5.7安装,及python操作mysql

windows下mysql5.7安装,及python操作mysql

时间:2023-04-26 19:01:16浏览次数:43  
标签:windows mysql5.7 pymysql cursor python tb1 mysql close conn

windows下mysql5.7安装

mysql5.7官方下载:https://www.mysql.com/

可参考教程:https://blog.csdn.net/qq_39715000/article/details/123534326?

注意:一:

my.ini配置文件:如果保存目录以t开头,默认会将t转义为空格(解决方法加这个 \\):

[mysqld]
#端口号
port = 3306
#mysql-5.7.27-winx64的路径
basedir=D:\toos\MySQL\mysql-5.7.42-winx64
#mysql-5.7.27-winx64的路径+\data
datadir=D:\\toos\\MySQL\\mysql-5.7.42-winx64\\data 

#skip-grant-tables

#编码
character-set-server=utf8
 
default-storage-engine=INNODB
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
 
[mysql]
#编码
default-character-set=utf8

二:

初始化有data目录:

命令:mysqld --initialize-insecure

用法

创建数据库

create database 数据库名

删除数据库

drop database 数据库名

查看数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| study              |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

创建表

mysql> create table tb1(
    -> id bigint auto_increment primary key,
    -> salary int,
    -> age tinyint
    -> ) default charset=utf8;
Query OK, 0 rows affected (0.32 sec)

查看表结构

mysql> desc tb1;
+--------+------------+------+-----+---------+----------------+
| Field  | Type       | Null | Key | Default | Extra          |
+--------+------------+------+-----+---------+----------------+
| id     | bigint(20) | NO   | PRI | NULL    | auto_increment |
| salary | int(11)    | YES  |     | NULL    |                |
| age    | tinyint(4) | YES  |     | NULL    |                |
+--------+------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

给表中插入数据

mysql> insert into tb1(salary,age) values(10000,18);
Query OK, 1 row affected (0.12 sec)

mysql> insert into tb1(salary,age) values(20000,19);
Query OK, 1 row affected (0.03 sec)

mysql> insert into tb1(salary,age) values(20000,21),(30000,22);
Query OK, 2 rows affected (0.03 sec)

查看数据

mysql> select * from tb1;
+----+--------+------+
| id | salary | age  |
+----+--------+------+
|  1 |  10000 |   18 |
|  2 |  20000 |   19 |
|  3 |  20000 |   21 |
|  4 |  30000 |   22 |
+----+--------+------+
4 rows in set (0.10 sec)

mysql>

删除表内数据:

delete from 表名;

delete from 表名 where 条件;

删除表

mysql> drop table test1;
Query OK, 0 rows affected (0.18 sec)

其它用法可参考视频:https://www.bilibili.com/video/BV1rT4y1v7uQ?p=29&spm_id_from=pageDriver&vd_source=5fed6e8a7e3ad9f10860bf7a4540ba71

python操作数据库

创建数据

# 1 连接数据库
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='你的密码', charset='utf8', db='study')
cursor = conn.cursor()


# 2 发送指令
cursor.execute('insert into tb1 (salary, age) values (1000, 55)')
conn.commit()

# 3 关闭连接
cursor.close()
conn.close()


# 或者
 db = pymysql.connect(host="43.139.184.232", port=3306, user="python4fcf6", password="xxxxx", db="python4fcf6",
                             charset='utf8')
        cursor = db.cursor()  # 游标 能获得连接的游标,这个游标可以用来执行SQL查询

        sql = "INSERT INTO gpt_new (ip, time, question, answer) VALUE (%s, %s, %s, %s)"
        gpt_data = (ip, self.__time, self.__msg, info)

        with contextlib.suppress(Exception):
            cursor.execute(sql, gpt_data)  # 执行sql语句
            db.commit()  # 提交至数据库
        # 关闭光标对象
        cursor.close()
        # 关闭连接
        db.close()

查询数据

import pymysql

# 1 连接数据库
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='xxxx', charset='utf8', db='study')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)   # 加这个输出方式为字典,不加默认元组输出


# 2 执行查询指令
sql = "select * from tb1"
cursor.execute(sql)
data_list = cursor.fetchall()  # 获取数据    如果要获取符合条件的第一条数据,fetchone即可
print(data_list)


# 3 关闭连接
cursor.close()
conn.close()

删除数据

import pymysql

# 1 连接数据库
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='xxxx', charset='utf8', db='study')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)   # 加这个输出方式为字典,不加默认元组输出


# 2 删除
cursor.execute("delete from tb1 where id = %s", [3, ])
conn.commit()


# 3 关闭连接
cursor.close()
conn.close()

修改数据

import pymysql

# 1 连接数据库
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='xxx', charset='utf8', db='study')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)   # 加这个输出方式为字典,不加默认元组输出


# 2 修改
cursor.execute("update tb1 set salary=%s where id=%s", [20000, 5])
conn.commit()


# 3 关闭连接
cursor.close()
conn.close()

总结:在进行 新增、删除、修改时,需要加conn.commit(),否则数据库无数据

​ 查询时,不需要,单,需执行fetchall/fetchone

标签:windows,mysql5.7,pymysql,cursor,python,tb1,mysql,close,conn
From: https://www.cnblogs.com/code3/p/17357004.html

相关文章

  • 树莓派4B-Python-控制超声波模块
    树莓派4B-Python-控制超声波模块超声波模块:超声波模块为常用的HC-SR04型号,有四个引脚,分别为Vcc、Trig(控制端)、Echo(接收端)、GND,使用起来也比较简单。在树莓派最新官方系统Raspbian中都安装有一个比较好使用的GPIO库,名为“gpiozero”,它包含了许多模块的使用函数,直接调用就好。参......
  • pycharm中python测试一直‘Instantiating tests...’转圈
    问题描述:defget_formatted_name(first,last):"""生成简洁的姓名"""full_name=first+""+lastreturnfull_name.title()importunittestfromname_functionimportget_formatted_nameclassNamesTestCase(unit......
  • Nginx部署成Windows服务
    把nginx部署成服务,随系统开机启动,方法如下: 一、下载官网下载地址:http://nginx.org/en/download.html准备nginx-service 二、配置 三、安装服务 四、启动 Done. ......
  • 如何用Python画一只狗狗——turtle基础
    这只小狗主要用了turtle库里的circle()头有些方正,比较自然。话不多说,展示代码:fromturtleimport*pensize(5)seth(0)pd()color('black')circle(20,80)circle(200,30)circle(30,60)circle(200,29.5)circle(20,60)circle(-150,22)circle(-50,10)circle(5......
  • 如何用Python画奥运五环——circle()
    奥林匹克标志(theOlympicsymbol),又称奥运五环标志,是由《奥林匹克宪章》确定的奥林匹克运动在全球范围内的视觉形象标识。它由5个奥林匹克环从左到右互相套接组成,上方是蓝色、黑色、红色三环,下方是黄色、绿色二环,亦能以单色形式使用,整体造型为一个底部小的规则梯形。1913年,现代奥......
  • 恒创科技:Windows云主机崩溃了怎么办?
    ​无法避免服务器崩溃已不是什么秘密,无论选择Windows云主机还是Linux云主机。但不可否认的是,任何错误都可能给企业带来灾难性的后果。该怎么办?持续监控服务器是可能的解决方案之一。即便如此,如果服务器已经处于关闭阶段,也不是一切都丢失了。首先,最好立即采取行动,但是,要了解......
  • Java程序部署成Windows服务
    大多数时候部署Java程序时,都是简单弄一个控制台,一是简单,二是能很方便监测运行是否正常。但如果是在服务器上部署这种模式就不可取,假设服务器重启了,重启之后不登录到远程桌面的话,即使把启动命令放到启动项,也不会自动运行。另一个问题就是,针对WindowsServer2008以后的操作系统,控制......
  • python 函数是对象
    defhi(name="yasoob"):return"hi"+nameprint(hi())#output:'hiyasoob'#我们甚至可以将一个函数赋值给一个变量,比如greet=hi#我们这里没有在使用小括号,因为我们并不是在调用hi函数#而是在将它放在greet变量里头。我们尝试运行下这个print(greet())#out......
  • Python基础
    list方法len返回list长度append往list末尾追加元素insert可以通过索引号插入指定位置,如果插入的位置之前没有元素,会追加到该位置myList=[0,1,2]myList.insert(5,5)print(len(myList))print(myList)//4//[0,1,2,5]pop删除list末尾的元素,如果要删除指定......
  • python与java 对应的加密算法
    python与java对应的加密算法1.gzip加密java的gzip加密:importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.util.Arrays;importjava.util.zip.GZIPInputStream;importjava.util.zip.GZIPOutputStream;publicclassHello{......