Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'beginner': 0.05; 'class,': 0.07; 'finally:': 0.07; 'subject:help': 0.08; '"__main__":': 0.09; '__init__': 0.09; '__name__': 0.09; 'main()': 0.09; 'parameter': 0.09; 'properly.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'runs': 0.10; 'python': 0.11; 'def': 0.12; 'itself.': 0.14; "wouldn't": 0.14; 'class:': 0.16; 'erroneously': 0.16; 'feedparser': 0.16; 'imports': 0.16; 'main():': 0.16; 'message-id:@web.de': 0.16; 'podcast': 0.16; 'received:188.174': 0.16; 'received:80.91.229.3': 0.16; 'received :mnet-online.de': 0.16; 'received:plane.gmane.org': 0.16; 'sqlite': 0.16; 'variable.': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'module': 0.19; 'command': 0.22; 'import': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'certainly': 0.24; 'exists': 0.24; "haven't": 0.24; 'question': 0.24; 'this:': 0.26; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'database,': 0.30; 'programming.': 0.30; 'work.': 0.31; 'included': 0.31; 'though.': 0.31; 'class': 0.32; 'probably': 0.32; 'thanks!': 0.32; 'open': 0.33; 'table': 0.34; 'could': 0.34; 'connection': 0.35; 'skip:s 30': 0.35; 'case,': 0.35; 'but': 0.35; 'add': 0.35; 'building': 0.35; '14,': 0.36; 'done': 0.36; 'method': 0.36; "i'll": 0.36; 'should': 0.36; 'list.': 0.37; 'skip:o 20': 0.38; 'feed': 0.38; 'question,': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'bad': 0.39; 'delete': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'most': 0.60; 'new': 0.61; 'simple': 0.61; 'first': 0.61; 'information': 0.63; 'more': 0.64; 'here': 0.66; 'started.': 0.68; 'chuck': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Sibylle Koczian Subject: Re: Sqlite3 help Date: Sat, 18 Oct 2014 12:27:37 +0200 References: <4a691d82-be63-4355-8898-0ecbd9cb16bd@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: ppp-188-174-183-19.dynamic.mnet-online.de User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <4a691d82-be63-4355-8898-0ecbd9cb16bd@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413628076 news.xs4all.nl 2928 [2001:888:2000:d::a6]:50840 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79266 Am 14.10.2014 15:36, schrieb Chuck: > I am building a simple podcast program where I download all the data from a feed with feedparser and store the data in sqlite3. I am spanking new to sqlite and database programming. Should I create the database in the __init__ method of my class, or is that a bad idea. I haven't done any designing that included databases. > > Thanks! > Chuck > When I first answered this question, I sent the answer erroneously to the OP and not to the list. So here it is again. On Tue, Oct 14, 2014 at 1:19 PM, Sibylle Koczian wrote: > > As I don't know anything at all about podcasts and feeds, I'll just > answer your question about creating the database in the __init__ > method of your class: > > That would destroy the existing database and create a new one without > any data. If you don't want to keep data from former runs of your > application, this wouldn't be necessary, because you could just > delete the data and keep the database. But most probably you want to > keep your data and add to it. In this case, you certainly don't want > to create a new database. > > The __init__ method of your class might be the right place to open > the database, though. > > You know that the documentation for the sqlite3 module is part of the > Python documentation? It contains links to the SQLite web page and to > a website with beginner information about SQL itself. So that should > help to get you started. Am 15.10.2014 18:51, schrieb Chuck Johnson: > > I was thinking that I could fix that by using ' ' ' CREATE TABLE IF > NOT EXISTS ' ' ' Should I make the sqlite3.connect() command a > global variable? I am not sure about how to design this properly. > That should work. I wouldn't make the connection a global variable. Use it as a parameter for every function that needs it. Roughly like this: import sqlite3 # other imports as needed def one_of_my_functions(myconn, my_other_parameters): mycurs = myconn.cursor() try: # do things with mycurs and my_other_parameters finally: mycurs.close() myconn.commit() # more of the same ... def main(): conn = sqlite3.connect('path/to/my/database') try: one_of_my_functions(conn, params) # more of the same finally: conn.close() if __name__ == "__main__": main() Or, if your program gets bigger, you might start and stop a connection inside a function instead of keeping one connection open for all of the application. HTH Sibylle