Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';t使用Python在表中插入或在MySQL中创建表_Php_Python_Mysql - Fatal编程技术网

Php Can';t使用Python在表中插入或在MySQL中创建表

Php Can';t使用Python在表中插入或在MySQL中创建表,php,python,mysql,Php,Python,Mysql,我有一个arduino uno和temp36。我用Python从arduino串行监视器读取传感器值。它起作用了 但是我不能使用Python在创建的MySQL表中插入,我不知道为什么。我没有错误或警告 import serial import time import MySQLdb dbhost = 'localhost' dbname = 'test' dbuser = 'X' dbpass = 'Y' ser = serial.Serial('COM7',9600,timeout=1) #

我有一个arduino uno和temp36。我用Python从arduino串行监视器读取传感器值。它起作用了

但是我不能使用Python在创建的MySQL表中插入,我不知道为什么。我没有错误或警告

import serial
import time
import MySQLdb

dbhost = 'localhost'
dbname = 'test'
dbuser = 'X'
dbpass = 'Y'
ser = serial.Serial('COM7',9600,timeout=1) # On Ubuntu systems, /dev/ttyACM0 is the default path to the serial device on Arduinos, yours is likely different.
while 1:
time.sleep(1)
the_goods = ser.readline()
str_parts = the_goods.split(' ')
conn = MySQLdb.connect (host = dbhost,
                user = dbuser,
                passwd = dbpass,
                db = dbname)
cursor = conn.cursor ()
sql = "INSERT INTO arduino_temp (temperature) VALUES ('%s');" % (str_parts[0])
print "Number of rows inserted: %d" % cursor.rowcount
try:
    cursor.execute(sql)
except:
    pass
cursor.close ()
conn.autocommit=True
conn.close ()
print the_goods
我做错什么了吗

SQL表中的这些值需要在php中打印(实时打印)


硬件:windows 7、Python 2.7、Python编辑器:Pyscripter、arduino uno、MySQL工作台执行sql语句后必须提交:

cursor.execute(sql)
cursor.commit()

或者在建立连接后设置
autocommit=True

如果您打开数据库连接,它最初是以
autocommit=False
模式打开的,因此在执行SQL语句后,您必须
commit()
。也不要在异常情况下传递
。这样你就不会看到问题。您可以将异常记录到文件中。在执行INSERT语句之前,还可以使用
cursor.rowcount
。在它之后使用它

从您的评论来看,您似乎没有定义
arduino\u temp
数据库。我没有MySQL数据库,但我已经用PostgreSQL和ODBC驱动程序测试了这些代码。您可以使用它来测试数据库和驱动程序:

import MySQLdb
import traceback


def ensure_table(conn):
    cursor = conn.cursor()
    try:
        cursor.execute('SELECT COUNT(*) FROM arduino_temp')
        for txt in cursor.fetchall():
            print('Number of already inserted temepratures: %s' % (txt[0]))
    except:
        s = traceback.format_exc()
        print('Problem while counting records:\n%s\n' % (s))
        print('Creating arduino_temp table ...')
        # table arduino_temp does not exist
        # you have to define table there
        # you will probably have to add some columns as:
        #  test_id SERIAL primary key,
        #  test_date timestamp default CURRENT_TIMESTAMP,
        # and add index on test_date
        cursor.execute('CREATE TABLE arduino_temp (temperature integer)')
        print('table created')
    cursor.close()


def insert_temperature(conn, temperature):
    cursor = conn.cursor()
    #cursor.execute("INSERT INTO arduino_temp (temperature) VALUES (?)", (temperature, ))
    #cursor.execute("INSERT INTO arduino_temp (temperature) VALUES (%s)", (temperature, ))
    cursor.execute("INSERT INTO arduino_temp (temperature) VALUES (%d)" % (int(temperature)))
    print("Number of rows inserted: %d" % (cursor.rowcount))
    conn.commit()
    cursor.close()


def main():
    # ...
    conn = MySQLdb.connect (host = dbhost, user = dbuser, passwd = dbpass, db = dbname)
    #conn = pyodbc.connect('DSN=isof_test;uid=dbuser;pwd=dbpass')
    ensure_table(conn)
    insert_temperature(conn, 10)


main()

如果数据库有问题,请创建测试代码并使用小函数。您的代码从串行接口读取温度并将其插入数据库。为每个操作创建单独的功能。

您需要从删除
开始尝试
/
,但不包括
全面异常处理。您的
autocommit
更改来得太晚,无法应用于insert语句。您不应使用字符串格式的sql命令,而应使用占位符:
cursor.execute('insert INTO arduino_temp(温度)值(%s)',str_parts[0])
非常感谢您的响应。我收到一个错误:programmingerror:“Table test.arduino_temp不存在。”该错误的含义是什么?OP确实试图设置
autocommit=True
,但这是否太晚了。非常感谢您的回复。我得到一个错误:programmingerror:“Table test.arduino_temp不存在。”这个错误是什么意思?它意味着在
test
数据库中没有
arduino_temp
表。您连接到错误的数据库或拼写错误的表名,或者您必须创建
arduino\u temp
表。非常感谢您的回答。但它不起作用。如果您对数据库有问题,请创建测试代码并使用小函数。您的代码从串行接口读取温度并将其插入数据库。为每个操作制定单独的功能。向我们显示错误信息。确保
arduino\u temp
表在数据库中。您好,我决定在phpmyadmin中创建一个新表。但是只有第一个数据从phpMyadmin写入MysqlTable。我不知道为什么。它不适用于工作台。我在python或mysql中没有错误。