首页 > 解决方案 > SQL Server:pypodbc 插入空值

问题描述

我有以下插入语句的代码。

cursor = self.conn.cursor()
        print("New config with values: {0}, {1}".format(self.scale_factor, self.min_neighbours))
        cursor.execute("INSERT INTO config(scale_factor, min_neighbours) VALUES(?,?)", float(self.scale_factor), int(self.min_neighbours))
        self.conn.commit()
        record_id = cursor.execute('SELECT @@IDENTITY AS id;').fetchone()[0]
        if record_id:
            self.id = record_id
            print("Insertion succesfull. Generated id: {0}".format(record_id))
        else:
            print("Insertion failed.")

插入成功,但除了标识列没有插入任何数据。

程序输出:

New config with values: 0, 0.08
Config insertion succesfull. Inserted id: 176

从表中选择:

id  | scale_factor | min_neighbours
176 | 0.0000       |    0

从 SQL Profiler 插入:

INSERT INTO config(scale_factor, min_neighbours) VALUES(0.0,0)

有谁知道为什么这些值为零?我是蟒蛇的新手。

标签: pythonsql-serverpyodbc

解决方案


推荐阅读