18 12 07 MySQL 与python 的交互

---恢复内容开始---

 python 中 关于SQL语句的查询

from pymysql import *  # 由于只能用了一个MySQL 的包所以全部引进

def main():
    # 创建Connection连接
    conn = connect(host='localhost',port=3306,user='root',password='root',database='jing_dong',charset='utf8')
    # 获得Cursor对象
    cs1 = conn.cursor()
    # 执行select语句,并返回受影响的行数:查询一条数据  execute语句后 用的是SQL语句
    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()

fetch 用来进行取值  有  fecthone  fecthmany  fecthall

python 中 SQL  运用于增删改

from pymysql import *

def main():
    # 创建Connection连接
    conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='root',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()

防止sql注入的安全问题

  • 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()

 

 自己写的很low逼的查询框架   以后或许可以用来修改修改

//实验四
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
  float m,temp=0,sum=0;
     int   n,i;
   float averge=0;
   cout<<"How many  number  do you input?\n";//询问输入的个数
   cin>>n;
   int   a[n];
   cout<<"input  1  zheng shu\n";
   cin>>m;
   a[0]=m;
   cout<<"please  input    "<<n-1<<"    numbers\n";
   for(i=1;i<n;i++)//循环输入整数
   {
       cin>>a[i];
   }
  int  max=a[0],min=a[0];
   for(i=0;i<n;i++)
   {
       if(max<a[i]){max=a[i];}//比较max和各整数的大小
       if(min>a[i]){min=a[i];}
        sum+=a[i];
    }
    averge=sum/n;
   cout<<"the  MAX numbers is :"<<max<<endl;
   cout<<"the  Min numbers is :"<<min<<endl;

   cout<<"the  averge is :"<<averge<<endl;
return 0;   
}

 

  

posted @ 2018-12-10 19:34  Mr喃先森  阅读(214)  评论(0编辑  收藏  举报