Insert values in database only once

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

Hello,

I have an application that reads data from a file and stores them into a database. But I need to store the data only once, so if I close the app and open it again, the same values should not be inserted again.

Is there a way to create a flag or mark the database and check if there are already tables inside?

Best Regards, Martin

2 Answers

Hello Martin,

first you have to set primary key of any common field....

after set primary key write code like this....

var btn = Ti.UI.createButton({
    title : 'Add',
    height : 35,
    width : 140,
});
win.add(btn);
 
btn.addEventListener('click', function(e) {
    try {
        var myDB = Ti.Database.open("films");
        var sql = 'INSERT INTO table (title,filmYear) VALUES ("' + 'DDL' + '","' + '1995' + '")';
        Ti.API.info('SQL => ' + sql);
        myDB.execute(sql);
        myDB.close();
        alert('Your film was added to your table.')
    } catch(e) {
        alert('This Film is already in table');
    }
});

— answered 11 months ago by Ritesh .
answer permalink
3 Comments
  • Martin,have you tried or not?

    — commented 11 months ago by Ritesh .

  • Hey, thanks for your answer. I haven't tried it out cause there is no button in my app to fill the database. Ivan's answer was exactly what I needed. Thanks though

    — commented 11 months ago by Martin Golpashin

  • I m using button only for the example purpose, main code is in button click event... anyway no problem, solution is more important for us...

    — commented 11 months ago by Ritesh .

Your Answer

Think you can help? Login to answer this question!