0

I'm trying to create a table in sql server database using PYMSSQL ,below the script I use:

from os import getenv
import pymssql

server = getenv("SQLSERVER")
user = getenv("USER")
password = getenv("MYPWD")

cnxn= pymssql.connect(server, user, password, "tempdb")
cursor = cnxn.cursor()
cursor.execute("""
IF OBJECT_ID('persons', 'U') IS NOT NULL
    DROP TABLE persons
CREATE TABLE persons (
    id INT NOT NULL,
    name VARCHAR(100),
    salesrep VARCHAR(100),
    PRIMARY KEY(id)
)
""")
cursor.executemany(
    "INSERT INTO persons VALUES (%d, %s, %s)",
    [(1, 'ME', 'OTHERME'),
     (2, 'ANOTHERGUY', 'GUY'),
     (3, 'NAME', 'SURNAME')])
cnxn.commit()

But I keep getting the error below

----> 2 import pymssql

ModuleNotFoundError: No module named 'pymssql'

any hints, ideas or solutions :)

Mohamed Azizi
  • 124
  • 1
  • 3
  • 13
  • Did you install pymssql? Try using `pip freeze` in the command line and make sure that it is listed – Kevin K. Aug 15 '18 at 13:31
  • This seems like a really bad approach to handling data. You are creating a persistent table in tempdb. This indicates you are using this table as a temporary storage solution. But it has concurrency issues like crazy. If you have two people running this the second one will drop the original users table and populate it with new data. I think the real issue is your approach, not the error message you are receiving. A stored proc with a temp table is likely a better approach but hard to know for sure without more details. – Sean Lange Aug 15 '18 at 14:19
  • @KevinK. I did `pip freeze` but I can't see the pymssql – Mohamed Azizi Aug 15 '18 at 14:32
  • @SeanLange it's just learning purposes, that's why I use tempdb – Mohamed Azizi Aug 15 '18 at 14:33
  • Then why not use a temp table and a stored proc? I would prefer to learn doing things in a way that is better suited to how it would be done in the real world. – Sean Lange Aug 15 '18 at 14:49

2 Answers2

0

Try:

pip install pymssql

If you already did this and it still does not work, maybe it is not recognizing pymssql in the python path. Try adding the module's directory into the PYTHONPATH.

Permanently add a directory to PYTHONPATH

Hope it helps!

Carlos Bettin
  • 83
  • 2
  • 10
  • I did try to install but i didn't work, no error message no failed message – Mohamed Azizi Aug 15 '18 at 14:36
  • The error "ModuleNotFoundError"' appears if there is no module with this name installed. When you try to install the module with pip or whatever, what does it appear? – Carlos Bettin Aug 15 '18 at 14:43
  • As I stated in the previous comment, No message error appears or success message, just run and give me the line to write another command – Mohamed Azizi Aug 15 '18 at 14:47
0

pymssql is no longer being maintained so use pyodbc instead. This allows inserting many value tuples in one execute by passing a tuple of tuples as values. Much faster than executemany.

tfranch
  • 1
  • 2