python操作mysql如何使用变量

来源:互联网 发布:mac找不到启动磁盘 编辑:程序博客网 时间:2024/05/22 15:58
测试工作遇到一个场景:我们服务端向外部系统取数据,然后通过将一定级别的信息push告警出来,由于外部系统不可控性,
需要设计一个脚本,启动后,不断的PUSH 提供测试使用。
脚本使用MySQLdb向数据库插入数据,会使用到变量
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import time
import MySQLdb
def posttodb():
    var = 1
    while var == 1:
        try:
            conn = MySQLdb.connect(host='',user='',passwd='',db=') 
        except Exception, e:
            sys.exit()
     
    
        timestamp = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 
        print timestamp
        cursor = conn.cursor()
    
        content = '{\"a805658-70ed-449d-9eb4e9b3f\",\"S"standard_time\":\"'+timestamp+'\",bsaa"}'
        print content
        sql = "insert into notify_cache values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"%('8a8056d-449d-9ebf-8a78f64e9b3f',timestamp,'hhh,content,'0','0','','','1446586614',timestamp,'504849524549494549563240')
         try:
             cursor.execute(sql)
         except Exception, e:
             sys.exit(
        conn.commit();
        time.sleep(10)
if __name__ == '__main__':  
    posttodb()
0 0
原创粉丝点击