0

I'm trying to create a table on a tempdb database on a local server KHBW001 using MSSQL. My code is:

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()  

I'm getting the error:

"CREATE TABLE tempdb.dbo.NewTestPyTable(Symbol varchar(15), Shares integer, Price double)") # creates new table

TypeError: function takes exactly 2 arguments (1 given)

I don't quite understand what I'm doing wrong. Please assist

7
  • you should be using executemany as opposed to execute --> github.com/mkleehammer/pyodbc/wiki/… Commented May 26, 2019 at 12:28
  • I tried that, didn't work @aws_apprentice Commented May 26, 2019 at 12:37
  • wrap your sql statement in 3 double quotes as opposed to three single ticks so that your string column can be properly formatted, also put your parameters in a list as per how the GH shows Commented May 26, 2019 at 12:38
  • I did that... now i'm getting the error TypeError: function takes exactly 2 arguments (1 given) Commented May 26, 2019 at 12:44
  • how about you update your post with what you have tried, because I nor any one else can see how you attempted this... Commented May 26, 2019 at 12:44

2 Answers 2

1

Figured it out...

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()
Sign up to request clarification or add additional context in comments.

Comments

0

i think first of all the problem is in table creation

here is the documentation how to create it correctly https://www.w3schools.com/sql/sql_create_table.asp

type of data in SQL https://www.journaldev.com/16774/sql-data-types

further it seems to me you also need to use the money type for the price.

that's how i would do:

    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()  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.