python for mysql

# -*- coding: utf-8 -*-

‘‘‘
python coded by

written in 2016/8/31

Used for get win os log for each windows server
‘‘‘

‘‘‘
pymssql 帮助文档
http://pymssql.org/en/stable/pymssql_examples.html
‘‘‘

import pymssql
import MySQLdb
import time

#全局变量
host = "sqlmonitorex.mysql.db.aa.com"
user = "aa"
password = "aa"
dbname = "testdb"
port = 555

def fetch_row():
try:
#conn = pymssql.connect(host,port,user,password,dbname,charset="UTF-8",timeout=3)
conn = MySQLdb.connect(host,user,password,dbname,port=port,charset="utf8",connect_timeout=3)
cursor = conn.cursor()
sql = "select * from temp1;"
cursor.execute(sql)
row = cursor.fetchone() #相当于 cursor.next方法
while row:
print row[0],row[1] #格式化输出

#time.sleep(1) #每次输出等待1秒
row = cursor.fetchone() #输出下一行 cursor.next方法

conn.close()
return 1

except Exception,e:
return e

def change_row():
try:
conn = MySQLdb.connect(host,user,password,dbname,port=port,charset="utf8",connect_timeout=3)
cursor = conn.cursor()
#sql="insert into temp1 (id,name)values (1,‘xxx‘)"
sql="update temp1 set name=‘ZZZ‘ where id=1 "
cursor.execute(sql)
conn.commit()
conn.close()
return 1

except Exception,e:
conn.rollback()
return e

i=fetch_row()
print i
change_row()

时间: 2024-08-11 20:19:06

python for mysql的相关文章

python操作mysql ------- SqlAchemy正传

本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 下载安装 pip3 install pymysql 使用操作 1.执行SQL #!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql # 创建连接 conn = pymysql.connect(host='127.0.0.1

python操作mysql数据库

连接数据库 输入值 存入数据库 关闭 import string import mysql.connector conn=mysql.connector.connect(user='root',password='test',database='dalian',use_unicode=True) cursor=conn.cursor() a=raw_input('enter an id: ') b=raw_input('enter a name: ') while(a!='quit' or b!

Python 3 mysql 简介安装

Python 3 mysql 简介安装 一.数据库是什么 1.  什么是数据库(DataBase,简称DB) 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数字模型组织.描述和存储,具有较小的冗余,较高的数据独立性和易扩展性,并且可为各种用户共享. 2.什么是数据(Data) 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机 在计算机中

python操作MySQL

本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 下载安装 ? 1 pip3 install pymysql 使用操作 1.执行SQL + ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #!/usr/bin/env python # -*-

python 3 mysql sql逻辑查询语句执行顺序

python 3 mysql sql逻辑查询语句执行顺序 一 .SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN <right_table> ON <join_condition> WHERE <where_condition> GROUP BY <group_by_list> HAVING <havin

fedora python访问mysql

1 下载驱动库 http://sourceforge.net/projects/mysql-python/ 2, yum install mysql-dev* yum install python-dev* 3, python setup.py install fedora python访问mysql,布布扣,bubuko.com

Python使用MySQL数据库(新)

之前写过一篇 Python使用MySQL数据库的博客,主要使用的是Python2和MySQLdb驱动. python使用mysql数据库 然而,2016年开始,我从Python2切换到了Python3,Python2已经基本不再使用,MySQLdb驱动从2014年1月停止了维护.所以,打算重新再来写这篇博客. Python2 ---> Python3 MySQLdb --> PyMySQL 一,安装PyMySQL Python是编程语言,MySQL是数据库,它们是两种不同的技术:要想使Pyth

python使用mysql数据库

一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的linux 仓库中都会有mysql ,我们只需要通过一个命令就可以下载安装: Ubuntu\deepin >>sudo apt-get install mysql-server >>Sudo apt-get install  mysql-client centOS/redhat >

python 操作mysql

安装模块: #pip install .... MySQLdb(2.x) pymysql(3.x) import MySQLdb as sql con = sql.connect( host = "localhost", #host 主机,本机的主机名或者ip,指你连接的数据库的主机 user = "root", #user 登录数据库的用户名称 passwd="123456", #passwd 登录数据库用户名称对应的密码 db="m

python控制mysql的API手记

--------------------python控制mysql的API--------------------#import MySQLdb:引用对应的开发包#conn=MySQLdb.connect (host='localhost',user='root',passwd='root',db='test',port=3306):创建数据 库连接#cur=conn.cursor():创建游标 #cur.execute(self, query, args):执行单条sql语句,接收的参数为sq