Reputation: 41
I need to insert a row like following:
INSERT INTO table1 (id, name) VALUES (1, SELECT otherName FROM table2);
Is it possible in SQLite?
Upvotes: 2
Views: 264
Reputation: 1168
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);
Upvotes: -1
Reputation: 457
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
Upvotes: 0
Reputation: 521249
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.
Upvotes: 4
Reputation: 2354
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];
Upvotes: 1