我正在尝试读取脚本文件,我输入了一个文件名“ test.sql”。但是运行代码时出现错误。
这是错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.waiku.work2, PID: 3990
android.database.sqlite.SQLiteException: near "Pertama": syntax error (code 1): , while compiling: INSERT INTO `Table1` (name,address,phone,id) VALUES (WK,Taman Pertama,NULL,'4');
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "Pertama": syntax error (code 1): , while compiling: INSERT INTO `Table1` (name,address,phone,id) VALUES (WK,Taman Pertama,NULL,'4');)
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at com.example.waiku.work2.MainActivity$3.onClick(MainActivity.java:100)
这是我的代码,错误行100指向db.execSQL(line);
。
mBtnImport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = mSQLiteHelper.getWritableDatabase();
try {
String filename = "test.sql";
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, filename);
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
if (line.trim().length() > 0) {
db.execSQL(line);
db.close();
}
}
Toast.makeText(getApplicationContext(), "Upgrade complete.", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
new upgradeDB().execute("", "", "");
}
});
我可以知道是什么错误吗?
答案 0 :(得分:2)
android.database.sqlite.SQLiteException:“ Pertama”附近:语法错误 (代码1):,而在编译时:INSERT INTO
Table1
(名称,地址,电话,id)值(WK,Taman Pertama,NULL,'4');
是说您有语法错误。字符串/文本应该用单引号引起来。
所以需要使用类似的东西
INSERT INTO Table1 (name,address,phone,id) VALUES ('WK','Taman Pertama',NULL,4);
即WK和Taman Pertama不需要用引号括起来,而数字则不需要。