简体   繁体   English

Python Access odbc,应跳过该行

[英]Python Access odbc and should skip the row

I am using MS Access to import the table information. 我正在使用MS Access导入表信息。 Table is below: 下表如下:

Field1   Field2
U1a14    High Speed Link
U0001    Medium speed data
U0022    Low Speed Link 

My code value is like: code = U1a14 code = U0001 code = B11DB (This code is not present in table) 我的代码值就像: code = U1a14 code = U0001 code = B11DB (This code is not present in table)

I am connecting to odbc and searching for code in table. 我正在连接到odbc并在表中搜索代码。

connection = pypyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;UserCommitSync=Yes;SafeTransactions=0;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=C:\PYTHON27;DBQ=C:\PYTHON27\iso14229dtcs.mdb;')
cursor1 = connection.cursor()
cursor1.execute("SELECT Field2 FROM Table Where Field1 = '{}'".format(code))

for row in cursor1.fetchone():
  print row

I am getting error: 我收到错误消息:

error:
for row in cursor1.fetchone():
TypeError: 'NoneType' object is not iterable

How Can I fix it, If the code is not present in my Table, it should skip and print only existing row 我该如何解决它,如果我的表中没有代码,它应该跳过并仅打印现有行

Change the code to the following, check cursor1.fetchone() is not NULL. 将代码更改为以下代码,检查cursor1.fetchone()不为NULL。

    connection = pypyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;UserCommitSync=Yes;SafeTransactions=0;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=C:\PYTHON27;DBQ=C:\PYTHON27\iso14229dtcs.mdb;')
    cursor1 = connection.cursor()
    cursor1.execute("SELECT Field2 FROM Table Where Field1 = '{}'".format(code))
    row = cursor1.fetchone()
    while row is not None:
        print row[0], row[1]
        row = cursor1.fetchone()
    cursor1.close()
    connection.close()

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

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