Mohamed Salah
Mohamed Salah

Reputation: 1205

INSERT INTO not Inserting When Declared Twice SqlLite ANDROID

I have some SQL syntax need to be executed but when I try to execute two INSERT Syntax the first is executed and the second is not Executed So Why?

Code:

db.excuteSQL("INSERT INTO `en_ahmedali`  VALUES\n" +
                    "(1, 1, 1, 'In the name of Allah, most benevolent, ever-merciful.'),\n" +
                    "(2, 1, 2, 'ALL PRAISE BE to Allah, Lord of all the worlds,'),\n" +
                    "(3, 1, 3, 'Most beneficent, ever-merciful,'),\n" +
                    "(4, 1, 4, 'King of the Day of Judgement.'),\n" +
                    "(5, 1, 5, 'You alone we worship, and to You alone turn for help.'),\n" +
                    "(6, 1, 6, 'Guide us (O Lord) to the path that is straight,'),\n" +
                    "(7, 1, 7, 'The path of those You have blessed, Not of those who have earned Your anger, nor those who have gone astray.');\n" +
                    "\n" +
                    "INSERT INTO `en_ahmedali`  VALUES\n" +
                    "(6231, 114, 1, 'SAY: \"I SEEK refuge with the Lord of men,'),\n" +
                    "(6232, 114, 2, 'The King of men,'),\n" +
                    "(6233, 114, 3, 'The God of men,'),\n" +
                    "(6234, 114, 4, 'From the evil of him who breathes temptations into the minds of men,'),\n" +
                    "(6235, 114, 5, 'Who suggests evil thoughts to the hearts of men --'),\n" +
                    "(6236, 114, 6, 'From among the jinns and men.');");

Upvotes: 1

Views: 65

Answers (6)

DarioN1
DarioN1

Reputation: 2552

Try an insert sintax like this:

INSERT INTO myTab (field1,field2,field3)
SELECT 1, 2,'3'UNION ALL
SELECT 2,3, 'v' UNION ALL
SELECT 1,4,'6' 

I think it should work

Upvotes: 1

Mohamed Salah
Mohamed Salah

Reputation: 1205

What I reached:

  • You Can't Execute More Than one statement in db.excuteSQL()

So there are two Options:

First, You Can Separate The Statements and Execute them Manually:

db.executeSQL("INSERT INTO en_ahmedali VALUES (1, 'text', 'text');");
db.executeSQL("INSERT INTO en_ahmedali VALUES (2, 'text2', 'text2');");

Second, You Will Use split() to put all statements in one Array and then Execute Them with For Loop:

String sql = "INSERT INTO en_ahmedali VALUES (1, 'text1', 'text1');\nINSERT INTO en_ahmedali VALUES (2, 'text2', 'text2');";
String[] queries = sql.split(";\n"); //Make Sure That Each Statment is seprated with new line.

for(String query : queries){
    db.execSQL(query);
}

Thanks,

Upvotes: 0

Karakuri
Karakuri

Reputation: 38605

executeSQL() on Android can only execute on SQL statement. If you put a two statements separated by semicolon, only the first will run. You need to either combine all the values lists into one giant INSERT statement, or you need to call executeSQL() separately for each INSERT statement.

Upvotes: 2

Gérard Binkhorst
Gérard Binkhorst

Reputation: 77

Put the inserts in separate db.excuteSQL statements and don't forget to commit at some time.

Upvotes: 0

Steve Smith
Steve Smith

Reputation: 2270

You don't say what SQL library you are using, but I would guess that you can only call one command per call.

Upvotes: 0

StuStirling
StuStirling

Reputation: 16221

You cannot execute two statements in one execSQL call. Separate them into two calls, either programatically using possibly the semi-colon or just manually and it will work.

Upvotes: 1

Related Questions