B4J Question SQL check rows problem

Peter Lewis

Active Member
Licensed User
Longtime User
Hi
I have many subs where this is workign but all of a sudden , this does not work. I have checked the strings are the same and still it does not pick up that there are records already in the database. If there is already the same date (String) in the database then it should do nothing. If the result is equal to 0 then it should insert...
This is from the log

Today is: 05/20/2017
05/20/2017

and the fields in the database are TEXT for the Date and Numberic for the Rate


Here is the snip of code

Thank you

B4X:
    txtDte.Text=DateTime.Date(DateTime.Now)
    Log(txtDte.Text)
   
    If sql1.ExecQuerySingleResult("SELECT count(*) FROM xrate WHERE Date = "&txtDte.text) <> 0 Then
       
    Else
       
        sql1.ExecNonQuery2("INSERT INTO xrate VALUES (?,?)", Array As Object(txtDte.text, xrate)) ' add field
       
    End If
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
most likely, this is returning an object:

B4X:
sql1.ExecQuerySingleResult("SELECT count(*) FROM xrate WHERE Date = "&txtDte.text)

and an object compared to a int will always be false. ie.

B4X:
dim var1 as string = "0"
dim var2 as int = 0

log(var1 = var2) ' this will be false

you can force to be an int like this:

B4X:
dim counter as int = sql1.ExecQuerySingleResult("SELECT count(*) FROM xrate WHERE Date = "&txtDte.text)
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
most likely, this is returning an object:

B4X:
sql1.ExecQuerySingleResult("SELECT count(*) FROM xrate WHERE Date = "&txtDte.text)

and an object compared to a int will always be false. ie.

B4X:
dim var1 as string = "0"
dim var2 as int = 0

log(var1 = var2) ' this will be false

you can force to be an int like this:

B4X:
dim counter as int = sql1.ExecQuerySingleResult("SELECT count(*) FROM xrate WHERE Date = "&txtDte.text)


I updated the code to include what you suggested and it does not count any , even though it is in the database. It gives an error that the data is already in and connot add due to duplication

java.sql.SQLException: [SQLITE_CONSTRAINT] Abort due to constraint violation (column Date is not unique)

B4X:
        Dim counter As Int = sql1.ExecQuerySingleResult("SELECT count(*) FROM xrate WHERE Date = "&txtDte.text)
        If counter = 1 Then
        Log("already in DB")
    Else
            sql1.ExecNonQuery2("INSERT INTO xrate VALUES (?,?)", Array As Object(txtDte.text, xrate)) ' add field
                       
    End If
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
whats the log? for
B4X:
log(counter)

if you use

B4X:
sql1.ExecQuerySingleResult2("SELECT count(*) FROM xrate WHERE Date = ?",array as string(txtDte.text))

does it change?
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
whats the log? for
B4X:
log(counter)

if you use

B4X:
sql1.ExecQuerySingleResult2("SELECT count(*) FROM xrate WHERE Date = ?",array as string(txtDte.text))

does it change?

the Log(counter) will be to check how many records in the database have this date. If non then add the date

with
sql1.ExecQuerySingleResult2("SELECT count(*) FROM xrate WHERE Date = ?",array as string(txtDte.text)) there is no variable counter

UPDATE:::

Tried with the new format as String and it worked..

Thank you
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
No no, i am sorry i did not explain myself

this is to check in your logs, what value is holding the counter variable
B4X:
Dim counter As Int = sql1.ExecQuerySingleResult("SELECT count(*) FROM xrate WHERE Date = "&txtDte.text)
log("dates in db " &counter)

this is just a change.
B4X:
Dim counter As Int = sql1.ExecQuerySingleResult2("SELECT count(*) FROM xrate WHERE Date = ?",array as string(txtDte.text))
log("dates in db " &counter)
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
No no, i am sorry i did not explain myself

this is to check in your logs, what value is holding the counter variable
B4X:
Dim counter As Int = sql1.ExecQuerySingleResult("SELECT count(*) FROM xrate WHERE Date = "&txtDte.text)
log("dates in db " &counter)

this is just a change.
B4X:
Dim counter As Int = sql1.ExecQuerySingleResult2("SELECT count(*) FROM xrate WHERE Date = ?",array as string(txtDte.text))
log("dates in db " &counter)

So from what I have seen here, I should always use the ?,?,? and either Array as String or Array as object ?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I think your sql line in post #1 would have worked had you quoted the date string ie
B4X:
 If sql1.ExecQuerySingleResult("SELECT count(*) FROM xrate WHERE Date = '" & txtDte.text & "'") <> 0 Then
 
Upvote 0
Top