1

I'm sure there must be an easy answer to this but everything I've googled seems to over complicate things and now I'm not so sure.

So here's what I'd have to do to insert records into my table now

INSERT INTO CAT_BUD_TAB (_id, CAT_ITEM, IN_OUT) VALUES (null, 'Student Loan', 'out');
INSERT INTO CAT_BUD_TAB (_id, CAT_ITEM, IN_OUT) VALUES (null, 'Food', 'in');

I figure there must be a shorter way, say something like..

INSERT INTO CAT_BUD_TAB (_id, CAT_ITEM, IN_OUT) VALUES (null, 'Student Loan', 'out'),(null, 'Food', 'in');

I'm using SQLite as I'm developing for android, but I'm pretty sure it's SQLite3 which means INSERTS like above should be possible right? How do I do it?

Kara
  • 5,650
  • 15
  • 48
  • 55
Holly
  • 1,846
  • 6
  • 38
  • 56
  • 1
    This question has also been answered here: http://stackoverflow.com/questions/1609637/is-it-possible-to-insert-multiple-rows-at-a-time-in-an-sqlite-database – McStretch Dec 08 '10 at 15:29
  • Yeah i was getting mixed messages and thought maybe things had changes since last year, thanks. – Holly Dec 08 '10 at 15:35

5 Answers5

3

Try use statement as folowing :

INSERT INTO table(field1,field2) SELECT "value1","value2" UNION SELECT "value1","value2" ...
Abimaran Kugathasan
  • 26,826
  • 11
  • 67
  • 101
Slach
  • 929
  • 7
  • 19
2

Here is the official reference:

http://www.sqlite.org/lang_insert.html

However, it seems to be possible:

insert into myTable (col1,col2) 
     select aValue as col1,anotherValue as col2 
     union select moreValue,evenMoreValue 
     union...

Also, consider using things like:

begin transaction;
insert into ...;
insert into ...;
etc.
commit;
Cristian
  • 191,931
  • 60
  • 351
  • 260
1

I don´t know to combining multiple Inserts. I think this is not possible!

Use transactions and prepared statements, if you want to insert a long list in a very short time.

For what do you need it?

kodex83
  • 49
  • 7
  • Shouldn't this be a comment rather than an answer if kodex83 doesn't know the answer to the OP's question? – McStretch Dec 08 '10 at 15:32
1

The docs on the SQL syntax understood by SQLite seem to indicate that only one set of VALUES can be used with each INSERT statement.

http://www.sqlite.org/lang_insert.html

Alison R.
  • 3,994
  • 25
  • 32
0

simple way is store the value1 and value2 in array and use a for loop like

for(int i = 0;i< value1.length,i++)
{
INSERT INTO CAT_BUD_TAB (_id, CAT_ITEM, IN_OUT) VALUES (null, 'value1[i]', 'value2[i]');
}
McDowell
  • 102,869
  • 29
  • 193
  • 261
user1203673
  • 1,015
  • 7
  • 13