python_数据库(增删查改)

news/2024/7/16 7:30:48

使用python对数据库进行操作的总结:

import random
from pymysql import cursors,connect

conn=connect(host='127.0.0.1',
             user='root',
             password='admin',
             db='test',
             charset='utf8',
             cursorclass=cursors.DictCursor)

students_num=100

###函数区#####
def random_name():
    t_name_len = random.randint(3, 10)
    t_name=""
    for i in range(t_name_len):
        if i == 0:
            t_name=t_name+chr(random.randint(97, 122)).upper()
        else:
            t_name = t_name + chr(random.randint(97, 122))
    return t_name
###函数区#####

try:
    with conn.cursor() as cursor:
        ###创建表
        create_sql = 'create table students(num INT NULL AUTO_INCREMENT,' \
                                                'name VARCHAR(12) default NULL,' \
                                                'score INT,' \
                                                'PRIMARY KEY (num));'
        cursor.execute(create_sql)
        ###增加数据
        add_sql = 'insert into students values'
        for i in range(students_num):
            t_score = random.randint(50, 100)
            add_sql=add_sql+'('+str(100+i)+','+'\''+random_name()+'\''+','+str(t_score)+'),'
        add_sql=add_sql[0:-1]+";"
        cursor.execute(add_sql)
        conn.commit()
        ###修改数据
        update_sql = 'update students set name=concat_ws(\'-\',\'a\',name)'+'where length(name) < 4;'
        cursor.execute(update_sql)
        conn.commit()
        ###删除数据
        for i in range(students_num):
            if i % 2 == 0:
                delete_sql='delete from students where num='+str(100+i)+';'
                cursor.execute(delete_sql)
                conn.commit()
        ###查看数据库
        select_sql = 'select * from students;'
        cursor.execute(select_sql)
        results = cursor.fetchall()
        for iline in results:
            print(iline)
finally:
    conn.close()


http://www.niftyadmin.cn/n/1774042.html

相关文章

C++中explicit关键字的作用

explicit用来防止由构造函数定义的隐式转换。 要明白它的作用&#xff0c;首先要了解隐式转换&#xff1a;可以用单个实参来调用的构造函数定义了从形参类型到该类类型的一个隐式转换。 例如: class things {public:things(const std::string&name ""):m_name(n…

部署监控三剑客 Cacti 服务器监控

Cacti 简介 Cacti 是一款使用 PHP 语言开发的性能与流量监控的工具&#xff0c;监测的对象可以是 Linux 或 Windows 服务器&#xff0c;也可以是路由器、交换机等网络设备&#xff0c;主要基于 SNMP 来搜集 CPU 占用、内存占用、运行进程数、磁盘空间、网卡流量等各种数据。 部…

图标设计的意思是什么?资深UI设计师告诉你图标的含义!

2019独角兽企业重金招聘Python工程师标准>>> 接触图标就是从手机的界面&#xff0c;很简单的直观视觉体验&#xff0c;从未考虑过它到底为什么这么设计&#xff0c;用的每个界面图标都是因为好看精致&#xff0c;完全没有考虑它的设计理念&#xff0c;学习图标从它的…

python_安居客区域房源均价工具(matplotlib)

让数据更有价值&#xff0c;就需要对抓取的信息进行适当的处理&#xff0c;然后展现出来。0.打开源码&#xff0c;修改源码该位置选择城市&#xff1a;1.抓取的安居客不团区域房价&#xff0c;然后计算该区域均价&#xff0c;然后通过matploylib绘图&#xff1a;2.python源码&a…

static关键字作用总结

前言 之前讲到final关键字的作用是每次面试的时候我必问求职者的两个问题之一&#xff0c;另外一个问题就是文本会写到的static。final和static一样&#xff0c;都是一个小问题可以看到一个人的基础是否扎实以及平时是否有钻研精神。 静态变量和静态方法 static关键字最基本…

keil5 MDK warning:registered ARM compiler version not found in path

重装 打开keil5弹出窗口&#xff1a; warning:registered ARM compiler version not found in path... 解决&#xff1a; 增加系统环境变量 ARMCC5LIB X:\keil_v5\ARM\ARMCC\bin 貌似需要与下面这个变量都存在 ADSK_CLM_WPAD_PROXY_CHECK FALSE 转载于:https://www.cnblogs.com…

Linux无法解析主机问题

登陆root 进入 /etc/resolv.conf 添加下列内容 nameserver 8.8.8.8 #google域名服务器 nameserver 8.8.4.4 #google域名服务器

python_多线程

学习python多线程&#xff0c;参考了别人的博客&#xff0c;完成了以下简单的示例&#xff1a;1.使用多线程“同时”去做两件事&#xff1a;#codingutf-8 import threading from time import ctime,sleepdef music(func):for i in range(2):print("I was listening to %s.…