-
Hi all, I'm quite a newby to Javascript and QTscript, and I simply cannot resolve the following issue: A field from table old should be split into several records that will be inserted in table new. For instance the original data from old
should become something like this in new
I know I can handle this with a little Python (export as CSV, process with Python script, re-import to database), but I just thought a user-defined function might be nice. So I came up with the following: var ID = arguments[0];
var value_in = arguments[1];
var table_out = arguments[2];
// split value_in to array
var values_out = value_in.split('|');
//create beginning of INSERT statement
var sql = "INSERT INTO " + table_out + " (myID, value_no, value)\nVALUES\n"
//loop through array to add the value sets
for (var i = 0; i < values_out.length; i++){
if(i+1<values_out.length ){
sql = sql + "('" + ID + "', '" + (i+1) + "', '" + values_out[i] + "'),\n";
}
//semicolon instead of trailing comma for the last value set!
else{
sql = sql + "('" + ID + "', '" + (i+1) + "', '" + values_out[i] + "');";
}
}
db.eval(sql);
return(sql); When I call this function with INSERT INTO new (myID, value_no, value)
VALUES
('1', '1', 'abc'),
('1', '2', 'def'); However, for some reason the INSERT statement is executed twice, and I end up with duplicate records in the table new. I assume that my approach to trigger the function by a SELECT statement and/or the way how I pass the parameters to the function might be faulty, but I'm stuck completely. All the best and thanks in advance, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
SQLiteStudio indeed does execute your query twice. Why? A way to work around it would be to use your function in other query than
and then you can do: This way you will have whatever is returned by |
Beta Was this translation helpful? Give feedback.
-
Hi Pawel, Thanks for looking into that and pointing out the cause of the issue. Apart from this, let me confirm that SQLiteStudio is an excellent tool and I appreciate it very much! Thanks and all the best for the new year, |
Beta Was this translation helpful? Give feedback.
Hi Pawel,
Thanks for looking into that and pointing out the cause of the issue.
I felt like something that is going on behind the curtains leads to this effect, but of course I could not be sure ...
I think I'll go for Python when I have to split records (of course I should work directly on the database then); I guess this might be a cleaner approach :-)
Apart from this, let me confirm that SQLiteStudio is an excellent tool and I appreciate it very much!
Thanks and all the best for the new year,
\frank