Fastest way to insert elements from Ruby hash to SQLite 3 - ruby

I have a Ruby hash with variables:
a two-element array of strings
an integer
I have tried the two following ways to insert the elements from the hash to a SQLite 3 DB:
myRubyHash.each do |k, v|
x=[k[0],k[1],v]
db.execute "INSERT INTO MyTable VALUES ( ?, ?, ? )", x
end
And,
myRubyHash.each do |x|
db.execute "INSERT INTO MyTable VALUES ( ?, ?, ? )", x
end
The first being considerably faster (but still quite slow). Is there a faster way to go about this?
If it helps, my SQLite 3 table was created by:
rows = db.execute <<-SQL
CREATE TABLE Assoc_words_p (
name1 varchar(30),
name2 varchar(30),
val int,
PRIMARY KEY (name1,name2)
);
SQL
Thanks

I found the prepare statement can be used as follows:
stmnt1 = db.prepare( "INSERT INTO Table(name1,name2,val) VALUES (?, ?, ?)" )
myRubyHash.each do |k, v|
stmnt1.execute(k[0],k[1],v/2)
end

Sadly, Hash#each is just not a fast method, and I expect that's what's causing the performance concerns. The first thing that comes to mind in terms of performance is wondering why there's a hash there at all - it seems like the faster way to go would be to put your data into the database directly and skip the hash altogether.

Related

JDBC connection for HANA with input parameters

I am trying to extract some tables with using RJDBC connection. However, in the tables, there are some must input variables that I need to give. I need to find a way that extract these input parameters. As I understand, then I can extract the tables I want. However, I don't know how to find and extract them.
qry <- 'select * from "__tablename__"(
PLACEHOLDER."$$variable1$$" => ?,
PLACEHOLDER."$$variable2$$" => ?,
PLACEHOLDER."$$variable3$$" => ?,
PLACEHOLDER."$$variable4$$" => ?,
PLACEHOLDER."$$variable5$$" => ?)'
data <- dbGetQuery(conn, qry, variable1, variable2, variable3, variable4, variable5)

Can't insert multiple INSERT queries via Laravel

I have simple multiple insert:
$query = "
INSERT INTO `products` SET `code` = '0100130', `price` = '273.90', `brand` = 'Alpina', `supplier` = 'karat';
INSERT INTO `products` SET `code` = '0600075', `price` = '222.24', `brand` = 'Alpina', `supplier` = 'karat';
";
I have tried DB::raw($query), DB::query($query), DB::statement($query) - all three fails. But all three works if there is only one INSERT statement. If more than one, I get no error, but inserts are not performed.
I'm looking for a fastest way to import 13million inserts. Inserting one by one will take 24 hours for server.
Laravel v7.12.0
Try like this :
$query = "
INSERT INTO products(code, price, brand, supplier) VALUES
(0100130, 273.90, 'Alpina', 'karat'),
(0100130, 273.90, 'Alpina', 'karat')
";
You can't do two (or more) INSERT INTO into one sql query.

summing values from one table into another table

I'm an SQL newbie using VB6/Access 2000 and am trying to get a query which puts the sum of values from a table into another table.
VB6 does the job, but it's so slow.
I searched and tried in Access many times, just got lost with keywords IN, ON, (INNER) JOIN, each time getting a different error.
The core code should be as follows:
update t1
set t1.value = sum(t2.value)
where
val(t2.code)>89
and
t2.date=t1.date
t1.date is a date, no duplicates
t2.code is a variable string like '0081', '090'
values are single precision
After further searching i found a similar question here ( http://goo.gl/uqlw0U ) and tried that:
UPDATE t1
SET t1.value =
(
SELECT
SUM(t2.value)
FROM spese
WHERE
t1.date=t2.date
AND
val(t2.code)>89
)
but Access just says "updatable query needed" -- what does that mean?
Try this:
UPDATE t1
SET t1.value = SUM(t2.value)
FROM t1, t2
WHERE
val(t2.code)>89
AND
t2.date=t1.date

H2 MERGE: Column not found

I've got the following table:
create table companies (id identity, version int not null, last_modified timestamp not null);
insert into companies (version, last_modified) values (0, NOW());
I then create a PreparedStatement and supply a value for index 1:
merge into companies (id, version, last_modified) values(?, version + 1, NOW())
H2 fails with this error:
Column "VERSION" not found
I understand that H2 doesn't like version + 1 on the right-hand side, but it's not clear how to return 0 for new rows and version + 1 for existing rows. Is there an easier way than using a select statement with a union?
You could use:
merge into companies (id, version, last_modified)
values(?, coalesce((select version + 1 from companies where id = ?), 0), NOW())
Thomas's answer of
merge into companies (id, version, last_modified)
values(?, coalesce((select version + 1 from companies where id = ?), 0), NOW())
Becomes rather cumbersome if you (as I do) want to conditionally insert or update several fields - you need a coalesce((select ...), default) for each one!
It would appear that a more general answer needs to be two statements:
MERGE INTO companies (id) key (id) VALUES (?)
UPDATE companies SET version=1+IFNULL(version,0), otherfields... WHERE id=?
In other words: don't use MERGE for multiple conditional changes (where you need an expression and not just a value) in a record.
I'd love to be proven wrong on this...

How do I dynamically change constant assignment?

I'm writing an insert with a select:
my_object_id = 7
id_list = [1,2,4,5]
TEST_TEMPLATE = %Q{
INSERT INTO tests
(test_id, data_id, text, created_at, updated_at)
select #{my_object_id}, data_id, text, created_at, updated_at
from tests where id in (#{id_list})
}
ActiveRecord::Base.connection.execute(TEST_TEMPLATE);
I get error that I cannot change the constant. How do I inject values into a string so I can use it in my insert/select statements?
How can this be solved in Ruby?
Here's a bit of an explanation to #SergioTulentsev answer:
You should change the first letter of TEST_TEMPLATE to lowercase, because variables starting with uppercase letters are not actually variables, they are constants, so you shouldn't change them.
As #SergioTulentsev shows in his code, you should change every letter to lowercase to match the style-conventions used in Ruby.
There's no reason to make it a constant.
my_object_id = 7
id_list = [1,2,4,5]
test_template = %Q{
INSERT INTO tests
(test_id, data_id, text, created_at, updated_at)
select #{my_object_id}, data_id, text, created_at, updated_at
from tests where id in (#{id_list})
}
ActiveRecord::Base.connection.execute(test_template)

Resources