SQLite

Discussion in 'Python' started by Matthias Huening, Oct 3, 2008.

  1. Hi,

    This is probably trivial, but I cannot find a solution right now.
    With MySQLdb, I could test the success of a deleting action like this:

    sql = "DELETE FROM table WHERE name='xxx'"
    res = cur.execute(sql)
    if res == 1:
    print 'Okay'
    else:
    print 'nothing deleted'


    This seems not to work with sqlite3. How can I test whether deletion was
    sussessful or not?

    Thanks,
    Matthias
     
    Matthias Huening, Oct 3, 2008
    #1
    1. Advertising

  2. Matthias Huening

    gordyt Guest

    Howdy Matthias!

    The delete operation will set the rowcount member of your cursor.
    Let's assume you have an sqlite3 database with a table called 'test'
    with an id column. It does have a record with id=1. It does not have
    a record with id=2.

    >>> import sqlite3
    >>> connection = sqlite3.connect('test.sqlite3')
    >>> cursor = connection.cursor()
    >>> cursor.execute('delete from test where id=1')

    <sqlite3.Cursor object at 0x7f3450>
    >>> print cursor.rowcount

    1
    >>> cursor.execute('delete from test where id=2')

    <sqlite3.Cursor object at 0x7f3450>
    >>> print cursor.rowcount

    0
    >>> cursor.close()
    >>> connection.close()



    --gordy
     
    gordyt, Oct 3, 2008
    #2
    1. Advertising

  3. On Oct 3, 2008, at 2:33 PM, Matthias Huening wrote:

    > This seems not to work with sqlite3.


    Before going any further... make sure that SQLite's count_change is
    enabled:

    PRAGMA count_changes
    PRAGMA count_changes = 0 | 1

    "Query or change the count-changes flag. Normally, when the count-
    changes flag is not set, INSERT, UPDATE and DELETE statements return
    no data. When count-changes is set, each of these commands returns a
    single row of data consisting of one integer value - the number of
    rows inserted, modified or deleted by the command. "

    http://www.sqlite.org/pragma.html

    Cheers,

    --
    PA.
    http://alt.textdrive.com/nanoki/
     
    Petite Abeille, Oct 3, 2008
    #3
  4. Thanks!
    cursor.rowcount does exactly what I need.

    > Before going any further... make sure that SQLite's count_change is
    > enabled:
    >
    > PRAGMA count_changes
    > PRAGMA count_changes = 0 | 1
    >
    > "Query or change the count-changes flag. Normally, when the
    > count-changes flag is not set, INSERT, UPDATE and DELETE statements
    > return no data. When count-changes is set, each of these commands
    > returns a single row of data consisting of one integer value - the
    > number of rows inserted, modified or deleted by the command. "
    >


    Looks like this is not really necessary. My database hat count_changes
    disabled, but rowcount seems to work anyway. Are there any pitfalls?

    Matthias
     
    Matthias Huening, Oct 3, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Andree_Gro=DFe?=

    Re: Java JDBC to SQLite

    =?ISO-8859-1?Q?Andree_Gro=DFe?=, Aug 15, 2003, in forum: Java
    Replies:
    0
    Views:
    2,221
    =?ISO-8859-1?Q?Andree_Gro=DFe?=
    Aug 15, 2003
  2. rj

    Sqlite

    rj, Aug 16, 2003, in forum: Java
    Replies:
    0
    Views:
    565
  3. Andrew Fabbro
    Replies:
    2
    Views:
    14,859
    Michael Borgwardt
    Jun 19, 2004
  4. Carl Youngblood
    Replies:
    1
    Views:
    262
    Carl Youngblood
    Apr 9, 2005
  5. Replies:
    4
    Views:
    375
Loading...

Share This Page