Python Forum
Comparing the count of rows from the tables that are present in two different databas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing the count of rows from the tables that are present in two different databas
#1
Say for example I have a database named A and B with same table name and with different set of records. I want to compare the count of the table 1 that is present in Database A and B.

Database A has 190 tables and Database B has 190 tables with same table names. But table 1 has 40 records in Database A and table 1 in Database 2 has 30 records.

As of now I am able to do that in SQL server with this query. select (select count() from A.table1) as Count1, (select count() from B.table1) as Count2

But I am trying to do the same in Python. How can I connect to the two databases with the same conn object and use it to count the table records in different databases. I would also thankful if someone can help me to automate these for all the tables in both the databases.Thank you.
Reply
#2
show what you've tried (as code)
Reply
#3
import pyodbc
conn1=pyodbc.connect('Driver={SQL Server};'
                   'Server=[server name];'
                   'Database=A;'
                   'Trusted_Connection=yes;')
conn2=pyodbc.connect('Driver={SQL Server};'
                   'Server=[server name];'
                   'Database=B;'
                   'Trusted_Connection=yes;')
cursor=conn1.cursor()
cursor.execute('select count(*) from A.table1')
for row in cursor:
    print(row)
Reply
#4
what errors (or results) --- please use BBcode tags
Reply
#5
Hey Larz,

If you have gone through my code, the code is connecting to two tables in different databases using two con objects. There would be no error. But I cannot achieve the functionality I am looking for that is I cannot query from two tables using two different connection objects. So I want some help in getting a single con object where it can connect to the two different databases.

Thank you
Reply
#6
which database are you using?
is there a reason why you are using ODBC?
Reply
#7
I am trying to connect to sql server...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,268 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Check if a value is present in each group Menthix 8 2,592 May-16-2022, 12:25 PM
Last Post: Menthix
  Anaconda pip install mpyc Error pip-script.py is not present Anldra12 2 7,725 Dec-13-2021, 06:59 PM
Last Post: Anldra12
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,603 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Slittping table into Multiple tables by rows drunkenneo 1 2,004 Oct-06-2021, 03:17 PM
Last Post: snippsat
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,083 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,963 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  len(row) does not count rows correctly quest_ 3 2,198 Jan-23-2021, 10:38 AM
Last Post: quest_
  No Scripts File present after python installation ag2207 5 4,769 Jul-30-2020, 11:11 AM
Last Post: buran
  Excel tables and move rows jdos 6 4,456 Jul-02-2020, 05:52 AM
Last Post: jdos

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020