473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sqlite3 trouble

I'm having some trouble with sqlite3 on my windows mobile device. Sometimes it happens that commit is not working.

Here is the code, that I'm using:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def OnSaveProfile(self, event):
  3.         naziv = self.edt_title.text
  4.         if not naziv:
  5.             gui.Message.ok(u"Error!", u"Enter profile title!", icon='error', parent=self)
  6.             return 0
  7.         potnik = self.edt_st_potn.text
  8.         if not potnik:
  9.             gui.Message.ok(u"Error!", u"Enter number of worker!", icon='error', parent=self)
  10.             return 0
  11.         baza = self.com_baza.text
  12.  
  13.         #Here I connect to the database
  14.         conn = orodja.povezava_na_bazo(self, "Nastavitve")
  15.         c = conn.cursor()
  16.         c.execute("Select nast_koda from Nastavitve where nast_koda = 'prof_%(naziv)s'" % {"naziv": naziv})
  17.         dRez = c.fetchone()
  18.         cins = conn.cursor()
  19.         if dRez:#Checking if I need to update or insert new value
  20.             retexec = cins.execute("Update Nastavitve set nast_vrednost = ? where nast_koda = ?", (potnik + "," + baza, "prof_"+naziv))
  21.         else:
  22.             retexec = cins.execute("Insert into Nastavitve Values (?, ?)", ("prof_"+naziv, potnik + "," + baza))
  23.  
  24.         print "Total changes", retexec.connection.total_changes #this returns 1 
  25.  
  26.         c2 = conn.cursor()
  27.         c2.execute("Select nast_koda from Nastavitve where nast_koda = 'prof_%(naziv)s'" % {"naziv": naziv})
  28.         dRez2 = c2.fetchone()
  29.         #Test if new value is in the table before commit
  30.         print "PredComm", dRez2#I always get the rasult here 
  31.  
  32.         ret = conn.commit()
  33.         c1 = conn.cursor()
  34.         c1.execute("Select nast_koda from Nastavitve where nast_koda = 'prof_%(naziv)s'" % {"naziv": naziv})
  35.         dRez1 = c1.fetchone()
  36.         #Test if new value is in the table after commit
  37.         print "PoComm", dRez1#Sometimes happens that dRez1 == None
  38.         print "Total changes", retexec.connection.total_changes #this returns 1 
  39.  
  40.         self.Osvezi_profile()
  41.         pass
  42.  
I call function OnSaveProfile when I click some button on form. This code is meant to insert or update some data in table 'Nastavitve'. It works most of time, but sometimes the data just doen't inserts into table. When I click the button again, then it works fine (sometimes not on first click, but on second, third, ...). There is always data in table before commit. After commit it is sometimes gone. It looks like that instead of commit sqlite3 executes rollback.

I've done integrity_check and it returned 'ok'. I have PRAGMA synchronous set to 2-FULL. I've tried different isolation_level-s with no success.

I'm having trouble only on windows mobile device. When I run the application on desktop PC it always works fine.

So is this a bug on Windows Mobile or am I doing something wrong?

Thanks in advance for answer.
Nov 16 '10 #1
6 1872
dwblas
626 Expert 512MB
Generally, connect is used in following format
con = sqlite.connect(table_name)
Since we don't have the code for the orodja.povezava_na_bazo() function, there is no way to tell if the connection is established correctly.
Nov 17 '10 #2
Here is the code of povezava_na_bazo:
Expand|Select|Wrap|Line Numbers
  1. def povezava_na_bazo(self, baza=""):
  2.     import sqlite3
  3.  
  4.     #baza is the name of database. I need this bacause I use application with more than one database.
  5.     if baza:
  6.         baza = "_"+baza
  7.     if os.name == 'ce':
  8.         #this is used for mobile device
  9.         conn = sqlite3.connect('\\Program Files\\Python25\\Baza\\ANA_Mob_prod%(baza)s.db' %{'baza':baza})
  10.     else:    
  11.         #this is used for desktop computer (testing)
  12.         conn = sqlite3.connect('D:/Database/Mobilno/ANA_Mob_prod%(baza)s.db' %{'baza':baza})
  13.     return conn
  14.  
