python操作Mysql数据库
Posted effortsing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python操作Mysql数据库相关的知识,希望对你有一定的参考价值。
1、通过 pip 安装 pymysql [[email protected] ~]# pip install pymysql 2、测试连接 [[email protected] ~]# python Python 3.6.5 (default, Nov 22 2018, 03:13:09) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pymysql >>> 3.1、 创建mysql数据库、表、语句 mysql> create database ceshi; Query OK, 1 row affected (0.02 sec) mysql> use ceshi; Database changed mysql> create table if not exists test ( id int(10) PRIMARY KEY AUTO_INCREMENT, name varchar(50) NOT NULL); Query OK, 0 rows affected (0.03 sec) mysql> insert into test values(1,‘dahuju‘); Query OK, 1 row affected (0.00 sec) mysql> insert into test values(2,‘yunjisuan‘); Query OK, 1 row affected (0.03 sec) mysql> select * from test; +----+-----------+ | id | name | +----+-----------+ | 1 | dahuju | | 2 | yunjisuan | +----+-----------+ 2 rows in set (0.00 sec) 3.2、查询操作 import pymysql #导入 pymysql #打开数据库连接 db= pymysql.connect(host="192.168.0.204",user="root",password="jenkins!666",db="ceshi",port=3306) # 使用cursor()方法获取操作游标 cur = db.cursor() #1.查询操作 # 编写sql 查询语句 test 对应的表名 sql = "select * from test" try: cur.execute(sql) #执行sql语句 results = cur.fetchall() #获取查询的所有记录 print("id","name") #遍历结果 for row in results : id = row[0] name = row[1] print(id,name) except Exception as e: raise e finally: db.close() #关闭连接 验证: ssh://[email protected]:22/usr/bin/python -u /home/progect/app/py_code/test3.py id name 1 dahuju 2 yunjisuan 3.3、插入操作 import pymysql #2.插入操作 db= pymysql.connect(host="192.168.0.204",user="root",password="jenkins!666",db="ceshi",port=3306) # 使用cursor()方法获取操作游标 cur = db.cursor() sql_insert ="""insert into test(id,name) values(3,‘人工智能‘)""" try: cur.execute(sql_insert) #提交 db.commit() except Exception as e: #错误回滚 db.rollback() finally: db.close() 验证: mysql> select * from test; +----+--------------+ | id | name | +----+--------------+ | 1 | dahuju | | 2 | yunjisuan | | 3 | 人工智能 | +----+--------------+ 3 rows in set (0.00 sec) 3.4、更新操作 import pymysql #3.更新操作 db= pymysql.connect(host="192.168.0.204",user="root",password="jenkins!666",db="ceshi",port=3306) # 使用cursor()方法获取操作游标 cur = db.cursor() sql_update ="update test set name = ‘%s‘ where id = %d" try: cur.execute(sql_update % ("大数据",1)) #像sql语句传递参数 #提交 db.commit() except Exception as e: #错误回滚 db.rollback() finally: db.close() 验证: mysql> select * from test; +----+--------------+ | id | name | +----+--------------+ | 1 | 大数据 | | 2 | yunjisuan | | 3 | 人工智能 | +----+--------------+ 3 rows in set (0.00 sec) 3.5、删除操作 import pymysql #4.删除操作 db= pymysql.connect(host="192.168.0.204",user="root",password="jenkins!666",db="ceshi",port=3306) # 使用cursor()方法获取操作游标 cur = db.cursor() sql_delete ="delete from test where id = %d" try: cur.execute(sql_delete % (3)) #像sql语句传递参数 #提交 db.commit() except Exception as e: #错误回滚 db.rollback() finally: db.close() 验证: mysql> select * from test; +----+-----------+ | id | name | +----+-----------+ | 1 | 大数据 | | 2 | yunjisuan | +----+-----------+ 2 rows in set (0.00 sec)
以上是关于python操作Mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章