raspberry pi将数据发送到XAMPP数据库

时间:2017-04-06 12:40:49

标签: python mysql xampp

我在python中有以下代码用于将数据发送到mysql数据库

import time
import datetime
import MySQLdb
from time import strftime
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
PIR_PIN = 21
GPIO.setup(PIR_PIN, GPIO.IN)

# Variables for MySQL
db = MySQLdb.connect(host="*******",    user="root",passwd="*****", db="sensor1")
cur = db.cursor()

while True:

i = GPIO.input(PIR_PIN)
print i

datetimeWrite = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
print datetimeWrite

sql = ("""INSERT INTO templog (datetime,temperature) VALUES (%s,%s)""",(datetimeWrite,i))
try:
    print "Writing to database..."
    # Execute the SQL command
    cur.execute(*sql)
    # Commit your changes in the database
    db.commit()
    print "Write Complete"

except:
    # Rollback in case there is any error
    db.rollback()
    print "Failed writing to database"

cur.close()
db.close()
break

我的问题是我的XAMPP服务器安装在pc中,我想在mysql数据库中查看来自raspberry pi的数据。 为了建立连接,我应该在host ="?"

中写什么?

2 个答案:

答案 0 :(得分:0)

连接字符串应如下所示:

db = MySQLdb.connect(host="192.168.0.xxx",    user="root",passwd="*****", db="sensor1")

请参阅this Question

答案 1 :(得分:0)

主机将是安装XAMPP的系统的IP地址,例如192.168.x.x