0

I have two SQLite database query INSERT into UserAccessLevels VALUES (12,"a", 21, "bb", 21) and INSERT INTO UserPersonalInfo VALUES (17, "a","a1",2,"a2","a3") two of them are working fine separately. I am tying to combine two of them together is it possible to do for SQL Database.

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
raz
  • 482
  • 1
  • 5
  • 17
  • 1
    normally `;` is used to delimter queries – Fabian N. Feb 22 '16 at 00:11
  • Not really. `INSERT` usually only works on one table, although some databases do have methods for combining them into a single query. – Gordon Linoff Feb 22 '16 at 00:12
  • oh. no way to combine them together. I think I have to run multiple query one after another then.. – raz Feb 22 '16 at 00:15
  • It depends on what SQL server you are using and what settings you have for allowing multiple queries in the same SQL statement. If the server allows it and you have enabled multiple statements, you usually separate the two statements with a semi-colon. It is a security risk so it is usually disabled by default or simply not allowed. – randyh22 Feb 22 '16 at 00:22

2 Answers2

0

Short answer: use a transaction to make sure both inserts happen as an atomic unit (the entire transaction will either succeed completely or fail completely).

If you are using SQLite, an example transaction would look something like this:

BEGIN;
INSERT INTO UserAccessLevels VALUES (12,"a", 21, "bb", 21);
INSERT INTO UserPersonalInfo VALUES (17, "a","a1",2,"a2","a3");
COMMIT;

If you are submitting a single string of all this for execution, the semicolons are critical.

If you don't actually need a transaction, or if there is one already created for you (by SQLiteManager, etc.), then simply run the INSERT statements with semicolons after each one, and you can exclude the BEGIN and COMMIT statements.

SlimsGhost
  • 2,849
  • 1
  • 10
  • 16
  • I am getting `SQLiteManager: BEGIN; [ cannot start a transaction within a transaction ]` error.......... – raz Feb 22 '16 at 00:54
  • 1
    Is SQLiteManager its own application with its own GUI? It seems to be wrapping everything in a transaction already, so trying to start another one won't work. Have you tried just running the two INSERT statements with semicolons at the end of each one? If that works, I'll update my answer to remove the transaction stuff. – SlimsGhost Feb 22 '16 at 01:04
  • I found the solution in http://stackoverflow.com/questions/12741891/run-multiple-commands-in-sqlite-manager.. it was already available. I think next time I have to pay more attention for google search. ;) – raz Feb 22 '16 at 01:06
0
BEGIN
INSERT INTO UserAccessLevels VALUES (12,"a", 21, "bb", 21)
INSERT INTO UserPersonalInfo VALUES (17, "a","a1",2,"a2","a3")
END
Prob1em
  • 74
  • 6
  • I am getting `SQLiteManager: Likely SQL syntax error: BEGIN INSERT INTO UserAccessLevels VALUES (12,"a", 21, "bb", 21) INSERT INTO UserPersonalInfo VALUES (17, "a","a1",2,"a2","a3") END [ near "INSERT": syntax error ]` – raz Feb 22 '16 at 01:00
  • Try to explain what your query is supposed to do. – Vincent Savard Feb 22 '16 at 20:05