【问题标题】:how to use value from a mssql query in a sqlite query如何在 sqlite 查询中使用 mssql 查询中的值
【发布时间】:2016-04-18 13:10:53
【问题描述】:

我有两张表,一张在 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'

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

【问题讨论】:

    标签: python-3.x sqlite pypyodbc


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多