[英]SQLiteException syntax error on line breaks trying to read dump file from assets
I'm developing an Android
app which in I have a SQLite
dump file in assets and I'm trying to create a database in user's phone using that dump. 我正在开发一个
Android
应用程序,该应用程序的资产中有一个SQLite
转储文件,并且正在尝试使用该转储在用户电话中创建数据库。
The problem is it's a long dump file containing sql command including line breaks like this: 问题是这是一个包含sql命令的长转储文件,其中包括如下换行符:
CREATE TABLE [tblType] (
[IdType] INTEGER NOT NULL ON CONFLICT ROLLBACK PRIMARY KEY ON CONFLICT ROLLBACK AUTOINCREMENT,
[Type] NVARCHAR);
So when I try to create the database using SQLiteOpenHelper
: 因此,当我尝试使用
SQLiteOpenHelper
创建数据库SQLiteOpenHelper
:
SQLiteOpenHelper openHelper0 = new DbHelper(this);
Cursor cu = null;
try {
cu = openHelper0.getReadableDatabase().rawQuery("select IdWord,Word from tblWord", null);
} catch (Exception e) {
e.printStackTrace();
}
I get syntax error on every line including a line break: 我在包括换行符的每一行上都收到语法错误:
android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: CREATE TABLE [tblType](
android.database.sqlite.SQLiteException:靠近“(”:语法错误(代码1):,而在编译时:CREATE TABLE [tblType](
And if I remove the first line break I get: 如果删除第一行,我将得到:
android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: CREATE TABLE [tblType]([IdType] INTEGER NOT NULL ON CONFLICT ROLLBACK PRIMARY KEY ON CONFLICT ROLLBACK AUTOINCREMENT,
android.database.sqlite.SQLiteException:在“,”附近:语法错误(代码1):
As I said, it's a large dump and I can't manually remove all linebreaks; 正如我所说,这是一个很大的转储,我无法手动删除所有换行符; So the question is is there any way like configurations or extra code to make
Android
ignores the line breaks? 因此问题是,是否有任何配置或额外代码之类的方法可以使
Android
忽略换行符?
This is the main code from DBHelper
: 这是
DBHelper
的主要代码:
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
try {
InputStream is = activity.getAssets().open("am.sql");
Scanner scanner = new Scanner(is).useDelimiter("\n");
while (scanner.hasNext()) {
sqLiteDatabase.execSQL(scanner.next());
}
is.close();
} catch (IOException ex){
}
}
You are reading the file line by line. 您正在逐行读取文件。 This does not work for SQL statements having multiple lines.
这对于具有多行的SQL语句不起作用。
You can use a semicolon at the end of a line to detect the actual end of statements, if you do not have any SQL statements that contain embedded semicolons (triggers, or text values with a semicolon before a newline). 如果您没有任何包含嵌入式分号(触发器或在换行符前带有分号的文本值)的SQL语句,则可以在行的末尾使用分号来检测语句的实际末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.