#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2017/11/20 16:03
# @Author : lijunjiang
# @File : demo.py import MySQLdb # 连接数据库
# host 数据库IP
# port 数据库监听端口
# user 数据库用户
# passwd 用户密码
# db 数据库名
# charset 字符集 默认uft-8 # MySQLdb.Connect 方法
#comn=MySQLdb.Connect(host="11.11.11.11",user="python",passwd="python",db="python",charset="utf8",port=3306) # 函数型式
def connect_mysql():
db_config = dict(host="11.11.11.11", port=3306, db="python", charset="utf8", user="python", passwd="python")
try:
cnx = MySQLdb.connect(**db_config)
except Exception as err:
raise err
return cnx if __name__ == "__main__":
sql = "create table test(id int not null);"
cnx = connect_mysql() # 连接mysql
cns = cnx.cursor() # 创建一个游标对象
# print(dir(cnx)) try:
cns.execute(sql) # 执行 sql execute执行一条语句
cns.close() # 关闭游标
cnx.commit() # 提交操作
except Exception as err:
raise err
finally:
cnx.close() # 关闭连接 # 执行多条语
sql_many = 'insert into test(id) value (%s);'
param = []
for i in xrange(90,101):
param.append([str(i)])
# print(param) cnx = connect_mysql()
cus = cnx.cursor() try:
cus.executemany(sql_many,param) # executemany()接收一个sql语句,一个列表
# print(dir(cus))
cus.close()
except Exception as err:
raise err
finally:
cnx.close() # 获取执行结果 sql_select = 'select * from test;' cnx = connect_mysql()
cus = cnx.cursor() try:
cus.execute(sql_select) result_one = cus.fetchone() # fetchone() 获取一条结果
print("resutl1 {0}",format(result_one)) result_many = cus.fetchmany(3) # fetchmany(n) 获取n条结果
print("resutl1 {0}", format(result_many)) result_all = cus.fetchall() # fetchall() 获取所有结果
print("resutl1 {0}", format(result_all))
cus.close()
except Exception as err:
raise err
finally:
cnx.close()
mysql> select * from test;
+-----+
| id |
+-----+
| 90 |
| 91 |
| 92 |
| 93 |
| 94 |
| 95 |
| 96 |
| 97 |
| 98 |
| 99 |
| 100 |
+-----+
11 rows in set (0.00 sec)

python 操作数据库1--连接、执行sql语句的更多相关文章

  1. 厚溥教育1718部数据库连接作业答案,分装一个操作数据库而无需写SQL语句的函数

    <?php header("Content-type:text/html;charset=utf8"); //PHP操作数据库的函数 function phpsql($dbc ...

  2. R语言︱ 数据库SQL-R连接与SQL语句执行(RODBC、sqldf包)

    要学的东西太多,无笔记不能学~~ 欢迎关注公众号,一起分享学习笔记,记录每一颗"贝壳"~ --------------------------- 数据库是极其重要的R语言数据导入源 ...

  3. Entity Framework Core 执行SQL语句和存储过程

    无论ORM有多么强大,总会出现一些特殊的情况,它无法满足我们的要求.在这篇文章中,我们介绍几种执行SQL的方法. 表结构 在具体内容开始之前,我们先简单说明一下要使用的表结构. public clas ...

  4. EF Core 执行SQL语句和存储过程

    无论ORM有多么强大,总会出现一些特殊的情况,它无法满足我们的要求.在这篇文章中,我们介绍几种执行SQL的方法. 表结构 在具体内容开始之前,我们先简单说明一下要使用的表结构. public clas ...

  5. Python连接MySQL数据库执行sql语句时的参数问题

    由于工作需要,今天写了一个Python小脚本,其中需要连接MySQL数据库,在执行sql命令时需要传递参数,结果出问题了.在网上查了一下,发现有以下几种方式传递参数: 一.直接把sql查询语句完整写入 ...

  6. 通过MyEclipse工具直接操作数据库,执行sql语句,方便快捷

    原文:通过MyEclipse工具直接操作数据库,执行sql语句,方便快捷 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下: 步骤1:通过MyEcl ...

  7. 在myeclipse中配置DB Driver(数据库用MySql),并在myeclipse执行sql语句操作

    在myeclipse中配置DB Driver(数据库用MySql),并在myeclipse执行sql语句操作 MyEclipse6.5    ,  mysq驱动jar包为mysql-connector ...

  8. 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷

    通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下:     步骤1:通过MyEclipse中的window->show View->ot ...

  9. 如何用VS EF连接 Mysql,以及执行SQL语句 和存储过程?

    VS2013, MySQL5.7.18 , MySQL5.7.14 执行SQL语句: ztp_user z = new ztp_user(); object[] obj = new object[] ...

  10. 使用C# 操作存储过程,执行sql语句通用类

    如何使用C# 操作存储过程,执行sql语句? 闲话不多说,直接上代码:     /// <summary>    /// Sql通用类    /// </summary>    ...

随机推荐

  1. 7.Mongodb安全性流程

    1.安全性流程 2.超级管理员 为了更安全的访问mongodb,需要访问者提供用户名和密码,于是需要在mongodb中创建用户 采用了角色-用户-数据库的安全管理方式 常用系统角色如下: root:只 ...

  2. Server Message Block

    Question: Server Message Block文件共享存储虚拟机的优势是什么? Answer:微软在Windows Server 2012和Hyper-V 3.0中引进了SMB文件共享存 ...

  3. 【转】Git命令解说

    3.12. Git branch  3.12.1. 总述  当第一次执行git init时,系统就会创建一个名为“master”的分支. 而其它分支则通过手工创建.  下面列举一些常见的分支策略:  ...

  4. 【Lowest Common Ancestor of a Binary Tree】cpp

    题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...

  5. python - web自动化测试 - 元素操作 - 定位

    # -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: find_ele.py @ide: PyCharm Community ...

  6. IPMITool driver

    官网链接: https://docs.openstack.org/ironic/latest/admin/drivers/ipmitool.html IPMITool driver 概述IPMI(In ...

  7. 聊聊、Mybatis Java注解实现

    AbstractAnnotationConfigDispatcherServletInitializer public class MvcInitializer extends AbstractAnn ...

  8. JavaWeb笔记(九)Ajax&Json

    AJAX 实现方式 原生的JS实现方式 //1.创建核心对象 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, C ...

  9. (总结)Nginx使用的php-fpm的两种进程管理方式及优化

    PS:前段时间配置php-fpm的时候,无意中发现原来它还有两种进程管理方式.与Apache类似,它的进程数也是可以根据设置分为动态和静态的. php-fpm目前主要又两个分支,分别对应于php-5. ...

  10. 【bzoj1070】[SCOI2007]修车 最小费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6798411.html 题目描述 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的 ...