sql example Error

volvomann

Active Member
Licensed User
Longtime User
When I tried to creath a sql base like the eample in beginnersguide i got this error
Compiling code. Error
Error parsing program.
Error description: Undeclared variable 'numberofcolumns' is used before it was assigned any value.
Occurred on line: 23
Dim columname(numberofcolumns) As String

Here is the code i use
Dim columname(numberofcolumns) As String
DBFileName = "persons, db"
DBFileDir = File.DirRootExternal
DBTableName = "persons"
What can I have done wrong
 

klaus

Expert
Licensed User
Longtime User
The code you show is not enough.
It seems that you didn't dim the variable numberofcolumns before using it:
B4X:
Dim numberofcolumns as Int   : numberofcolumns = 10
Dim columname(numberofcolumns) As String
The value of 10 is just an example, you shoul put your number on columns.

Best regards.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Klaus:
1. Could you please explain why don't you DIM like this:
B4X:
Dim columname(10) As String
instead of your code:
B4X:
Dim numberofcolumns as Int   : numberofcolumns = 10
Dim columname(numberofcolumns) As String
2. Also, when do you DIM MyVar() as opposed to MyVar(X)
Merci d'avance
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
The code above was ok but i have bigg trouble with the example in the beginnersguid, are there anyone that have an examplecode almost like the example to share with me i will be verry pleased:
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi Mahares,

1) The value of the number of columns is used quite often in the program so it's much more efficient to use a variable instead of using the value each time because if you need change this value you change it once in the variable and not x times in the program.
Example :
If the number of columns = 10
We could use:
B4X:
Dim ColumnName(10) As String
Dim ColumnWidth(10) As Int
.
.
For i = 0 To 9
  ColumnName(i) = something
  ColumnWidth(i) = 50dip
Next

For i = 0 To 9
.
Next
.
.
For i = 0 To 9
.
Next
If we need to change the value we need to change it everywhere in the program, this can become combersome and buggy.

So I prefer using a variable, even for the Dim
B4X:
Dim NumberOfColumns as Int  : NumberOfColumns = 10
Dim ColumnName(NumberOfColumns) As String
Dim ColumnWidth(NumberOfColumns) As Int
.
.
For i = 0 To NumberOfColumns  - 1
  ColumnName(i) = something
  ColumnWidth(i) = 50dip
Next

For i = 0 To NumberOfColumns - 1
.
Next
.
.
For i = 0 To NumberOfColumns - 1
.
Next
Changing the value from 10 to 12 is done in NumberOfColumns = 12 and all the rest you don't care.

2. Also, when do you DIM MyVar() as opposed to MyVar(X)
You use DIM MyVar(X) when you know the value of X and it will not be changed afterwords in the progam. Eventhough it's still possible to 'redim' an array dimed in Globals with a numer, but all values are set to 0 or "".

You use DIM MyVar()
- when you want to fill it with an array, like:
B4X:
Dim Days() As String
Days = Array As String("Sunday", "Monday", ...)
- or if you want to redim it somewhere else in the program.

Best regards.
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
Hi volvomann,
What trouble do you have ?

Best regards.
Actually i have a problem to understande the code in the example, so anyone shuddent have democode off a litle database witth 4 columns and add and edit function? I`m not so good in Inglish so What does it meane to create the table (side 158 beginners guid) My Inglish is my weaknis.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi volvomann,
...create the table (side 158 beginners guid)..
Sorry, this is an error in the guide.
The database already exists, so no need to to create it.
At the beginning when I wrote this chapter I used another example, SQLiteDB, where a database was created inside the program.

This error will be updated in the next version.

For your information, the last edition of the Beginner's Guide is edition 1.1 and the SQL chapter has moved to the User's Guide.
Note that the source code for all programs is included in the SoureCode folder you got with the Beginner's Guide.

Sorry for that,
Best regards.
 
Last edited:
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
Ok tank you no I get it, and databaseexample works but isen`t there a easier way ther I can save my data? I need to save " Name , Fitnesindex BMI, Ukk for ca 20 persons? I must ofcource open coud open the data and read them. The data is calculate in my app and shown in textboxes.
 
Upvote 0
Top