当前位置:   article > 正文

Python基础(二)--- IDEA中整合Python和MySQL,使用Python进行SQL操作_idea中写python mysql

idea中写python mysql
一、Python操作MySQL
-----------------------------------------------------
    1.安装MySQL

    2.安装mysql的python模块
        a.下载并安装PyMySQL-master.zip
        https://github.com/PyMySQL/PyMySQL

    3.解压,在解压目录下,进入cmd,执行安装命令
        cmd> python setup.py install

    4.重启idea

    5.创建python文件,测试是否连接
        
  1. import pymysql
  2. print("================测试是否连通====================")
  3. try:
  4. conn = pymysql.connect(host='localhost', user='mysql', passwd='mysql', db='python', port=3306, charset='utf8')
  5. cur = conn.cursor()
  6. cur.execute('select version()')
  7. version = cur.fetchone()
  8. print(version)
  9. cur.close()
  10. conn.close()
  11. except Exception:
  12. print("发生异常")
        ================测试是否连通====================
        ('5.5.60',)


二、使用Python进行SQL操作
--------------------------------------------------------------------------
    1.create and Drop Table
        
  1. # print("================ Drop Table====================")
  2. # try:
  3. # conn = pymysql.connect(host='localhost', user='mysql', passwd='mysql', db='python', port=3306, charset='utf8')
  4. # cur = conn.cursor()
  5. # cur.execute('drop table t1')
  6. # cur.close()
  7. # conn.close()
  8. # except Exception:
  9. # print("发生异常")
  10. # print("================ Create Table====================")
  11. # try:
  12. # conn = pymysql.connect(host='localhost', user='mysql', passwd='mysql', db='python', port=3306, charset='utf8')
  13. # cur = conn.cursor()
  14. # cur.execute('create table t1(id int primary key auto_increment, name varchar (20), age int)')
  15. # conn.commit()
  16. # cur.close()
  17. # conn.close()
  18. # except Exception:
  19. # print("发生异常")
    2.insert into
        --------------------------------------------------------------------
        
  1. print("================ Insert 插入 ====================")
  2. try:
  3. # 开启连接
  4. conn = pymysql.connect(host = "localhost", user = "mysql", passwd= "mysql",db = "python",port = 3306,charset="utf8");
  5. # 打开游标
  6. cur = conn.cursor();
  7. # 开始插入
  8. sql = "insert into t1 (name,age) values ('%s','%d')" %('tom',12);
  9. cur.execute(sql);
  10. conn.commit();
  11. cur.close()
  12. conn.close()
  13. except Exception:
  14. print("发生异常")
        ----------------------------------------------------------------------------------------
        
  1. print("================ Insert 插入10000条记录 ====================")
  2. print("================ 带提交和回滚机制 ====================")
  3. try:
  4. # 开启连接
  5. conn = pymysql.connect(host='localhost', user='mysql', passwd='mysql', db='python', port=3306, charset='utf8')
  6. # 打开游标
  7. cur = conn.cursor()
  8. # 关闭自动提交
  9. conn.autocommit(False);
  10. #执行sql
  11. i = 0 ;
  12. while i < 10000 :
  13. sql = "insert into t1(name,age) values('%s',%d)" % ("tom" + str(i),i % 100);
  14. #异常回滚测试
  15. if(i == 50):
  16. sql = "insert"
  17. #执行sql插入
  18. cur.execute(sql)
  19. i += 1 ;
  20. #提交
  21. conn.commit()
  22. except Exception:
  23. print("发生异常,进行回滚操作")
  24. conn.rollback()
  25. finally:
  26. #关闭
  27. cur.close()
  28. conn.close()
