Android Question SQL change one field on one row

MitchBu

Well-Known Member
Licensed User
Longtime User
I am working on the SQL tutorial at https://www.b4x.com/android/forum/threads/sql-tutorial.6736/#content

I seem to have pretty much understood the example project.

Now I am modifying it to get to what I need. I have this grid showing data, a bit like a spreadsheet, and the user may modify one of the cells.

I am trying to update one field only in one row.

All the example I could find on the Internet require that the value of a field be a condition. But I found an example of modifying a field without condition at the bottom of https://www.w3schools.com/sql/sql_update.asp

I tried to adapt that to the example project here :
B4X:
    Cursor1.Position = 0
    SQL1.BeginTransaction
    Try
        SQL1.ExecNonQuery("UPDATE table1 SET col1='tata'")
        SQL1.TransactionSuccessful
    Catch
        Log(LastException.Message)
    End Try
    SQL1.EndTransaction
    Log("Cursor1.position " & Cursor1.position)
    Log("colonne 1: " & Cursor1.GetString("col1"))

I was expecting that col1 contains "tata" after the update, but it remains the same. No error was generated, though.

I will appreciate any help.
 

sorex

Expert
Licensed User
Longtime User
I think the value comes from an earlier sql execution.

ExecNonQuery doesn't return a record set if I recall right and I don't see where you do a new select statement (after the update) before you read out the cursor.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
What exactly do you want to do ?

Change the data of one column in one specific row?

The code you show:
SQL1.ExecNonQuery("UPDATE table1 SET col1='tata'")
will change the data of col1 in all rows to tata!

To change the data in a specific row , your code should be like this:
SQL1.ExecNonQuery("UPDATE table1 SET col1='tata' WHERE rowid = xxx")
You need a reference ID to the specific row this could be the intrnal rowid or a specific column with the ID.

You may have a look at the SQLite Database Booklet, it contains several example projects.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…