Android Question Database on B4A

Jinora29

Member
Hello,
The project I am working on is a kind of database that, when it enters a date, can extract texts that correspond to the same date from the database and show them in the program. I set a set of values for the day and month so that a number is obtained when the user selects. For example, May 25 will be 525 and I intend to put the dates in the database in the same way so that it selects the date. But whatever I do, it sets the date to 0 and gives an error.
I think the reason is that when the user enters the date, it is in another module, but loading the database is in another. I don't really know why it shows 0, because when I want to show the value in msgbox, it shows correct. Or maybe the code is different and I am writing it wrong.
Can anyone help?
 

Attachments

  • Screen Shot 1.13.2021_1.png
    52.9 KB · Views: 138
  • Screen Shot 1.13.2021_2.png
    46.8 KB · Views: 133
  • Screen Shot 1.13.2021_3.png
    49.6 KB · Views: 135

Computersmith64

Well-Known Member
Licensed User
Longtime User
Why are you surrounding your query parameter with ampersands (Array As String("&total&"))?

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Where are you getting that it sets the date to 0? The error log you posted shows you are trying to access an empty cursor. At the very least you should be checking to see if the cursor is empty or not before you try to read from it.

I'd suggest you download an SQLite database browser (like this one), then you can check that the data is being saved in the format you think it is, plus you can run queries on it to verify that your syntax is correct.

- Colin.
 
Upvote 0

Jinora29

Member
Activity: Datetest

B4X:
Sub Process_Globals
    Dim day As Int
    Dim month As Int
End Sub

Sub Globals
    Private Spinner1 As Spinner
    Private Spinner2 As Spinner
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("datetest")   
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Button1_Click
    day=Spinner1.SelectedIndex+1
    month=(Spinner2.SelectedIndex+1)*100
End Sub

Sub Button2_Click
    Activity.Finish
    StartActivity(DataTest)
End Sub



Activity: Datatest

B4X:
Sub Process_Globals   
End Sub

Sub Globals
    Dim cur As Cursor
    Private Label1 As Label
    Dim total As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("testla")
    total=DateTest.day + DateTest.month
    cur=Starter.sqll.ExecQuery2("SELECT Text from Datable WHERE Date=?" , Array As String("total"))
    cur.Position=0
    Label1.Text=cur.GetString("Text")
    cur.Close
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Never use Cursor.
2. Always prefer local variables over global variables.
3. Never assume that there are results.
4. The sooner you switch to B4XPages, the sooner everything will become simpler.

B4X:
Dim rs As ResultSet = Starter.sqll.ExecQuery2("SELECT Text from Datable WHERE Date=?" , Array As String("total"))
If rs.NextRow Then
Label1.Text=rs.GetString("Text")
End If
rs.Close
 
Last edited:
Upvote 0

Jinora29

Member
I have DB Browser, and I ran the query there, but since the data should be loaded according to what the user chooses, I can't test it in the database browser. But I have tested this query and it works fine: "SELECT Text from Datable WHERE Date=202"
I don't know how to test it with the date that user chooses.
 
Upvote 0

Jinora29

Member
Thank you for the codes, with my old codes, when I opened the activity the app stopped, now it doesn't stop, but all I see is a blank page.
I think my problems are so basic, because I didn't have the chance to study coding completely and in the right way, so I'm sorry if I am asking these minor and noob questions...??
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
now it doesn't stop, but all I see is a blank page
Open a new thread with that problem, and upload a small project showing your problem.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
1. Never use Cursor.
2. Always prefer local variables over global variables.
3. Never assume that there are results.
4. The sooner you switch to B4XPages, the sooner everything will become simpler.

B4X:
Dim rs As ResultSet = Starter.sqll.ExecQuery2("SELECT Text from Datable WHERE Date=?" , Array As String("total"))
If rs.NextRow Then
    Label1.Text = rs.GetString("Text")   ' typo
End If
rs.Close
Sorry Erel
 
Upvote 0

Jinora29

Member
Sorry, do you have any idea why I can't see the text? I copied the same code.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Dim rs As ResultSet = Starter.sqll.ExecQuery("SELECT Text FROM Datable LIMIT 1")
If you change to this line, do you get any value?

If no, then either no rows inside the table, some typo error in your table/field name or the Text value is empty. Are you sure you connected to the correct database?
What is DateTest module? Do you declare the "day" and "month" variables as Private or Public?
 
Upvote 0

Jinora29

Member
Do you declare the "day" and "month" variables as Private or Public?
I don't know what these (Private or Public) are, I just wrote "Dim day as int", in process globals and then I got its value from spinner.
DateTest module is the activity before the activity that I want to show the data in. DateTest is where I put the spinners to get day and month...
Oh and the Text I want to load is not English, does this have anything to do with this error?
I tested this code, but I still don't see any text.
 
Upvote 0

Jinora29

Member
Right off the bat, this is wrong. Total is a variable, remove the double quotes. Your need to export your project as zip as you were asked in one of the posts. This thread has so far 16 posts and should not take but a handful at most to solve.
Actually, I was about to create the zip and send it here, but I saw your reply and I removed the quotes and now it works completely fine.
Thank you so much everyone.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Right off the bat, this is wrong. Total is a variable, remove the double quotes. Your need to export your project as zip as you were asked in one of the posts. This thread has so far 16 posts and should not take but a handful at most to solve.

Nice Good catch @Mahares
I second that.

Actually no, a great catch by @Mahares
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…