'
运行
    3.delect from
       
  1. print("================ Delete====================")
  2. try:
  3. # 开启连接
  4. conn = pymysql.connect(host='localhost', user='mysql', passwd='mysql', db='python', port=3306, charset='utf8')
  5. # 打开游标
  6. cur = conn.cursor()
  7. # 关闭自动提交
  8. conn.autocommit(False);
  9. #执行sql
  10. sql = "delete from t1 where id > 50";
  11. cur.execute(sql)
  12. #提交
  13. conn.commit()
  14. except Exception:
  15. print("发生异常,进行回滚操作")
  16. conn.rollback()
  17. finally:
  18. #关闭
  19. cur.close()
  20. conn.close()
    4.update set
        
  1. print("================ Update ====================")
  2. try:
  3. # 开启连接
  4. conn = pymysql.connect(host='localhost', user='mysql', passwd='mysql', db='python', port=3306, charset='utf8')
  5. # 打开游标
  6. cur = conn.cursor()
  7. # 关闭自动提交
  8. conn.autocommit(False);
  9. #执行sql
  10. sql = "update t1 set age = age - 20 where age > 20";
  11. cur.execute(sql)
  12. #提交
  13. conn.commit()
  14. except Exception:
  15. print("发生异常,进行回滚操作")
  16. conn.rollback()
  17. finally:
  18. #关闭
  19. cur.close()
  20. conn.close()
    5.select from
       
  1. print("================ select from ====================")
  2. try:
  3. # 开启连接
  4. conn = pymysql.connect(host='localhost', user='mysql', passwd='mysql', db='python', port=3306, charset='utf8')
  5. # 打开游标
  6. cur = conn.cursor()
  7. # 关闭自动提交
  8. conn.autocommit(False);
  9. #执行sql
  10. sql = "select * from t1";
  11. cur.execute(sql)
  12. res = cur.fetchall()
  13. for r in res :
  14. print(str(r[0]) + "," + r[1] + "," + str(r[2]));
  15. #提交
  16. conn.commit()
  17. except Exception:
  18. print("发生异常,进行回滚操作")
  19. conn.rollback()
  20. finally:
  21. #关闭
  22. cur.close()
  23. conn.close()
    6.select count(*)
       
  1. print("================ select count(*) from ====================")
  2. try:
  3. # 开启连接
  4. conn = pymysql.connect(host='localhost', user='mysql', passwd='mysql', db='python', port=3306, charset='utf8')
  5. # 打开游标
  6. cur = conn.cursor()
  7. # 关闭自动提交
  8. conn.autocommit(False);
  9. #执行sql
  10. sql = "select count(*) from t1 where id > 10";
  11. cur.execute(sql)
  12. res = cur.fetchone()
  13. print(res[0])
  14. #提交
  15. conn.commit()
  16. except Exception:
  17. print("发生异常,进行回滚操作")
  18. conn.rollback()
  19. finally:
  20. #关闭
  21. cur.close()
  22. conn.close()
    7.调用Mysql中的函数
        
  1. print("================ 调用Mysql的函数 ====================")
  2. try:
  3. # 开启连接
  4. conn = pymysql.connect(host='localhost', user='mysql', passwd='mysql', db='python', port=3306, charset='utf8')
  5. # 打开游标
  6. cur = conn.cursor()
  7. # 关闭自动提交
  8. conn.autocommit(False);
  9. #执行sql
  10. sql = "call mydata.up_add(1,2,@q);";
  11. cur.execute(sql)
  12. cur.execute("select @q");
  13. res = cur.fetchone()
  14. print(res[0])
  15. conn.commit()
  16. #提交
  17. except Exception:
  18. print("发生异常,进行回滚操作")
  19. conn.rollback()
  20. finally:
  21. #关闭
  22. cur.close()
  23. conn.close()
    8.调用Mysql中的存储过程
       

  1. print("================ 调用Mysql的存储过程 ====================")
  2. try:
  3. # 开启连接
  4. conn = pymysql.connect(host='localhost', user='mysql', passwd='mysql', db='mydata', port=3306, charset='utf8')
  5. # 打开游标
  6. cur = conn.cursor()
  7. # 关闭自动提交
  8. conn.autocommit(False);
  9. #创建表
  10. sql = "create table mytable(id int primary key auto_increment, name varchar(20), password varchar(20), age int)";
  11. cur.execute(sql)
  12. #执行存储过程
  13. cur.execute("call big_insert2(100)");
  14. #查询
  15. sql = "select * from mytable";
  16. cur.execute(sql)
  17. res = cur.fetchall()
  18. for r in res:
  19. print(str(r[0]) + "," + str(r[1]) + "," +str(r[2]) + "," +str(r[3]))
  20. conn.commit()
  21. #提交
  22. except Exception:
  23. print("发生异常,进行回滚操作")
  24. conn.rollback()
  25. finally:
  26. #关闭
  27. cur.close()
  28. conn.close()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/980499
推荐阅读
相关标签
  

闽ICP备14008679号