2

I need to insert a row like following:

INSERT INTO table1 (id, name) VALUES (1, SELECT otherName FROM table2);

Is it possible in SQLite?

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
XFreeUser
  • 41
  • 3

4 Answers4

4

Here is the syntax you are looking for:

INSERT INTO table1 (id, name)
SELECT 1, otherName
FROM table2

Have a look at this SO article which covers a similar question.

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

Coming from here : http://www.tutorialspoint.com/sqlite/sqlite_insert_query.htm

You can populate data into a table through select statement over another table provided another table has a set of fields, which are required to populate first table. Here is the syntax:

 INSERT INTO first_table_name [(column1, column2, ... columnN)]    
 SELECT column1, column2, ...columnN    
 FROM second_table_name   
 [WHERE condition];
Pierre-Alexandre Moller
  • 2,354
  • 1
  • 20
  • 29
0

This works :

INSERT INTO test2 values(1,(Select name from test1 where id=1))

Here i tested it on SQLite : http://sqlfiddle.com/#!5/9bcff/1

Shivam
  • 457
  • 1
  • 6
  • 15
-1

AM using java , so you can use some thing like this

prep = Db_Connector.connection.prepareStatement("insert into reg_member values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            prep.setString(2, surname);
            prep.setString(3, lastname);         
            prep.execute();
            prep.close();
            Db_Connector.killObjects(Db_Connector.statement, Db_Connector.connection);
Pierre-Alexandre Moller
  • 2,354
  • 1
  • 20
  • 29
Mwesigye John Bosco
  • 1,148
  • 2
  • 11
  • 14