Nov 17 '10 #3
dwblas
626 Expert 512MB
All of the separate cursors may be confusing the database. The code should be more like:
Expand|Select|Wrap|Line Numbers
  1.         #Here I connect to the database
  2.         conn = orodja.povezava_na_bazo(self, "Nastavitve")
  3.         c = conn.cursor()
  4.         c.execute("Select nast_koda from Nastavitve where nast_koda = 'prof_%(naziv)s'" % {"naziv": naziv})
  5.         dRez = c.fetchone()
  6. ##        cins = conn.cursor()
  7.         if dRez:#Checking if I need to update or insert new value
  8.  
  9.             ## use the same cursor
  10.             retexec = c.execute("Update Nastavitve set nast_vrednost = ? where nast_koda = ?", (potnik + "," + baza, "prof_"+naziv))
  11.         else:
  12.             retexec = c.execute("Insert into Nastavitve Values (?, ?)", ("prof_"+naziv, potnik + "," + baza)) 
Nov 17 '10 #4
Thanks for your answer. But unfortunately now is even worse. It happes more often, that the data doesn't saves.
I originally have the code written with only one cursor. But because of this trouble I tried with separate cursors.

Now I changed the code to:
Expand|Select|Wrap|Line Numbers
  1.     def OnSaveProfile(self, event):
  2.         print "Start" 
  3.         naziv = self.edt_title.text
  4.         if not naziv:
  5.             gui.Message.ok(u"Error!", u"Enter profile title!", icon='error', parent=self)
  6.             return 0
  7.         potnik = self.edt_st_potn.text
  8.         if not potnik:
  9.             gui.Message.ok(u"Error!", u"Enter number of worker!", icon='error', parent=self)
  10.             return 0
  11.         baza = self.com_baza.text
  12.  
  13.         #Here I connect to the database
  14.         conn = orodja.povezava_na_bazo(self, "Nastavitve")
  15.         c = conn.cursor()
  16.         c.execute("Select nast_koda from Nastavitve where nast_koda = 'prof_%(naziv)s'" % {"naziv": naziv})
  17.         dRez = c.fetchone()
  18.         #cins = conn.cursor()
  19.         if dRez:#Checking if I need to update or insert new value
  20.             retexec = c.execute("Update Nastavitve set nast_vrednost = ? where nast_koda = ?", (potnik + "," + baza, "prof_"+naziv))
  21.         else:
  22.             retexec = c.execute("Insert into Nastavitve Values (?, ?)", ("prof_"+naziv, potnik + "," + baza))
  23.  
  24.         print "Total changes", retexec.connection.total_changes #this returns 1 
  25.  
  26.         #c = conn.cursor()
  27.         c.execute("Select nast_koda from Nastavitve where nast_koda = 'prof_%(naziv)s'" % {"naziv": naziv})
  28.         dRez2 = c.fetchone()
  29.         #Test if new value is in the table before commit
  30.         print "PredComm", dRez2#I always get the rasult here 
  31.  
  32.         ret = conn.commit()
  33.         #c1 = conn.cursor()
  34.         c.execute("Select nast_koda from Nastavitve where nast_koda = 'prof_%(naziv)s'" % {"naziv": naziv})
  35.         dRez1 = c.fetchone()
  36.         #Test if new value is in the table after commit
  37.         print "PoComm", dRez1#Sometimes happens that dRez1 == None
  38.         print "Total changes", retexec.connection.total_changes #this returns 1 
  39.  
  40.         print "End"
  41.         conn.close()
  42.         self.Osvezi_profile()
  43.         pass
  44.  
  45.  
I added conn.close() at the end and it still doesn't make the difference.
This is what I got printed out (note prof_A98, prof_A94, prof_A93, prof_A92):
Start
Total changes 1
PredComm (u'prof_A99',)
PoComm (u'prof_A99',)
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A98',)
PoComm None
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A98',)
PoComm (u'prof_A98',)
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A97',)
PoComm (u'prof_A97',)
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A96',)
PoComm (u'prof_A96',)
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A95',)
PoComm (u'prof_A95',)
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A94',)
PoComm None
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A94',)
PoComm (u'prof_A94',)
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A93',)
PoComm None
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A93',)
PoComm (u'prof_A93',)
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A92',)
PoComm None
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A92',)
PoComm None
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A92',)
PoComm (u'prof_A92',)
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A91',)
PoComm (u'prof_A91',)
Total changes 1
End
Start
Total changes 1
PredComm (u'prof_A90',)
PoComm (u'prof_A90',)
Total changes 1
End
Nov 18 '10 #5
dwblas
626 Expert 512MB
Move line 32
ret = conn.commit()
to line 23, that is after the update/insert. I am still guessing, that doing a update, then select and fetchone, then committing, may be causing the problem. In theory that should not be a problem though. Also, inserts and updates are usually in the following format, which uses a dictionary not a "?". Try this as well. I am no SQLite expert, so you may have to comment everything except the errant code, and try updates one at a time until you find the data that has a problem, and try working backwards.
Expand|Select|Wrap|Line Numbers
  1. retexec = c.execute("Update Nastavitve set nast_vrednost = :dic_1 where nast_koda = :dic_2", {dic_1:potnik + "," + baza, dic_2:"prof_"+naziv})
