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.

4
  • 1
    normally ; is used to delimter queries Commented Feb 22, 2016 at 0:11
  • Not really. INSERT usually only works on one table, although some databases do have methods for combining them into a single query. Commented Feb 22, 2016 at 0:12
  • oh. no way to combine them together. I think I have to run multiple query one after another then.. Commented Feb 22, 2016 at 0: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. Commented Feb 22, 2016 at 0:22

2 Answers 2

0
BEGIN
INSERT INTO UserAccessLevels VALUES (12,"a", 21, "bb", 21)
INSERT INTO UserPersonalInfo VALUES (17, "a","a1",2,"a2","a3")
END
Sign up to request clarification or add additional context in comments.

2 Comments

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 ]
Try to explain what your query is supposed to do.
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.

3 Comments

I am getting SQLiteManager: BEGIN; [ cannot start a transaction within a transaction ] error..........
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.
I found the solution in stackoverflow.com/questions/12741891/….. it was already available. I think next time I have to pay more attention for google search. ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.