the-wondersmith / sqlalchemy-paradox

A SQLAlchemy dialect for the Microsoft Paradox DB ODBC Driver
Other
0 stars 0 forks source link

Insert into paradox .db #1

Open ScapeKP opened 3 years ago

ScapeKP commented 3 years ago

I'm trying to insert something into paradox .db file, but I get this error: TypeError: visit_insert() takes 2 positional arguments but 3 were given

I don't understand, what I'm doing wrong. I read docs and surf a lot, tried different code, but it doesn't work anyway....

My code:

import sqlalchemy_paradox
import sqlalchemy as db
from sqlalchemy import create_engine
from sqlalchemy import insert, select, update

engine = create_engine("paradox+pyodbc://@paradox", echo=False)
connection = engine.connect()
metadata = db.MetaData()
Test = db.Table('Test', metadata, autoload=True, autoload_with=engine)
list_values={'NAME':'John', 'FULLNAME':'John Smith'}
stmt=insert(Test).values(list_values)
connection.execute(stmt)`

Select statement works fine (and even update). And a little about my Test table. This code:

print(Test.c.keys())

stmt = 'SELECT * FROM Test'
results = connection.execute(stmt).fetchall()
print(results)

gives this results

['ID', 'NAME', 'FULLNAME']

[(1, 'My', 'My first')]

How to make insert work?

Test.db in attach Test.zip

gordthompson commented 3 years ago

FWIW, I just tried this …

import pyodbc

cnxn = pyodbc.connect("DSN=paradox", autocommit=True)
crsr = cnxn.cursor()
next_id = crsr.execute("SELECT MAX(ID) FROM Test").fetchval() + 1
print(f"inserting ID={next_id}")
sql = "INSERT INTO Test (ID, NAME, FULLNAME) VALUES (?, ?, ?)"
data = (next_id, f"name{next_id}", f"fullname{next_id}")
crsr.execute(sql, data)
print(crsr.execute("SELECT * FROM Test").fetchall())

… and although the SELECT was successful ("inserting ID=2") the INSERT failed with …

Traceback (most recent call last):
  File "C:/Users/Gord/Desktop/foo.py", line 9, in <module>
    crsr.execute(sql, data)
pyodbc.Error: ('HY000', '[HY000] [Microsoft][ODBC Paradox Driver] Operation must use an updateable query. (-3035) (SQLExecDirectW)')

… which doesn't look promising.

(However, I freely admit that I know nothing about the Paradox ODBC driver for Windows. The last time I used Paradox was version 3.5 for DOS, about 30 years ago.)