简体   繁体   English

Python停顿的sql(MonetDB)命令

[英]sql (MonetDB) commands from Python stalling

I scripted in Python SQL calls to my MonetDB server (which I verify is running, of course). 我编写了对我的MonetDB服务器的Python SQL调用的脚本(当然,我确认该服务器正在运行)。 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. 当我打印调用而不是调用时,命令看起来不错,但是如果我运行原始脚本,它不会崩溃,它确实使用了CPU和内存,但是数据库中没有任何变化,甚至第一行都没有。执行。 Why? 为什么?

The Python script looks like this: 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))

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. 表格中没有year列。

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. 默认情况下,autocommit参数设置为False。 You can either do: 您可以执行以下任一操作:

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

or simply run: connection.commit() 或简单地运行: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() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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