0

我有两个表,一个在 ms sql 中,另一个在 sqlite3 我需要将 mssql 表中的一个字段更新为 sqlite3 表中的相关值。

这是我的代码:

import pypyodbc
import sqlite3

connection = pypyodbc.connect('Driver={SQL Server};'
                              'Server=****'
                              'Database=****;'
                              'uid=****;pwd=****')

cursor = connection.cursor()
SQLCommand = ("select ObjectId, ObjectColumnName,Label from LocalizedLabel "
              "where LanguageId = 1065")

sqlUpdate = ("UPDATE LocalizedLabel "
             "SET Label = ? "
             "WHERE ObjectId = ? and ObjectColumnName = ? and LanguageId = 1065")

cursor.execute(SQLCommand)
results = cursor.fetchall()

srcDB = sqlite3.connect('crmLLDB')

for result in results:
    val = (result[0], result[1])
    print('SELECT "1065" FROM crm '
          'WHERE UPPER(ObjectID)= UPPER(%s) AND ObjectColumnName = %s' % val)
    srcCursor = srcDB.execute('SELECT "1065" FROM crm '
                              'WHERE UPPER(ObjectID)= UPPER(?) AND ObjectColumnName = ?', val)
    if srcCursor.fetchone() is None:
        print("No translation found for " + result[2])
    else:
        translation = srcCursor.fetchone()[0]
        updateVals = (translation, result[0], result[1])
        cursor.execute(sqlUpdate, updateVals)


connection.close()
srcDB.close()

objectId 是一个GuID,其他字段都是字符串

打印函数返回:

SELECT "1065" FROM crm WHERE UPPER(ObjectID)= UPPER(b'0340B506-2341-DB11-898A-0007E9E17EBD') AND ObjectColumnName = DisplayName

而观察者给出:

result[0] = 'b\\'0340B506-2341-DB11-898A-0007E9E17EBD\\''
result[1] = 'DisplayName'

这个查询当然会返回这个错误:

[1] [SQLITE_ERROR] SQL error or missing database (near "'0340B506-2341-DB11-898A-0007E9E17EBD'": syntax error)

而这个查询:

SELECT "1065" FROM crm WHERE UPPER(ObjectID)= UPPER('0340B506-2341-DB11-898A-0007E9E17EBD') AND ObjectColumnName = 'DisplayName'

返回完美答案,有人可以指出我的问题吗?

4

1 回答 1

1

显然问题出pypyodbc在GuId上,我用过pyodbc,现在一切正常!

我的最终代码,如果将来某个 googler 路过:

import pyodbc
import sqlite3

startTme = datetime.now()
connection = pyodbc.connect('Driver={SQL Server};'
                            'Server=*****;'
                            'Database=****;'
                            'uid=sa;pwd=*****')

cursor = connection.cursor()
SQLCommand = ("select ObjectId, ObjectColumnName,Label from LocalizedLabel "
              "where LanguageId = 1065")

sqlUpdate_p = ("UPDATE LocalizedLabel "
               "SET Label = ? "
               "WHERE ObjectId = ? and ObjectColumnName = ? and LanguageId = 1065")

cursor.execute(SQLCommand)
results = cursor.fetchall()

srcDB = sqlite3.connect('crmLLDB')

jobLength = str(len(results))
i = 0

for result in results:
    val = (result[0], result[1])
    srcCursor = srcDB.execute('SELECT "1065" FROM crm '
                              'WHERE UPPER(ObjectID)= UPPER(?) AND ObjectColumnName = ?', val)
    trans = srcCursor.fetchall()
    for tr in trans:
        updateVals = (tr[0], result[0], result[1])
        cursor.execute(sqlUpdate_p, updateVals)
        i += 1

connection.commit()
connection.close()
srcDB.close()
于 2016-04-19T06:14:21.607 回答