码迷,mamicode.com
首页 > 数据库 > 详细

python操作Mysql数据库

时间:2018-12-01 23:28:41      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:0.00   upd   arch   mysql   ssh   传递参数   数据   mysq   int   

1、通过 pip 安装 pymysql

[root@bogon ~]# pip install pymysql


2、测试连接

[root@bogon ~]# 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://root@192.168.0.204: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数据库

标签:0.00   upd   arch   mysql   ssh   传递参数   数据   mysq   int   

原文地址:https://www.cnblogs.com/effortsing/p/10051209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!