简体   繁体   中英

sql (MonetDB) commands from Python stalling

I scripted in Python SQL calls to my MonetDB server (which I verify is running, of course). When I print the calls instead of calling them, the commands look OK, but if I run the original script, it does not crash, it does use the CPU and memory, but nothing is changed in the database, not even the first line is executed. Why?

The Python script looks like this:

# script to merge tables in MonetDB
import re

from monetdb import mapi
server = mapi.Server()
server.connect(hostname="localhost", port=50000, username="monetdb", password="monetdb", database="dbfarm", language="sql")

def tablemerge(stub,yearlist):
    for year in yearlist:
#        server.cmd('ALTER TABLE %s_%d ADD COLUMN "year" INTEGER DEFAULT %d;' % (stub,year,year))
        print 'ALTER TABLE %s_%d ADD COLUMN "year" INTEGER DEFAULT %d;' % (stub,year,year)
        newstub = re.sub(r'sys.ds_chocker_lev_', r'', stub)
        if year == yearlist[0]:
            unioncall = 'CREATE TABLE %s AS SELECT * FROM %s_%d ' % (newstub,stub,year)
        else:
            unioncall += 'UNION ALL SELECT * FROM %s_%d ' % (stub,year)
    unioncall += ';'
    server.cmd(unioncall)
#    print unioncall
    for year in yearlist:
        server.cmd('DROP TABLE %s_%d;' % (stub,year))
#        print 'DROP TABLE %s_%d;' % (stub,year)
    print '%s done.' % stub
for stub in ['civandr']:
    tablemerge('sys.ds_chocker_lev_%s' % stub,xrange(1998,2013))

Eg the first call would be:

ALTER TABLE sys.ds_chocker_lev_civandr_1998 ADD COLUMN "year" INTEGER DEFAULT 1998;

But not even this happens. There is no year column in the table.

Or could I run the script in the console with more output than what I print myself?

Do commit ! By default, the autocommit parameter is set to False. You can either do:

server.connect(hostname="localhost", port=50000, username="monetdb", password="monetdb", database="dbfarm", language="sql", autocommit=True )

or simply run: connection.commit()

 connection = monetdb.sql.connect(username=username,password=password,hostname=hostname,port=port,database=database) cursor = connection.cursor() cursor.execute('create table test (id int, name varchar(50));') connection.commit() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM