#/usr/bin/python
#coding=utf-8
#@Time   :2017/11/21 0:20
#@Auther :liuzhenchuan
#@File   :mysql 数据操作.py
import MySQLdb
 
 
def connect_mysql():
    db_config={
        'host':'192.168.16.70',
        'port':3306,
        'user':'root',
        'db':'python',
        'passwd':'123123',
        'charset':'utf8'
    }
    try:
        cnx=MySQLdb.connect (**db_config)
    except Exception as e:
        raise e
    return  cnx
connect_mysql()
# print connect_mysql()
if __name__ == "__main__":
    sql = 'create table test(id int not null); insert into test(id) values (100);'
    cnx = connect_mysql()
    #查看链接数据库cnx 的方法
    #cursor 游标  execute 执行
    # print dir(cnx)
    cus = cnx.cursor()
    try:
        #执行游标
        cus.execute(sql)
        #关闭游标
        cus.close()
        #mysql 提交
        cnx.commit()
    except Exception as e:
        raise e
        #有异常进行回滚
        cnx.rollback()
    finally:
        #没有异常关闭连接
        cnx.close()
 
 
linux系统登录mysql进行查看,创建了test表,插入了语句
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cactidb            |
| mysql              |
| performance_schema |
| python             |
| test               |
+--------------------+
6 rows in set (0.00 sec)
 
mysql> use python;
Database changed
mysql> show tables;
+------------------+
| Tables_in_python |
+------------------+
| employees        |
| test             |
+------------------+
2 rows in set (0.00 sec)
mysql> select *from test;
+-----+
| id  |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)
 
 
 
 

python 基础 9.3 mysql 数据操作的更多相关文章

  1. MySQL数据操作与查询笔记 • 【目录】

    持续更新中- 我的大学笔记>>> 章节 内容 第1章 MySQL数据操作与查询笔记 • [第1章 MySQL数据库基础] 第2章 MySQL数据操作与查询笔记 • [第2章 表结构管 ...

  2. 《MySQL数据操作与查询》- 综合项目 - 学生管理系统

    <MySQL数据操作与查询>综合项目需求 一.系统整体功能 维护学生信息.老师信息和成绩信息. 支持按多种条件组合查询学生信息和成绩信息. 二.系统的信息需求 一个班级有一个讲师一个班主任 ...

  3. mysql 数据操作 目录

    mysql 记录的增删改查 mysql 数据操作 单表查询 mysql 数据操作 多表查询

  4. mysql 数据操作 单表查询 目录

    mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...

  5. mysql 数据操作 多表查询 目录

    mysql 数据操作 多表查询 准备 多表连接查询介绍 mysql 数据操作 多表查询 多表连接查询 笛卡尔积 mysql 数据操作 多表查询 多表连接查询 内连接 mysql 数据操作 多表查询 多 ...

  6. mysql 数据操作 单表查询 where 约束 目录

    mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...

  7. mysql 数据操作 单表查询 group by 分组 目录

    mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...

  8. mysql 数据操作 多表查询 子查询 介绍

    子查询就是: 把一条sql语句放在一个括号里,当做另外一条sql语句查询条件使用 拿到这个结果以后 当做下一个sql语句查询条件mysql 数据操作  子查询 #1:子查询是将一个查询语句嵌套在另一个 ...

  9. SQL学习笔记四之MySQL数据操作

    阅读目录 一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理 一 介绍 MySQL数据操作: DML =========== ...

随机推荐

  1. jenkins下脚本权限问题

    在jenkins环境下,执行需要root权限的脚本,报错. 修改方法: 1. centos环境下,在/etc/sudoers.d/ 目录下,增加一个 jenkins文件,内容如下: Defaults: ...

  2. LeetCode OJ--Anagrams **

    https://oj.leetcode.com/problems/anagrams/ 在一个vector<string>中,找到所有经过顺序变换,可以变成一样的 string. 首先,对每 ...

  3. 吉首大学 问题 L: 小李子的老年生活

    时间限制: 1 Sec  内存限制: 128 MB提交: 719  解决: 27 题目描述 小李子有n-1个朋友,分别编号为1..n-1,小李子的编号是n ,小李子的表面朋友的编号会与小李子编号互质 ...

  4. facebook architecture 2 【转】

    At the scale that Facebook operates, a lot of traditional approaches to serving web content breaks d ...

  5. python 集合互相转换

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...

  6. 数据库资源博客---小麦苗BEST

    http://blog.csdn.net/lihuarongaini/article/details/60584577 http://blog.csdn.net/lihuarongaini/artic ...

  7. 深入浅出 Cocoa 之 Core Data(4)- 使用绑定

    深入浅出 Cocoa 之 Core Data(4)- 使用绑定 罗朝辉(http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 前面讲解了 Core Data 的框架, ...

  8. Android常用URI收藏

    转:http://www.android-study.com/jichuzhishi/338.html 以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent 一.打开一 ...

  9. NVIDIA® Quadro® 四路缓冲 3D立体方案

    http://www.nvidia.cn/object/quadro_pro_graphics_boards_cn.html NVIDIA® Quadro® 专业显卡让地球学家以及时装设计师等许多专业 ...

  10. 2016.10.10 Failed to start component [StandardService[Catalina]]

    Failed to start component [StandardService[Catalina]] 错误原因:有数据残留,点击clean(见下图)     解决办法: 右键点击servers下 ...