我正在尝试使用MSSQL在本地服务器KHBW001
上的tempdb
数据库上创建一个表。我的代码是:
import pyodbc
connection = pyodbc.connect('Driver={SQL Server};'
'Server=KHBW001;'
'Database=tempdb;'
'Trusted_Connection=yes;')
cursor = connection.cursor()
cursor.executemany(
"CREATE TABLE tempdb.dbo.NewTestPyTable(Symbol varchar(15), Shares integer, Price double)") # creates new table
cursor.executemany("""
INSERT INTO tempdb.dbo.NewTestPyTable (Symbol, Shares, Price)
VALUES
[('ETH',55,199.55),
('KHC',66,33.5)]
""") # insert two records into new table
connection.commit()
我遇到了错误:
"创建表tempdb.dbo.newtestpytable(符号varchar(15(,共享 整数,价格double("(#创建新表格
typeError:函数精确需要2个参数(1给定(
我不太明白我在做什么错。请协助
弄清楚了...
import pyodbc
connection = pyodbc.connect('Driver={SQL Server};'
'Server=KHBW001;'
'Database=tempdb;'
'Trusted_Connection=yes;')
cursor = connection.cursor()
cursor.execute(
"CREATE TABLE NewTestPyTable(Symbol varchar(15), Shares integer, Price integer)") # creates new table
params = [('ETH', 55, 199),
('KHC', 66, 33)]
# insert two records into new table
cursor.executemany(
"INSERT INTO tempdb.dbo.NewTestPyTable (Symbol, Shares, Price) VALUES (?, ?, ?)", params)
connection.commit()
我认为第一个问题是在表创建中
这是文档如何正确创建它https://www.w3schools.com/sql/sql_create_table.asp
SQL中的数据类型https://www.journaldev.com/16774/sql-data-types
进一步看来您还需要以价格使用货币类型。
这就是我的方式:
import pyodbc
connection = pyodbc.connect('Driver={SQL Server};'
'Server=KHBW001;'
'Database=tempdb;'
'Trusted_Connection=yes;')
cursor = connection.cursor()
cursor.executemany(
"CREATE TABLE tempdb.dbo.NewTestPyTable(Symbol varchar(15), Shares int, Price money)") # creates new table
cursor.executemany("""
INSERT INTO tempdb.dbo.NewTestPyTable (Symbol, Shares, Price)
VALUES
('ETH',55,199.55),
('KHC',66,33.5);
""") # insert two records into new table
connection.commit()