Home > OS >  Problem printing number of records added to database
Problem printing number of records added to database

Time:10-29

How do I print the total number of records added in the database? In my case it's the records of both Nation 1 and Nation 2. Each nation has 20 records, so the total is 40 records, but I only see 20 records added with my code cursor.rowcount (40 records are entered correctly in the database)

I try this, but it's not ok, it just tells me the record number of one of the two Nations, and not you both in their total (I would like Nation 1 Nation 2):

records_added_Nations = 0
print ("Record inserted successfully", cursor.rowcount)
records_added_Nations = records_added_Nations   1
cursor.close ()

The code of the added records is this:

#NATION 1   
sqlite_insert_query_Nation1 = 'INSERT INTO Nation (Name_Nation) VALUES (?);'
count_Nation1 = cursor.executemany(sqlite_insert_query_Nation1, Values_Nation1)
con.commit()

#NATION 2   
sqlite_insert_query_Nation2 = 'INSERT INTO Nation (Name_Nation) VALUES (?);'
count_Nation2 = cursor.executemany(sqlite_insert_query_Nation2, Values_Nation2)
con.commit()

CodePudding user response:

Assign cursor.rowcount after each operation, and total them.

#NATION 1   
sqlite_insert_query_Nation1 = 'INSERT INTO Nation (Name_Nation) VALUES (?);'
cursor.executemany(sqlite_insert_query_Nation1, Values_Nation1)
count_Nation1 = cursor.rowcount
con.commit()

#NATION 2   
sqlite_insert_query_Nation2 = 'INSERT INTO Nation (Name_Nation) VALUES (?);'
cursor.executemany(sqlite_insert_query_Nation2, Values_Nation2)
count_Nation2 = cursor.rowcount
con.commit()

print ("Records inserted successfully", count_Nation1   count_Nation2)
  • Related