首页 > 数据库 >python-pymysql-基本使用

python-pymysql-基本使用

时间:2023-01-13 10:34:51浏览次数:43  
标签:基本 python pymysql cursor sql test MariaDB conn

python-pymysql-基本使用

1. pymysql-基本使用

  • 创建表

    [root@python tmp]#   mysql -h 127.0.0.1 -u test -p'Test@963852'   
    Welcome to the   MariaDB monitor. Commands end with ;   or \g.   
    Your MariaDB   connection id is 8   
    Server version:   5.5.65-MariaDB MariaDB Server       
    Copyright (c) 2000,   2018, Oracle, MariaDB Corporation Ab and others.       
    Type 'help;' or '\h'   for help. Type '\c' to clear the current input statement.       
    
    MariaDB [(none)]> use test
    Database changed
    
    # 执行以下创建表
    MariaDB [test]> show tables;
    Empty set (0.00 sec)
         
    
    MariaDB [test]> create table user(
        -> id bigint unsigned auto_increment,
        -> username varchar(100) not null,
        -> password varchar(100) not null,
        -> primary key (id)
        -> )ENGINE=innoDB DEFAULT CHARSET=utf8;
    Query OK, 0 rows affected (0.05 sec) 
    
  • 使用pymysql连接数据库

    #!/usr/bin/env python3
    # _*_ coding: utf-8 _*_
    # Author:shichao
    # File: .py
    
    import pymysql
    conn = pymysql.connect(host='127.0.0.1',
                           port=3306,
                           user='root',
                           password='123456',
                           db='test',
                           charset='utf8',
                           cursorclass=pymysql.cursors.DictCursor)
    try:
        with conn.cursor() as cursor:
            # 定义执行sql变量
            sql = "insert into user(username, password) values('test1', '123456')"
    
            # 执行一条sql记录
            cursor.execute(sql)
    
            # 写入到数据库
            conn.commit()
    
        with conn.cursor() as cursor:
            # 定义执行sql变量
            sql = "select id,username,password from user"
    
            # 执行一条记录
            cursor.execute(sql)
    
            # 读取一条记录
            result = cursor.fetchone()
            print(result)
    finally:
        # 执行完成后,执行关闭连接
        conn.close()
    

标签:基本,python,pymysql,cursor,sql,test,MariaDB,conn
From: https://www.cnblogs.com/scajy/p/17048772.html

相关文章

  • python-pymysql安装和数据库安装
    python-pymysql安装和数据库安装1.pymysql安装安装python连接数据库pymysql模块[root@python~]#pip3installPyMySQlLookinginindexes:http://mirror......
  • python-pymysql模块介绍
    python-pymysql模块介绍1.pymysql模块介绍pymysql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。但目前pymysql支持python3.x而后者不支持3.x版本。pymysql......
  • python pymysql-增删改查
    pymysql-增删改查1.pymysql-增删改查#!/usr/bin/envpython3#_*_coding:utf-8_*_#Author:shichao#File:.pyimportpymysqlconn=pymysql.connect(host='1......
  • DPDK入门实践1——基本概念
    我在一篇博文中看到DPDK的解释为内核旁路技术,我觉得这个解释很形象也很好记,DPDK重载了网卡驱动,将数据包的控制平面和数据平面分离,驱动在收到数据包后不再硬中断通知CPU,而......
  • python pymysql-参数使用
    pymysql-参数使用1.pymysql-参数使用connect()函数常用参数:方法描述host数据库主机地址user数据库账户passwd账户密码db使用的数据库port......
  • python-标准库os模块的使用
    python-标准库os模块的使用1.标准库osos库主要对目标和文件操作。方法描述os.name返回操作系统类型os.environ以字典形式返回系统变量os.putenv(ke......
  • python-标准库platform模块的使用
    python-标准库platform模块1.python-标准库platform模块platform库用于获取操作系统详细信息。方法描述platform.platform()返回操作系统平台platform.......
  • python-标准库sys模块的使用
    python-标准库sys模块的使用1.python-标准库sys模块sys库用于与Python解释器交互。方法描述sys.argv从程序外部传递参数argv[0]#代表本身名字argv[1]#第......
  • python-标准库random模块的使用
    python-标准库random模块1.python-标准库random模块random库用于生成随机数。方法描述random.randint(a,b)随机返回整数a和b范围内数字random.random()......
  • python-标准库glob模块的使用
    python-标准库glob模块1.python-标准库glob模块glob库用于文件查找,支持通配符(*、?、[])示例示例1:查找目录中所有以.sh为后缀的文件:glob.glob('/home/user/*.sh'......