So I need to run a loop in Ruby to pass some strings into SQLite. Basically I have a table that looks like this:
pID Data
1649,1650,1651|Some data
1643,3|some more data
23,4,5,6,7|More data
Now in my SQLite queries, I will sometimes need to pass all the pIDs for a given line through as one whole string, which I can easily do with the ‘i’ variable in the loop below:
pID = db.execute( "select distinct pID from pmesh")
pID.each do |i|
end
Where ‘i’ will equal each pID line and each string looks like this:
1649,1650,1651
1643,3
23,4,5,6,7
But sometimes I will need each string to look like this for me to pass them through:
"1649","1650","1651"
"1643","3"
"23,"4","5","6","7"
Is there any easy loop that I can do that will put quotations and proper commas to make the string appear as I want them to? I know I can do “i = i.to_s() and a = i.split(“,”)”, but I really don’t know much else loop-wise after that.
Thanks,
Bobby
Use Enumerable.collect
Here’s an example:
or, if you want to join them into one string:
or, if your input is a csv string:
Edit:
Here is an example of what I think you’re trying to do.
Here is the output:
edit2:
if you want your values to be a string of , for instance, “”1643”, “3””, then you’ll have to do the following: