python MySQL 插入数据

# -*- coding: utf-8 -*-
import MySQLdb

import time

#order: 0->quik 1->voice 1->temp 2->humi

def sql_thq(order, num):

    th_time = time.strftime('%H:%M',time.localtime(time.time()))
    q_time = time.strftime('%Y-%m-%d',time.localtime(time.time()))
    
    conn = MySQLdb.connect(host = 'localhost',  # 远程主机的ip地址, 
                               user = 'root',   # MySQL用户名
                               db = 'ssrbed',   # database名
                               passwd = 'helloworld',   # 数据库密码
                               port = 3306,  #数据库监听端口,默认3306
                               charset = "utf8")  #指定utf8编码的连接
    cursor = conn.cursor()  # 创建一个光标,然后通过光标执行sql语句
    sql = ""
    if(order == 1):
    
        #sql = "insert temperature(temp, currentTime) values('%s', '%s')"%(int(num), th_time)
        sql = "INSERT INTO `temperature` VALUE('%s', '%s') "%(int(num), th_time)
    elif order == 2:
        
        #sql = "insert humidity(humi, currenttime) values('%s', '%s')"%(int(num), th_time)
        sql = "INSERT INTO `humidity` VALUE('%s', '%s') "%(int(num), th_time)
    elif order == 0:
        
        #sql = "insert amplitude(ampl, currentTime) values('%s', '%s')"%(int(num), q_time)
        sql = "INSERT INTO `amplitude` VALUE('%s', '%s') "%(int(num), q_time)
    cursor.execute(sql)
    conn.commit() 
    cursor.close(); conn.close()  #最后记得关闭光标和连接,防止数据泄露

          
temp = [25,26,26,26,25,25,26,27];
hmui = [52,53,53,52,53,54,55,52];
count = 0
while True:
    for i in range(1, 3):
        if i == 1:
            sql_thq(1, temp[count%8])
        elif i == 2:
            sql_thq(2, hmui[count%8])
        count = count+1
time.sleep(3)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值