XFreeUser
XFreeUser

Reputation: 41

SQLite Insert value and select

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

Answers (4)

Mwesigye John Bosco
Mwesigye John Bosco

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

Shivam
Shivam

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

Tim Biegeleisen
Tim Biegeleisen

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

Pierre-Alexandre Moller
Pierre-Alexandre Moller

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

Related Questions