#/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)
 
 
 
 

最新文章

  1. 【从零开始学BPM,Day4】业务集成
  2. .dat 导入sqlserver2000
  3. http://www.cnblogs.com/youring2/archive/2011/03/28/1997694.html
  4. [BZOJ3611][Heoi2014]大工程
  5. iOS学习05C语言函数
  6. Boostrap响应式与非响应式
  7. TCP/IP协议原理与应用笔记27:网际协议(IP)之 选项(Options)
  8. Mysql学习(慕课学习笔记9)查询、分组
  9. 枚举子集的3种方式 -- C++描述
  10. Buffett saying
  11. 当今Web应用的主要技术
  12. 一行代码实现FMDB的CURD操作
  13. Docker 架构(二)【转】
  14. 微信支付没有结果通知,notify_url参数的接口没有收到微信支付结果通知
  15. web安全系列2:http初探
  16. [HAOI2008]圆上的整点(数论)
  17. 哥们,你真以为你会做这道JVM面试题?
  18. 微服务架构与实践3_api
  19. 求[1,n]中与m互素的个数
  20. centos 安装git服务器,配置使用证书登录并你用hook实现代码自动部署

热门文章

  1. C#TreeView读取Xml,TreeView导出到Xml
  2. JS和jquery加载的区别
  3. hdu 3879 hdu 3917 构造最大权闭合图 俩经典题
  4. LeetCode OJ-- Text Justification
  5. (0)git安装
  6. 避免在block中循环引用(Retain Cycle in Block)
  7. Autolayout 03
  8. nginx +uwsgi + django配置
  9. http://preshing.com/
  10. .NET中XML 注释 SandCastle 帮助文件.hhp 使用HTML Help Workshop生成CHM文件