Python停止的sql(MonetDB)命令

时间:2014-05-27 12:16:58

标签: python sql monetdb

我用Python SQL调用脚本编写了我的MonetDB服务器(当然我验证了它正在运行)。当我打印调用而不是调用它们时,命令看起来没问题,但如果我运行原始脚本,它不会崩溃,它会使用CPU和内存,但数据库中没有任何更改,甚至第一行也没有执行。为什么呢?

Python脚本如下所示:

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

E.g。第一个电话会是:

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

但即便如此。表中没有year列。

或者我可以在控制台中运行脚本,输出的输出比我自己打印的更多吗?

1 个答案:

答案 0 :(得分:1)

提交!默认情况下,autocommit参数设置为False。你可以这样做:

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

或只是运行: 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()