Inserting row from database to another

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Inserting row from database to another



I am trying to insert a row from a database server to another database server in a different environment using pymysql


prod_conn = pymysql.connect(
prod_common_db_endpoint,
prod_user,
prod_password,
prod_common_table_name)

prod_cursor = prod_conn.cursor()


prod_ConnectFirm_table = prod_cursor.execute("select * from Common.ConnectFirm where FirmId = " + prod_source_firm + ";")
for row in prod_ConnectFirm_table:
ConnectFirmId = int(row[1])
ConnectFirmName = (row[2])
FirmId = int(row[3])
isDeleted = (row[4])
tags = (row[5])
createdDate = (row[6])
createdBy = int(row[7])
editedDate = (row[8])
editedBy = int(row[9])


conn = pymysql.connect(target_common_db_endpoint, user, password, common_schema_name)

cursor = conn.cursor()

cursor.execute("INSERT INTO Common.ConnectFirm(ConnectFirmId, ConnectFirmName, editedDate) VALUES(" + ConnectFirmId + ", " + ConnectFirmName + ", " + editedDate + ", ")")
conn.commit()
print("error inserting")



it fails giving me this error


Traceback (most recent call last):
File "sql_test_3_d.py", line 109, in <module>
for row in prod_orionConnectFirm_table:
TypeError: 'int' object is not iterable





Why are you connecting to db in every step of loop? Can you change the indentation?
– mad_
Aug 10 at 19:45






Dont convert your values to tuple. It will throw error while concatenating with other string objects
– mad_
Aug 10 at 19:48





@mad_ I am connecting to two databse servers and I am trying to have connection to both of them so I can select from one database and insert to the other one. I am not sure if I'm doing it the right way!
– Mina Gobrial
Aug 10 at 20:15




1 Answer
1



prod_ConnectFirm_table will contain execution status which is an integer value which you can't over.So after executing the query as :


prod_ConnectFirm_table = prod_cursor.execute("select * from Common.ConnectFirm where FirmId = " + prod_source_firm + ";")



you need to fetch data from cursor :


data = prod_cursor.fetchall()



"data" will contain the query result in form of tuple which you parse over.





I believe it is more of a comment rather than an answer.
– mad_
Aug 10 at 19:54





@mad_ it would have been best if you would have edited my answer or pointed out the mistake, i would love to improve the answer and probably would have kept few things in mind while answering next time, Thanks
– dilkash
Aug 10 at 20:08





@dilkash your suggestion eliminated the error of TypeError: 'int' object is not iterable, but I get a different one this time Traceback (most recent call last): File "sql_test_3_d.py", line 139, in <module> " + editedBy + ")") TypeError: must be str, not int
– Mina Gobrial
Aug 10 at 20:18






@MinaGobrial whats the code at line 139 in sql_test_3_d.py
– dilkash
Aug 10 at 20:22





" + editedBy + ")")
– Mina Gobrial
Aug 10 at 20:35






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard