实训day25(8.9)

一、方法一
指定pip从哪个源服务器下载和安装Python包

pip3 config set global.index-url

清华镜像站

https://pypi.tuna.tsinghua.edu.cn/simple

安装 SQLAlchemy

yum -y install sqlalchemy

使用pip3安装pandas库

pip3 install pandas

导入pandas作为pd
import pandas as pd
创建数据表

from sqlalchemy import create_engine
        class Python_Mysql(object):
                def __init__(self):
                        print("test")
                def getEngine(seft):
                        host=input("sign mysql server host:")
                        username=input("sign mysql username:")
                        password=input("sign mysql password:")
                        databasename=input("sign database name:")
                        port=input("sign mysql port:")
        engine=create_engine(f"mysql+pymysql://{username}:
{password}@{host}:{port}/{databasename}")
                        return engine
                def querySql(self,conn):
                        sql=input("sign your sql:")
                        return
pd.read_sql(sql=sql,con=conn)
if __name__=="__main__":
        demo=Python_Mysql()
        #sql=input("sign sql:")
        # sql="select * from user"
        rs=demo.querySql(demo.getEngine())
        print(rs)

二、方法二
1. 设置清华镜像站(从国内下载安装包,提⾼下载和安装速度)
2. 安装pandas数据分析⼯具(pandas是知名的数据分析⼯具,pandas有完整的读取数据的⼯具,以及DateFrame数据框架,⽤于保存从数据库中读取的数据)
3. 安装pymysql连接器(oracle为开发者提供的python管理mysql的⼯具,通过这个⼯具,就恶意在不替原有代码的情况下,应对数据库软件的升级)
指定pip从哪个源服务器下载和安装Python包

pip3 config set global.index-url

清华镜像站

https://pypi.tuna.tsinghua.edu.cn/simple

​​安装pandas

yum -y install pandas

安装pymysql

yum -y install pymysql

导入pandas作为pd

>>> import pandas as pd

导入pymysql
>>> import pymysql
>>> conn=pymysql.connect(
... host='192.168.1.130',
... user='dongdong',
... password='1',
... database='test',
... port=3306
... )
>>> conn
<pymysql.connections.Connection object at 0x7f9e24ba2c88>
>>> cursor=conn.cursor()
>>> cursor
<pymysql.cursors.Cursor object at 0x7f9e24ba2668>
>>> sql="select * from user"
>>> cursor.excute(sql)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Cursor' object has no attribute 'excute'
>>> cursor.execute(sql)
3
>>> cursor.description
(('id', 3, None, 11, 11, 0, False), ('username', 253, None, 180, 180, 0, False), ('password', 253, None, 180, 180, 0, False))
>>> desc=cursor.description
>>> res
3
>>> res=cursor.fetchall()
>>> res
((1, 'aaa', '123'), (8, 'baba', 'pipi'), (20, 'aaaaaaaa', 'bbbbbbbb'))
>>> desc
(('id', 3, None, 11, 11, 0, False), ('username', 253, None, 180, 180, 0, False), ('password', 253, None, 180, 180, 0, False))
>>> [item[0] for item in desc]
['id', 'username', 'password']
>>> col=[item[0] for item in desc]
>>> df=pd.DataFrame(res,columns=col)
>>> df
           id         username         password
0         1            aaa                 123
1         8            baba                pipi
2         20         aaaaaaaa         bbbbbbbb

总结

1. 和shell脚本⼀样python⽂件也可以称为py脚本,也是将pyhton指令做⼀个集合
2. 为了脚本更加的智能化和⾃动化,添加选择语句(智能)循环语句(⾃动化)
3. 同时为了开发效率,可读性,做了⽅法,类,模块,
三、python脚本
1. python脚本完成并配置成功之后,将脚本部署为⼀个⼆进制的可执⾏⽂件
2. 因为py⽂件要被执⾏需要在linux中安装python环境
3. 但是⼆进制可执⾏⽂件,不要环境,在任何linux主机上都可以执⾏
4. 步骤
(2) 安装pyinstaller

pip3 install pyinstaller

(2)使⽤pyinstaller⽣成可执⾏⽂件

pyinstaller --onefile xxx.py

# py⽂件中必须是有 if __name__=="__main__":
# xxxxx

以下是一个简单的C语言万年历的实训报告,供您参考: 一、实训目的 本次实训的主要目的是通过编写一个简单的C语言万年历程序,巩固C语言基本语法和编程思想,提高编程能力和解决问题的能力。 二、实训要求 1、能够使用C语言基本语法和控制语句编写程序。 2、能够使用数组来存储和操作数据。 3、能够使用函数来实现模块化编程。 4、能够使用指针来操作内存中的数据。 5、能够使用文件来读取和写入数据。 三、实训内容 1、程序功能 本程序实现了一个简单的万年历功能,可以通过输入年份和月份来显示该月的日历。 2、程序实现 以下是本程序的主要代码实现: ```c #include <stdio.h> // 判断是否为闰年 int is_leap_year(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return 1; } else { return 0; } } // 获取某个月份的总天数 int get_days(int year, int month) { int days[] = {31, 28 + is_leap_year(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return days[month - 1]; } // 获取某个日期是星期几 int get_weekday(int year, int month, int day) { if (month == 1 || month == 2) { year--; month += 12; } int c = year / 100; int y = year % 100; int w = y + y / 4 + c / 4 - 2 * c + 26 * (month + 1) / 10 + day - 1; w = (w % 7 + 7) % 7; return w; } // 显示日历 void show_calendar(int year, int month) { int days = get_days(year, month); int weekday = get_weekday(year, month, 1); printf(" 日 一 二 三 四 五 六\n"); int i; for (i = 0; i < weekday; i++) { printf(" "); } for (i = 1; i <= days; i++) { printf("%2d ", i); if ((weekday + i) % 7 == 0) { printf("\n"); } } if ((weekday + days) % 7 != 0) { printf("\n"); } } int main() { int year, month; printf("请输入年份:"); scanf("%d", &year); printf("请输入月份:"); scanf("%d", &month); if (month < 1 || month > 12) { printf("月份输入错误!\n"); return 1; } printf(" %d年%d月\n", year, month); show_calendar(year, month); return 0; } ``` 四、实训总结 通过本次实训,我学会了如何使用C语言来编写一个简单的万年历程序,巩固了C语言基本语法和编程思想,加强了对函数、数组、指针、文件等概念和用法的理解,提高了编程能力和解决问题的能力。同时,我也意识到在编程过程中需要注重代码的规范、可读性和可维护性,这对于日后的开发工作非常重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值