如何在python中构建动态sql查询并使用executemany()来插入?

时间:2015-08-18 18:00:01

标签: python mysql

在我的代码中,我从MySQL表中选择10 000行数据,并一次构建一个包含100行的列表。我将数据插入另一台服务器上的表中。我希望能够将此代码用于具有不同值和列的表。我怎么能这样做?

这是我到目前为止的代码:

while outerIndex<outerLoops:

    ts1 = time.time()
    ts2 = 0

    sqlReadRows = 'SELECT * FROM `user_session_0805` WHERE record_time >=%s AND record_time < %s ORDER BY record_time ASC LIMIT %s'
    readCur.execute(sqlReadRows,(startTime, maxTime, selectLimit)) 

    dataResults = readCur.fetchall()
    innerLoops = len(dataResults)
    innerIndex = 0

    batch = []
    while innerIndex<innerLoops:
        if len(dataResults) <100:
            for row in dataResults:
                if row:
                    batch.append(
                        row
                    )
        else:

            for i in range(innerIndex, innerIndex+100):
                if dataResults[i]:
                    batch.append(
                        dataResults
                    )
                else:
                    break

        innerIndex+=100

    sqlWrite = # Generate sql

    if batch:
        writeCur.executemany(sqlWrite, batch)
        cnx2.commit()
        startTime = batch[-1][1]
    ts2 = time.time()
    print 'Took %s secs to insert %s rows of data'%(int(ts2-ts1), len(batch))

    outerIndex+=1   

我对Python很新,所以我也很感激任何有用的建议!

2 个答案:

答案 0 :(得分:0)

您不对数据进行任何修改,对吗?您可以将SELECT结果直接插入新表:

// from the app.post 
sshuser = req.body.username;
sshhost = req.body.hostname;
password = req.body.password;


conn.on('message', function(msg) {

    var data = JSON.parse(msg.utf8Data);
    if (!term) {
       term = pty.spawn('ssh', [sshuser +'@'+ sshhost], {
                name: 'xterm-256color',
                cols: 80,
                rows: 30
            });

        // Password Input         
        setTimeout(function(){
                term.write(password);
                term.write('\r');},3000);

您不必首先创建INSERT INTO newTable SELECT * FROM `user_session_0805` WHERE record_time >=%s AND record_time < %s ORDER BY record_time ASC LIMIT %s ,MySQL将为您处理此问题。但你应该做一个

newTable

<强>更新

您可以使用

将值导出到csv文件
DROP TABLE IF EXISTS newTable;

答案 1 :(得分:0)

我最后用这个编写了sql:

if batch:
        key =""
        key = '({})'.format(','.join(elem for elem in batch[0]))
        print key
        value ='({})'.format(','.join("'"+str(eleme)+"'" for eleme in batch[0].values()))
        print value
        sqlWrite = []
        sqlWrite.append("INSERT IGNORE INTO %s " %writeTable)
        sqlWrite.append("".join(key))
        sqlWrite.append(" VALUES ")
        sqlWrite.append("".join(value))
        sql="".join(sqlWrite)
        print sql
        if batch:
            writeCur.executemany(sql, batch)
            cnx2.commit()