声明两次SqlLite ANDROID

时间:2017-01-27 14:50:08

标签: java android sqlite android-sqlite

我需要执行一些SQL语法,但是当我尝试执行两个INSERT语法时,第一个执行,第二个执行不执行,为什么?

代码:

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.');");

6 个答案:

答案 0 :(得分:2)

Android上的

executeSQL()只能在SQL语句上执行。如果用分号分隔两个语句,则只运行第一个语句。您需要将所有值列表组合到一个巨大的INSERT语句中,或者需要为每个INSERT语句单独调用executeSQL()

答案 1 :(得分:1)

您无法在一次execSQL调用中执行两个语句。将它们分成两个调用,可以使用可能的分号进行编程,也可以手动进行,也可以使用。

答案 2 :(得分:1)

尝试像这样插入sintax:

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

我认为应该有效

答案 3 :(得分:0)

您不会说出您正在使用的SQL库,但我猜您每次调用只能调用一个命令。

答案 4 :(得分:0)

将插入内容放在单独的db.excuteSQL语句中,并且不要忘记在某个时间提交。

答案 5 :(得分:0)

我到达了什么:

  • 您无法在db.excuteSQL()
  • 中执行多个语句

所以有两个选项:

首先,您可以手动分离语句并执行它们:

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

其次,您将使用split()将所有语句放在一个数组中,然后使用For循环执行它们:

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);
}

谢谢,