EditText problem for storing data in SQL

masterleous

Member
Licensed User
Longtime User
Hi,

I am using 5 EditText fields in Designer window and has declared in Process global. I am trying to store all these EditText data in SQL, for which i had made one Save Button. I am using DBUilt example as a reference. The problem is that when ever i use auto fill data using FillFirstData function for stroing in SQL, its goes fine, but whenever i tried to store custom data by typing in above five Edittext field, i got error.

B4X:
Sub FillFirstData

   Dim ListOfMaps As List
   ListOfMaps.Initialize
   Dim id As Int
   For i = 1 To 5
      Dim m As Map
      m.Initialize
      m.Put("Data1", "ABC" & i)
      m.Put("Data2", "DEF" & i)
      m.Put("Data3", "GHI" & i)
      m.Put("Data4", "MNO" & i)
      m.Put("Data5", "PQR" & i)
      ListOfMaps.Add(m)
   Next
   DBUtils.InsertMaps(SQL, "Datatable", ListOfMaps)

End Sub

Above code stores 5 different data to sql table, and i can view that.

But following code gives error

B4X:
Sub btSave_Click

   Dim ListOfMaps As List
   ListOfMaps.Initialize
      Dim m As Map
      m.Initialize
      m.Put("Data1", EditText1.Text)
      m.Put("Data2", EditText2.Text)
      m.Put("Data3", EditText3.Text)
      m.Put("Data4", EditText4.Text)
      m.Put("Data5", EditText5.Text)
      ListOfMaps.Add(m)
   Next
   DBUtils.InsertMaps(SQL, "Datatable", ListOfMaps)

End Sub

Above Code Gives Error

B4X:
InsertMaps (first query out of 2): INSERT INTO [Datatable] ([Data1], [Data2], [Data3], [Data4], [Data5]) VALUES (?, ?, ?, ?, ?, ?)

(SQLiteConstraintException) android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

I had tried to view EditText fields with LOGcat, and realized that only first EditText gets the value, other has no value

What i am doing wrong?
 
Last edited:

Malky

Active Member
Licensed User
Longtime User
Where is the loop?

For i = 1 to 5?

You seem to be creating one row?

Could you maybe add the edit.texts to an array beforehand and use the same loop?

Sorry if I'm off course on this, only trying to help.

I've never tried a concat on a variable name as in EditText & i.text in B4A which would seem ideal?

Malky
 
Upvote 0

masterleous

Member
Licensed User
Longtime User
Thanks Malky for your reply :)

Where is the loop?

For i = 1 to 5?

You seem to be creating one row?

Loop was miss typed, it is FillFirstData function in which i am adding 5 different rows of 5 column data.

I've never tried a concat on a variable name as in EditText & i.text in B4A which would seem ideal?

I am Using Concat as EDITTEXT.Text =, for displaying field text value.

Could you maybe add the edit.texts to an array beforehand and use the same loop?

not understand your question, please specify
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Sorry for the confusion, I don't know if the concat thing would work (except maybe in my head)?

I am a newbie, but will try what I know.

Create an array and add the texts from each edittext

The use your loop (for i = 1 to 5) to step through the array and use the same code you have in the first code posted to add the data in the maps?

Malky
 
Upvote 0

masterleous

Member
Licensed User
Longtime User
Create an array and add the texts from each edittext

The use your loop (for i = 1 to 5) to step through the array and use the same code you have in the first code posted to add the data in the maps?

I shall try it today, and let you know
 
Upvote 0

masterleous

Member
Licensed User
Longtime User
Thanks Malky for your help,

Problem has been solved now, i was doing mistake in initializing of EditText fields, i was doing wrong. Now it is cleared by initializing EditText like as;

B4X:
EditText1.Initialize("EditText1")

before it was i was doing mistake like as

B4X:
EditText1.Initialize("EditText")
which was incorrect format.

Thanks
 
Upvote 0
Top