sqlite insert into table select * from
explicitly specify the column name in the INSERT
clause,
INSERT INTO destinationTable (risposta, data_ins)
SELECT STATUS risposta, DATETIME('now') data_ins
FROM sourceTable
SQLite INSERT with SELECT
Put your select in Brackets:
INSERT INTO Players
VALUES('Name',
10.0,
(SELECT COUNT(*) AS Amount FROM Stack7 WHERE Name LIKE '%Name%'),
1.0);
In this way the compiler knows where the one value defined by your select statement starts and where it ends. And you are able to use a comma (,
) inside your brackets if needed.
SQLite: How to retrieve data from column in one table using SELECT to insert retrieved data in another table
I suspect that what you want is this:
INSERT INTO "mutants.teams" (team, members)
SELECT team, COUNT(*)
FROM "mutants.info"
GROUP BY team
This query selects all the (distinct) team names from "mutants.info"
and inserts them in "mutants.teams"
with the number of times each team appears in "mutants.info"
.
I don't know why you use in your code INSERT OR REPLACE
instead of just INSERT
.
If there are already rows in "mutants.teams"
and you want them replaced by the new rows if there is a unique constraint violation on the team's name then fine.
Sqlite - SELECT or INSERT and SELECT in one statement
Your command does not work because in SQL, INSERT
does not return a value.
If you have a unique constraint/index on the data
column, you can use that to prevent duplicates if you blindly insert the value; this uses SQLite's INSERT OR IGNORE
extension:
INSERT OR IGNORE INTO "Values"(data) VALUES('SOME_DATE');
SELECT id FROM "Values" WHERE data = 'SOME_DATA';
Using insert into database.table1 select * from table2 where id=some_value;
In the end I made an in-memory database, with the same table except leaving off "primary key". I did a first "insert into mytable select * from ... " into the in-memory database, then set absid to null, and then a second "insert into ..." into the target db. That way I got the row copied and with a new key value.
Although there is a second step, this whole action (in my app) is not done with huge numbers of rows so efficiency becomes secondary to maintainability.
SQLite Insert value and select
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.
Related Topics
How to Get Thumbnail for Video in My /Sdcard/Android/Data/Mypackage/Files Folder
How to Restrict My Edittext Input to Numerical (Possibly Decimal and Signed) Input
Android: How to Check How Much Memory Is Remaining
How to Capture Key Events from Bluetooth Headset with Android
Failed to Find Build Tools Revision 21.1.1 - Sdk Up to Date
Speechrecognizer Causes Anr... I Need Help with Android Speech API
Android: How to Get a Modal Dialog or Similar Modal Behavior
Android Volley Imageloader - Bitmaplrucache Parameter
Release APK Not Updating with JavaScript Code
How to Replace a String for a Buildvariant with Gradle in Android Studio
Does Android Studio Have a Hierarchy Viewer or Layout Inspector
Android 4.3: Ble: Filtering Behaviour of Startlescan()
Change Progressbar Color Through Code Only in Android
How to Get Pid of Android Application Without Using Adb Shell
Android How to Wait Until a Service Is Actually Connected
Writing Some Characters Like '<' in an Xml File
Should I Use Getapplicationcontext or Activity.This in a Long Running Asynctask