Marilyn Davis wrote: > #!/usr/bin/env python3 > """ > Hello Tutors, > > I can't figure out why the FillWithStars() function puts Canopus in the db > twice. > > What am I missing? > > Thank you for any help. > > Marilyn Davis > > p.s. That Reset(db_name) is in there so that you can run it over and over > if you want. > > --- > """ > import os, sqlite3, subprocess > > def Reset(db_name): > os.system("rm " + db_name) > db_process = subprocess.Popen(("sqlite3", db_name), > stdin=subprocess.PIPE, > stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > for send in (b'.tables', ): > returned = db_process.communicate(send) > assert returned == (b'', b'') > > def FillWithStars(): > > with sqlite3.connect("stars.db") as connection: > > connection.executescript(""" > CREATE TABLE brightest( > name, > constellation, > apparent_magnitude, > absolute_magnitude, > distance); > INSERT INTO brightest VALUES("Canopus", "Carina", -0.72, -2.5, > 74); > """) > > connection.executemany("INSERT INTO brightest VALUES(?, ?, ?, ?, > ?)", > [("Arcturus", "Bootes", -0.04, 0.2, 34),]) > > stored_stars = connection.execute("SELECT * FROM BRIGHTEST")
At this point the connection is closed, and it looks like stored_stars is still iterable but contains garbage. > for star in stored_stars: > print(star) Try and indent the lines above one more level so that they are executed inside the with-suite. > > def main(): > Reset("stars.db") > FillWithStars() > > if __name__ == '__main__': > main() > > """Output: > > bash-3.2$ ./why3.py > ('Canopus', 'Carina', -0.72, -2.5, 74) > ('Canopus', 'Carina', -0.72, -2.5, 74) > ('Arcturus', 'Bootes', -0.04, 0.2, 34) > bash-3.2$ > """ > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor