gpt4 book ai didi

python - 树莓派发送数据到XAMPP数据库

转载 作者:行者123 更新时间:2023-11-29 19:08:55 28 4
gpt4 key购买 nike

我在 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服务器安装在电脑上,我想在mysql数据库中查看树莓派的数据。那么为了建立连接,我应该在 host= "?"中写什么

最佳答案

连接字符串应该是这样的:

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

参见this Question

关于python - 树莓派发送数据到XAMPP数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43255524/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com