Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > SQLite

Reply
Thread Tools

SQLite

 
 
Matthias Huening
Guest
Posts: n/a
 
      10-03-2008
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
 
Reply With Quote
 
 
 
 
gordyt
Guest
Posts: n/a
 
      10-03-2008
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
 
Reply With Quote
 
 
 
 
Petite Abeille
Guest
Posts: n/a
 
      10-03-2008

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/
 
Reply With Quote
 
Matthias Huening
Guest
Posts: n/a
 
      10-03-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby sqlite/gem error: Could not load sqlite adapter jhs408@gmail.com Ruby 4 04-18-2009 12:53 AM
Installing sqlite-ruby when sqlite is in non-standard location Carl Youngblood Ruby 1 04-09-2005 03:32 AM
SQLite or other "DB in a file" for Java? Andrew Fabbro Java 2 06-19-2004 06:11 PM
Sqlite rj Java 0 08-16-2003 10:59 PM
Re: Java JDBC to SQLite =?ISO-8859-1?Q?Andree_Gro=DFe?= Java 0 08-15-2003 12:57 PM



Advertisments