Insert Multi-values

42 views
Skip to first unread message

A.less.io

unread,
May 25, 2012, 6:44:00 AM5/25/12
to ActiveJDBC Group
Hi,

Does ActiveJDBC manage multiple insert for mySQL ?

Does it use multiple INSERT statements :

INSERT INTO table (field) VALUES ('foo1');
INSERT INTO table (field) VALUES ('foo2');

Or it use multiple sets of values:

INSERT INTO table (field) VALUES ('foo1'),('foo2);

Igor Polevoy

unread,
May 25, 2012, 1:22:14 PM5/25/12
to activejd...@googlegroups.com
Multiple values are inserted with you save a model, for example, this code:

Person p = new Person();
        p.set("name", "Marilyn");
        p.set("last_name", "Monroe");
        p.set("dob", getDate(1935, 12, 6));
        p.saveIt();

Will generate this query:
INSERT INTO people (updated_at, dob, name, last_name, created_at) VALUES (?, ?, ?, ?, ?)", with parameters: <2012-05-25 12:21:03.875>, <1935-12-06>, <Marilyn>, <Monroe>, <2012-05-25 12:21:03.875>

tx

Ale Fina

unread,
May 31, 2012, 6:59:03 AM5/31/12
to activejd...@googlegroups.com
I mean If it's possibile to do something like that:

List<Map> personList = new ArrayList();

HashMap mp = new HashMap();
mp.put("name","Marylin");
mp.put("last_name", "Monroe");
mp.put("dob", getDate(1935, 12, 6));
personList.add(mp);  

mp = new HashMap();
mp.put("name","Elizabeth);
mp.put("last_name", "Taylor");
mp.put("dob", getDate(1932, 11, 6));
personList.add(mp);


Person p = new Person();
p.setMultiValue(personList);  // this wolud run only one INSERT INTO table (person)  VALUES ('Marylin','Monroe','1935, 12, 6'),('Elizabeth', 'Taylor', '1932, 11, 6');

thx, bye
aLe

Igor Polevoy

unread,
May 31, 2012, 11:51:54 AM5/31/12
to activejd...@googlegroups.com
OK, understand the question now. Currently there is no functionality in ActiveJDBC to do this, however, this is not a bad idea. 
Except, I'd propose to use models for this instead of maps, something like this:

Person p1 = Person.create("name", "Marylin", "last_name", "Monroe", "dob", "1935/12/6");
Person p2 = Person.create("name", "Elizabeth", "last_name", "Tayor", "dob", "1935/11/6");

Person.save(p1, p2); // this would run only one INSERT INTO table (person)  VALUES ('Marylin','Monroe','1935, 12, 6'),('Elizabeth', 'Taylor', '1932, 11, 6');

And the method save() would generate either insert or update depending if these models have IDs or not. 

This is something to consider to add to the framework.

thanks
igor

Gladyston Batista

unread,
May 31, 2012, 1:53:32 PM5/31/12
to activejd...@googlegroups.com
Ideally, make a loop and save a list.

Example:

List<Person> list = new ArrayList();

for (int i=0; i<2; i++) {

  Person p = Person.create("name", "Elizabeth", "last_name", i+1);
  list.add(p);
}

//this would run only one INSERT INTO table (person) VALUES ('Elizabeth', 1),('Elizabeth', 2);
Person.save(list);


Atenciosamente,
--
Gladyston Batista
Belo Horizonte-MG / Brazil


2012/5/31 Igor Polevoy <ig...@expresspigeon.com>

Igor Polevoy

unread,
May 31, 2012, 2:09:44 PM5/31/12
to activejd...@googlegroups.com
sure, array or list will do 
Reply all
Reply to author
Forward
0 new messages