Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72741

How to use SQLite (sqlite3) more efficiently

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ps16thypresenceisfullnessofjoy@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'interfaces': 0.04; '(even': 0.05; 'filename:fname piece:py': 0.07; '%s"': 0.09; 'apis': 0.09; 'here?': 0.09; 'parameter': 0.09; 'suggestions.': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'posted': 0.15; 'windows': 0.15; '"making': 0.16; '"python",': 0.16; '__future__': 0.16; 'attempted': 0.16; 'exit.': 0.16; 'name):': 0.16; 'operation,': 0.16; 'sqlite': 0.16; 'stringio': 0.16; 'subject:sqlite3': 0.16; 'wxpython': 0.16; 'language': 0.16; 'code.': 0.18; 'do.': 0.18; 'first.': 0.19; 'pointed': 0.19; 'split': 0.19; 'code,': 0.22; 'select': 0.22; 'import': 0.22; 'load': 0.23; 'header:User-Agent:1': 0.23; "aren't": 0.24; 'exists': 0.24; 'necessary.': 0.24; 'fairly': 0.24; 'helpful': 0.24; "i've": 0.25; 'primary': 0.26; 'somewhere': 0.26; 'installed': 0.27; 'function': 0.29; 'correct': 0.29; 'subject:) ': 0.29; 'xml': 0.29; "doesn't": 0.30; 'skip:( 20': 0.30; '(which': 0.31; 'program,': 0.31; 'code': 0.31; 'easier': 0.31; 'easy,': 0.31; 'anyone': 0.31; 'class': 0.32; 'there.': 0.32; 'figure': 0.32; 'languages': 0.32; 'text': 0.33; 'url:python': 0.33; 'checking': 0.33; 'actual': 0.34; 'table': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'could': 0.34; 'subject: (': 0.35; 'agree': 0.35; 'connection': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'editor': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'google': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'c++': 0.36; 'object,': 0.36; 'replies': 0.36; 'skip:> 10': 0.36; 'doing': 0.36; "i'll": 0.36; 'url:org': 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'list': 0.37; 'message-id:@gmail.com': 0.38; 'thank': 0.38; 'rich': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'mailing': 0.39; 'called': 0.40; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'easy': 0.60; 'blank': 0.60; 'transaction.': 0.60; 'new': 0.61; "you're": 0.61; 'save': 0.62; 'show': 0.63; 'information': 0.63; 'hear': 0.63; 'subject:more': 0.64; 'more': 0.64; 'charset:windows-1252': 0.65; 'here': 0.66; 'sample': 0.67; 'close': 0.67; 'improvements': 0.68; 'useful.': 0.68; 'saving': 0.69; 'hey,': 0.75; 'saw': 0.77; '8.1': 0.84; ':).': 0.84; 'delete,': 0.84; 'paradox': 0.84; 'timothy': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type; bh=j0D2kC+hBhzIaLS3sIu88DGrLsz1v4wN4N9/BrpqKME=; b=U3mq5MgX0NdjOluVOQHUDhjO+ABkSIjtwO7UInYDi0+ZN227arsP60sisM6Aa0qyfu sUuZ4VulEO56yYnr3b2eJstwHOQ+8Ct1opXEGEijCcKQpCxRb62Koq2qsmw3VhTAKgpF rL1qXHawAHCAqI1rEJYNzxZ3x6sE5hD2hyX6If/fSDl/JSbdaDV3VVo56HrWhCT3cMba G2Y3YeLrwncM4Yy2r5G+KqQA8IHLTt5wuzrrKpdD2PQnTGW/pFyRhsGEtnKMWRrKoaK8 jge42CAL9W0bXG8wYQqcfQuUXWvq+7exfFLtNvrqXzK5FLXjUeioPmm6HrtU7P6wZFze AyCg==
X-Received by 10.229.127.9 with SMTP id e9mr14255509qcs.5.1401990132925; Thu, 05 Jun 2014 10:42:12 -0700 (PDT)
Date Thu, 05 Jun 2014 13:42:10 -0400
From R Johnson <ps16thypresenceisfullnessofjoy@gmail.com>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
MIME-Version 1.0
To python-list@python.org
Subject How to use SQLite (sqlite3) more efficiently
Content-Type multipart/mixed; boundary="------------050902080507040401050606"
X-Antivirus avast! (VPS 140605-0, 06/05/2014), Outbound message
X-Antivirus-Status Clean
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10754.1401990136.18130.python-list@python.org> (permalink)
Lines 134
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1401990136 news.xs4all.nl 2848 [2001:888:2000:d::a6]:59313
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72741

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Thank you all for your replies and suggestions.

To Chris's "two small points":
I saw that using the mailing list was recommended to several other 
people who posted here using Google Groups, so I thought it might be 
recommended to me as well sometime :). I'll try to use it from now on.
My code was tested on Python 2.7.6 on Windows 8.1 (and I just installed 
Python 2.7.7 yesterday).

 > There's a general principle in Python APIs that a "mode switch"
 > parameter isn't a good thing. Even more strongly, I would be very
 > surprised if attempting to set a blank description deleted the row
 > instead of setting the description to blank. My recommendation: Split
 > this into two functions, set_description and delete_language. In
 > delete_language, just unconditionally delete, don't bother checking
 > for the row's presence first.

I agree for the case of the sample code I showed here (which was really 
just a scaled-down version of some of the functions in my program). But 
in my actual program, I am using SQLite to load and save information 
from a wxPython GUI, where it's more practical to call a single save 
function. Below is the actual function (that's part of a class in my 
program):

def save_text(self):
     if not self.editor.IsModified():
         return
     if not self.editor.IsEmpty():
         stream = cStringIO.StringIO()
         self.editor.GetBuffer().SaveStream(stream,
             richtext.RICHTEXT_TYPE_XML)
         self.conn.execute("REPLACE INTO notes VALUES(?,?)",
             (self.db_key, stream.getvalue()))
         self.editor.SetModified(False)
     else:
         self.conn.execute("DELETE FROM notes WHERE topic=?",
             (self.db_key,))

(Even if you're not familiar with wxPython, it should be fairly easy to 
figure out what the code is doing. It's just saving some XML from a rich 
text editor to a StringIO object, and then to an SQLite database.)

 > > set_description(conn, "Assembly",
 > >     "Making Easy Things Very Hard & Hard Things Impossible")
 >
 > Hey, that's not fair! Assembly language makes some hard things really
 > easy, like segfaulting your process. Credit where it's due! :)

OK, I'll admit that I don't know Assembly :). How about the paradox 
"Making Easy Things Hard & Hard Things Easy"? Although that might make 
my description of C++ too unfair; suggestions for improvements to my 
language descriptions are welcome :).

 >        While /maybe/ not required for a SELECT operation, I'd put a
 >conn.commit() somewhere in there before the return(s). The standard for
 > Python DB-API interfaces is that auto-commit is turned off -- meaning the
 > SELECT has started a database transaction.

I don't exactly understand why conn.commit() should be called there. I 
thought it's only necessary to call it when the database has been 
changed, which a SELECT call doesn't do. Am I misunderstanding something 
here?

 > >        with conn:
 >
 >         This isn't really doing anything useful. You aren't opening a new
 > connection object, so there isn't really anything to close on block 
exit.

See 
https://docs.python.org/2/library/sqlite3.html#using-the-connection-as-a-context-manager. 
I removed it from my code, though, because it doesn't really seem necessary.

I've attached some new sample code in which I've attempted to correct 
various things that you mentioned. The links Peter pointed to were also 
helpful to show me some improvements I could make to my code. I'd be 
happy to hear any suggestions that anyone may have to improve the code 
further.

-- Timothy

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

How to use SQLite (sqlite3) more efficiently R Johnson <ps16thypresenceisfullnessofjoy@gmail.com> - 2014-06-05 13:42 -0400

csiph-web