python 基础 9.3 mysql 数据操作
2024-08-24 05:19:30
#/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)
最新文章
- 【从零开始学BPM,Day4】业务集成
- .dat 导入sqlserver2000
- http://www.cnblogs.com/youring2/archive/2011/03/28/1997694.html
- [BZOJ3611][Heoi2014]大工程
- iOS学习05C语言函数
- Boostrap响应式与非响应式
- TCP/IP协议原理与应用笔记27:网际协议(IP)之 选项(Options)
- Mysql学习(慕课学习笔记9)查询、分组
- 枚举子集的3种方式 -- C++描述
- Buffett saying
- 当今Web应用的主要技术
- 一行代码实现FMDB的CURD操作
- Docker 架构(二)【转】
- 微信支付没有结果通知,notify_url参数的接口没有收到微信支付结果通知
- web安全系列2:http初探
- [HAOI2008]圆上的整点(数论)
- 哥们,你真以为你会做这道JVM面试题?
- 微服务架构与实践3_api
- 求[1,n]中与m互素的个数
- centos 安装git服务器,配置使用证书登录并你用hook实现代码自动部署
热门文章
- C#TreeView读取Xml,TreeView导出到Xml
- JS和jquery加载的区别
- hdu 3879 hdu 3917 构造最大权闭合图 俩经典题
- LeetCode OJ-- Text Justification
- (0)git安装
- 避免在block中循环引用(Retain Cycle in Block)
- Autolayout 03
- nginx +uwsgi + django配置
- http://preshing.com/
- .NET中XML 注释 SandCastle 帮助文件.hhp 使用HTML Help Workshop生成CHM文件