Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Column Summing Problem in SQLite
Started by bulcolimpex Jul 12 2019 11:55 PM

4 replies to this topic
column summing sqlite

Best Answer bulcolimpex , 13 July 2019 - 07:54 AM

Thanks nick sherman,

I edit the query and all work perfectly!

Here is the code for others members to consul if necesary.

Thanks !

 
local path = system.pathForFile( "mydatabase.db", system.DocumentsDirectory )
local db = sqlite3.open( path ) 
 
local query = [[ SELECT SUM ( obCena) AS obCena_sum FROM accounts ]]
 
local obCena_sum = 0
 
for row in db:nrows(query) do
 
  obCena_sum = row.obCena_sum
 
end
 
print( obCena_sum )
 

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

bulcolimpex

[GLOBAL: userInfoPane.html]
bulcolimpex
  • Observer

  • 7 posts
  • Corona SDK

Hello, I'm working on an app and have to sum up the
one of the columns in the database.
Surely I make a mistake, but I can not handle myself, so please for your help.
In the console returns nil, and there are values (digits) in the database column.
Please give advice on how to sum the column values in the database.
 
Excuse me for my bad English.
 
1.  local sumata
2.  local obCena_sum
3.
4.  local path = system.pathForFile( "mydatabase.db", system.DocumentsDirectory )
5.
6.  local db = sqlite3.open( path ) 
7.
8.  local sumata = [[ SELECT SUM ( obCena) FROM accounts AS obCena_sum ]]
9.
10. db:exec( sumata )
11.
12. print( obCena_sum )


[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

 
local path = system.pathForFile( "mydatabase.db", system.DocumentsDirectory )
local db = sqlite3.open( path ) 
 
local query = [[ SELECT SUM ( obCena) FROM accounts AS obCena_sum ]]
 
local obCena_sum = 0
 
for row in db:nrows(query) do
 
  obCena_sum = row.obCena_sum
 
end
 
print( obCena_sum )
 


[TOPIC: post.html]
#3

bulcolimpex

[GLOBAL: userInfoPane.html]
bulcolimpex
  • Observer

  • 7 posts
  • Corona SDK

Hi nick sherman,

Thanks for the suggestion, I change the code, but the result is same.

In the console print : nil



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

EDIT: your query was incorrect.

 

 
local query = [[ SELECT SUM (obCena) AS obCena_sum FROM accounts]]
 



[TOPIC: post.html]
#5

bulcolimpex

[GLOBAL: userInfoPane.html]
bulcolimpex
  • Observer

  • 7 posts
  • Corona SDK

  Best Answer

Thanks nick sherman,

I edit the query and all work perfectly!

Here is the code for others members to consul if necesary.

Thanks !

 
local path = system.pathForFile( "mydatabase.db", system.DocumentsDirectory )
local db = sqlite3.open( path ) 
 
local query = [[ SELECT SUM ( obCena) AS obCena_sum FROM accounts ]]
 
local obCena_sum = 0
 
for row in db:nrows(query) do
 
  obCena_sum = row.obCena_sum
 
end
 
print( obCena_sum )
 



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: column summing, sqlite