for abc in cursor: NameError: name 'cursor' is not defined (data migration)

for abc in cursor: NameError: name 'cursor' is not defined (data migration)

我正在尝试将我的数据从 MongoDB 迁移到 Mysql,我的数据有超过 100 万条记录。 我为此使用了 Studio 3T,但它将记录限制为 1000 条记录(好吧,我们没有购买高级版本的预算)。因此,我尝试在 post 之后使用 python:“http://www.youngdba.com/2017/03/migrating-mongodb-data-to-mysql-using.html”.

一切顺利,直到最后一部分 python shell returns 错误:

cursor1= db.cursor()
 i=1
 for abc in cursor:
    uids=int(abc.get("uids"))
    fnames=abc.get("first_name")
    sql= "insert into client_test(uid,first_name) values"+'('+str(uids)+",'"+fnames+"')"
    print ("Inserted"+str(i)+" record")
    i+=1
    Number_of_rows=cursor1.execute(sql)

回溯(最近调用最后): 文件“”,第 1 行,位于 对于光标中的 abc: NameError: 名称 'cursor' 未定义

为什么代码不起作用?我在这里错过了什么吗? 请帮忙!提前致谢!

你的 for 循环应该是:

for abc in cursor1:

没有名为 cursor 的变量,只有 cursor1。您缺少变量名称中的 1。