Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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
Python Pymysql插入外键错误_Python_Mysql_Python 3.x - Fatal编程技术网

Python Pymysql插入外键错误

Python Pymysql插入外键错误,python,mysql,python-3.x,Python,Mysql,Python 3.x,我的餐桌上的天气 create table weather ( time timestamp default current_timestamp on update current_timestamp, id smallint, tpr float(3,1), wet float(3,1), uv tinyint(2), foreign key (id)

我的餐桌上的天气

create table weather (
            time timestamp default current_timestamp on update current_timestamp,
            id smallint,
            tpr float(3,1),
            wet float(3,1),
            uv tinyint(2),
            foreign key (id) references database.station(pk));
台站

CREATE TABLE station(
pk SMALLINT(5) NOT NULL AUTO_INCREMENT,
name VARCHAR(5),
lng DOUBLE(10,6),
lat DOUBLE(10,6),
PRIMARY KEY(pk));
当我使用pymysql将id插入天气时

我做了以下两个功能:

conn = pymysql.connect()#ignore the details
def get_id_from_station():
    sql = """SELECT pk FROM  station """
    conn.cursor().execute(sql)
    id = conn.cursor().fetchone()[0]
    weather_saved(id)

def weather_saved(id):
    #get the time,tpr,wet,uv value
    sql = """INSERT INTO weather (time,id,tpr,wet,uv) VALUES (%s,%s,%s,%s,%s)"""
    db.execute(sql, (time, id,tpr, wet, uv))
但是天气表没有更新


怎么了

以下是更新天气表的代码。我使用了Python2.7,但应该是相同的。我认为您的问题可能是您没有提交插入

import pymysql

conn = pymysql.connect(...)

def get_id_from_station():
    sql = """SELECT pk FROM  station """

    cursor = conn.cursor()
    cursor.execute(sql)
    row = cursor.fetchone()
    weather_saved(row)

import time
import datetime

def weather_saved(id):
    #get the time,tpr,wet,uv value
    sql = """INSERT INTO weather (time,id,tpr,wet,uv) VALUES (%s,%s,%s,%s,%s)"""
    cursor = conn.cursor()
    ts = time.time()
    timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    cursor.execute(sql, (timestamp, id,7, 7, 7))
    conn.commit()

get_id_from_station()