You Are Here:

Community: Developer Discussion Boards

#1 Old how to write and read float values to/from databases? - 2007-11-16, 16:08

Join Date: Sep 2003
Posts: 50
dehlerm
Offline
Regular Contributor
While starting to explore the DBMS features of PyS60 I ran into a problem I can't solve. When I write and then read float values to a database the decimal places seem to be truncated and only the integer places plus '.0' are shown. E.g. '123.0' appears instead of the stored '123.123'.
The code below creates a database and fills it with some float values and there string representations. Then the stored values are printed out.
In the printout the problem, hopefully, becomes visible.

Any help is appreciated
Thanks Martin

Code:
import e32db

db_name = u"c:\\test.db"

#create/open database
try:
	db = e32db.Dbms()
	db.open(db_name)
except:
	db.create(db_name)
	db.open(db_name)
	sql_create = u"CREATE TABLE test (value FLOAT, string VARCHAR)"
	db.execute(sql_create)

#delete all data in table test
sql_del = u"DELETE FROM test"
db.execute(sql_del)

#write data to database
for x in range(10):
	value = x * 123.123
	string = unicode(x * 123.123)
	sql_add = "INSERT INTO test (value, string) VALUES (%d,'%s')"%(value, string)
	db.execute(sql_add)

#read and print out data
dbv = e32db.Db_view()
dbv.prepare(db, u"SELECT * from test")
dbv.first_line()
print "float", "string"
for i in range(dbv.count_line()):
	dbv.get_line()
	print dbv.col(1), dbv.col(2)
	dbv.next_line()

db.compact()
db.close()
Reply With Quote

#2 Old Re: how to write and read float values to/from databases? - 2007-11-19, 08:52

Join Date: Oct 2007
Posts: 67
nlsp
Offline
Regular Contributor
Quote:
Originally Posted by dehlerm View Post
Code:
    sql_add = "INSERT INTO test (value, string) VALUES (%d,'%s')"%(value, string)
I'd say your problem is you use python string format %d for the float value. Try %f instead.
Reply With Quote

#3 Old Re: how to write and read float values to/from databases? - 2007-11-20, 15:52

Join Date: Sep 2003
Posts: 50
dehlerm
Offline
Regular Contributor
Quote:
Originally Posted by nlsp View Post
I'd say your problem is you use python string format %d for the float value. Try %f instead.
Thanks for your repy nlsp. But, %f doesn't work... -> KErrArgument error.

I also tried to specify the field which should store the float as DOUBLE or DOUBLE PRECISION in the CREATE statement (referring to table 7.1 of the PyS60 docu). This also doesn't solve the problem.

Anyone with more ideas?
Reply With Quote

#4 Old Re: how to write and read float values to/from databases? - 2007-11-20, 16:26

Join Date: Apr 2007
Posts: 81
Location: Lublin, Poland
janekw_'s Avatar
janekw_
Offline
Regular Contributor
Try %e. It works, although it's pita when you have to provide literals.


whatever happens happens
Reply With Quote

#5 Old Re: how to write and read float values to/from databases? - 2007-11-20, 21:49

Join Date: Sep 2003
Posts: 50
dehlerm
Offline
Regular Contributor
Thanks for the solution janekw_!

I tried to figure out how it might work by examening the Nokia 'sports diary' example code and google e.g. http://snippets.dzone.com/posts/show/1616. Both were misleading in this case ;-).
Where can i find a documentation for such problems? Is it Python or SQL specific?
Reply With Quote

#6 Old Re: how to write and read float values to/from databases? - 2007-11-20, 22:45

Join Date: Apr 2007
Posts: 81
Location: Lublin, Poland
janekw_'s Avatar
janekw_
Offline
Regular Contributor
You can find many useful info in Symbian SDK docs in DBMS section. PyS60 e32db is a very thin wrapper over native API. But in this case I just guessed right You can't give up precision (which you do with %d), you can't use decimal representation (%f) so the only option left was expotential representation.


whatever happens happens
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Read and write permissions jennie Mobile Java General 6 2009-01-23 17:48
RFile Read Write Asynchronous KErrCorrupt muntain General Symbian C++ 20 2007-08-26 21:46
How to read or write contact groups to/from PIM? giridharg Mobile Java General 3 2006-08-26 02:16
write and read a TInt data from the file billqu General Symbian C++ 2 2004-12-27 16:10
read and write Chinese menu in J2ME walterzcm Mobile Java General 0 2002-11-13 07:22

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia