UPDATE on SQLite Cause Crash please take a look.

You must Login before you can answer or comment on any questions.

Trying to update database from a textfield when you hit return and my app crashes because of it. here is the code.

textField.addEventListener('return',function(e)
{
dataBase.execute('UPDATE interests SET title= '+textField.value+'WHERE id='+id);    
 
});
Thanks for your help!

3 Answers

Accepted Answer

You aren't properly quoting the text in the SQL statement. By way of example, if the text field had the value my sample text the SQL would end up:

UPDATE interests SET title= my sample text WHERE id=...
As you can see, that would be invalid SQL syntax. So just try:
textField.addEventListener('return',function(e)
{
dataBase.execute('UPDATE interests SET title= "'+textField.value+'" WHERE id='+id);    
});
This ignores the potential for the user to mess up the SQL by actually keying a double quote in the field (for example: He is 6' 5" tall). Because of that, and to avoid the possibility of a SQL injection attempt, you should really use substitution variables in update statements:
textField.addEventListener('return',function(e)
{
dataBase.execute('UPDATE interests SET title= ? WHERE id='+id, textField.value);    
});

— answered 2 years ago by Doug Handy
answer permalink
9 Comments
  • Since you can't mark your own answer as correct (last I knew), I am resuming comments here instead of under your own answer.

    So the error you are getting is a parse error on line 40? I show that as being the top: 40, but then I show the SQL on line 56 not 55, so I am guessing you are getting the parse error on this line:

    value:myResultSet.fieldByName('title'),
    Is that right? And the parse error occurs at runtime?

    Are you sure you are getting a result set back at the top of the file? Try this at the top of the file where you define myResultSet:

    var myResultSet = dataBase.execute('SELECT * FROM interests WHERE id = ' +win.id);
    alert('Title is ' + myResultSet.fieldByName('title'));
    Does it display the title you expect?

    — commented 2 years ago by Doug Handy

  • Still struggling with this. I am now opening and closing the database and have fixed the issue with that. Now it seems that the database is confusing values with column.

    Here is the error message:

    , com.plausiblelabs.pldatabase.error.query.string=UPDATE person SET nickName = NickNam WHERE id=2, com.plausiblelabs.pldatabase.error.vendor.string=no such column: NickNam}

    I am the proper call but then it seems that it's trying to use the value as the column name and says it can't find the column.

    Could this be a bug?

    — commented 2 years ago by Leonardo Amigoni

  • Please post the exact code creating the SQL statement. This is a different table name (person) then the previous code (interests) so this must be from another block of code.

    The error is either with your quoting or with how you do the ? substitution.

    — commented 2 years ago by Doug Handy

  • Show 6 more comments

Thank you very much Dough. Unfortunately it did not work. The app still crashes in the simulator even with the changes you suggested.

I am a newbie at appcelerator but I see a message in the console saying:

~~~ [ERROR] Syntax Error = Parse error at interests.js (line 40)

Terminating in response to SpringBoard's termination. ~~~~

That might be the reason. Although I can't seem to find the issue at line 40 even if I comment the line out it still gives me the same error.

— answered 2 years ago by Leonardo Amigoni
answer permalink
3 Comments
  • First, please use the comment facility to retain continuity of the thread when trying to clarify an existing answer, as opposed to starting a new answer. It makes the Q&A flow easier to follow.

    Second, what is line 40? The dataBase.execute? If you comment out a line and you still get a parse error, there is most likely something in the surrounding code. Can you post a block of code with some lines both before and after line 40?

    — commented 2 years ago by Doug Handy

  • I attached the whole interests.js code. Please keep in mind that I am a newbee and I am still learning my way around. Feel free to give me any advice.

    I inserted the code you advised me to use on line 55. I am not sure how the simulation works but it appears to me that since it's terminated, it can't update the database.

    Thanks so much for your help.

    Ti.include('data.js')
     
    var win = Ti.UI.currentWindow;
    win.backgroundColor = '#FFFFCC';
    var myResultSet = dataBase.execute('SELECT * FROM interests WHERE id = ' +win.id);
    win.id =  myResultSet.fieldByName('id');
    win.title = myResultSet.fieldByName('title');
     
     
    var done = Titanium.UI.createButton({
        title:'Done',
        style:Titanium.UI.iPhone.SystemButtonStyle.DONE
        });
     
        done.addEventListener('click', function()
        {
            //win.setRightNavButton(edit);
            //myTableView.editing = false;
            win.close()
        });
     
    win.setRightNavButton(done);
     
    var nameLabel = Titanium.UI.createLabel({
        text:'Interest Name',
        color:'#000',
        font:{fontSize:15},
        textAlign:'left',
        width:'auto',
        height:'auto',
        top:20,
        left:20
        });
     
    win.add(nameLabel); 
     
    var title= Titanium.UI.createTextField({
        hintText:'',
        value:myResultSet.fieldByName('title'),
        top: 40,
        left: 20,
        width: 280,
        height: 40,
        textAlign:'left',
        paddingLeft:10,
        backgroundColor:'white',
        borderRadius:10,
        borderColor:'#bbb'
     
        });
     
     
        title.addEventListener('return',function(e)
        {
     
        dataBase.execute('UPDATE interests SET title= ? WHERE id='+win.id, title.value );   
     
        });
     
        title.addEventListener('blur',function(e)
        {
     
        });
     
     
    win.add(title)
     
     
    var notesLabel = Titanium.UI.createLabel({
        text:'Notes',
        color:'#000',
        font:{fontSize:15},
        textAlign:'left',
        width:'auto',
        height:'auto',
        top:90,
        left:20
        });
    win.add(notesLabel);
     
    var notes= Titanium.UI.createTextArea({
        value:myResultSet.fieldByName('notes'),
        top: 110,
        left: 20,
        width: 280,
        height:200,
        textAlign:'left',
        backgroundColor:'white',
        borderRadius:10,
        borderColor:'#bbb'
    });
     
    win.add(notes);

    — commented 2 years ago by Leonardo Amigoni

  • This makes no sense. I commented out the whole file and I still get the error. I event commented out any references to the file from other files. I still get the error. There has to be something in the build that is getting corrupted.

    — commented 2 years ago by Leonardo Amigoni

If you think something in the build is corrupted, just do a menu Project > Clean from Ti Studio. Then build it again. If the problem persists at runtime, place breakpoints shortly before the last activity you can see happening then start stepping through the code.

— answered 2 years ago by Doug Handy
answer permalink
10 Comments
  • Cleaned the project. No luck. Commented out the problematic area after clean. No Luck.

    I am fairly new to coding, so I am not very familiar with breakpoints. Either way I set a breakpoint at line 39 or 38 and it seems to be fine till then. Don't really know what to do after that. I don't have that type of knowledge.

    — commented 2 years ago by Leonardo Amigoni

  • Solved! But not in a nice way. I had to copy all my resource files. Start a new project. Copy them back in. Now it works with no errors.

    I wish I knew what happened and how to fix it. The clean didn't seem to do anything in this case.

    The issue with update still stands though. I even used this code. Still crashes but now the project doesn't get corrupted it was the VALUE that was making it corrupt.

    dataBase.execute('UPDATE interests SET title= ? WHERE id=1', 'title' );

    — commented 2 years ago by Leonardo Amigoni

  • I am glad you got that resolved, but it is a separate issue from the original question about the SQL error. The problem with that was the lack of quotes, as shown in my original answer.

    Once you have determined the SQL works, if you mark an answer as correct it helps ranking of future Q&A searches by users looking for answers to their own problem.

    — commented 2 years ago by Doug Handy

  • Show 7 more comments

Your Answer

Think you can help? Login to answer this question!