Nov 18 '10 #6
Now I tried this version:
Expand|Select|Wrap|Line Numbers
  1.         #Here I connect to the database
  2.         conn = orodja.povezava_na_bazo(self, "Nastavitve")
  3.         #conn.execute("begin immediate transaction")
  4.         c = conn.cursor()
  5.         c.execute("Select nast_koda from Nastavitve where nast_koda = 'prof_%(naziv)s'" % {"naziv": naziv})
  6.         dRez = c.fetchone()
  7.         #cins = conn.cursor()
  8.  
  9.         if dRez:#Checking if I need to update or insert new value
  10.             sSelect = "Update Nastavitve set nast_vrednost = '%(vrednost)s' where nast_koda = '%(naziv)s'" % {"vrednost": potnik + "," + baza, "naziv": "prof_"+naziv}
  11.         else:
  12.             sSelect = "Insert into Nastavitve Values ('%(naziv)s', '%(vrednost)s') " % {"vrednost": potnik + "," + baza, "naziv": "prof_"+naziv}
  13.         #Command execute on "conn" --> not on "c" (cursor)
  14.         retexec = conn.execute(sSelect)
  15.         conn.commit()
  16.  
So without setting cursor for insert/update statement and commit just after insert/update. But it is still the same. On desktop PC works fine, but on Win Mobile device (or emulator) the data sometimes just doesn't inserts.

I also tried this version:
Expand|Select|Wrap|Line Numbers
  1.         conn = orodja.povezava_na_bazo(self, "Nastavitve")
  2.         #conn.execute("begin immediate transaction")
  3.         c = conn.cursor()
  4.         c.execute("Select nast_koda from Nastavitve where nast_koda = 'prof_%(naziv)s'" % {"naziv": naziv})
  5.         dRez = c.fetchone()
  6.         #cins = conn.cursor()
  7.  
  8.         if dRez:#Checking if I need to update or insert new value
  9.             sSelect = "Update Nastavitve set nast_vrednost = '%(vrednost)s' where nast_koda = '%(naziv)s'" % {"vrednost": potnik + "," + baza, "naziv": "prof_"+naziv}
  10.         else:
  11.             sSelect = "Insert into Nastavitve Values ('%(naziv)s', '%(vrednost)s') " % {"vrednost": potnik + "," + baza, "naziv": "prof_"+naziv}
  12.         retexec = c.execute(sSelect)
  13.         conn.commit()
  14.  
So with setting cursor for insert/update statement and commit just after insert/update... Still the same...
Nov 19 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John Machin | last post by:
Apologies in advance if this is a bit bloggy, but I'd like to get comments on whether I've lost the plot (or, more likely, failed to acquire it) before I start reporting bugs etc. From "What's...
66
by: mensanator | last post by:
Probably just me. I've only been using Access and SQL Server for 12 years, so I'm sure my opinions don't count for anything. I was, nevertheless, looking forward to Sqlite3. And now that gmpy...
5
by: Jon | last post by:
Hi, I'm having some trouble trying to get DELETE to work with SQLITE3 and PHP and was wondering if anyone could be of some assistance? I have verified that it is set up correctly and I'm able...
4
by: Simon | last post by:
I installed the source code on unix for python 2.5.1. The install went mainly okay, except for some failures regarding: _ssl, _hashlib, _curses, _curses_panel. No errors regarding sqlite3....
0
by: David | last post by:
- Are there any peculiarities with using curs.executemany(...) vs. multiple How many times are you calling execute vs a single executemany? The python call overhead will add up for thousands of...
3
by: milan_sanremo | last post by:
I have sqlite installed, but when I try to import sqlite3 I receive: Python 2.5.1 (r251:54863, Nov 3 2007, 02:54:36) on sunos5 Type "help", "copyright", "credits" or "license" for more...
1
by: jeff_d_harper | last post by:
I've run into a problem with text encoding in the Sqlite3 module. I think it may be a bug. By default sqlite3 converts strings in the database from UTF-8 to unicode. This conversion can be...
2
bigturtle
by: bigturtle | last post by:
I'm trying to do some database stuff in Python 2.6 with sqlite3, and am having trouble understanding the basics of the interface between the two. I am able to create database files and retrieve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.