java - 将 xls 文件中的数据插入 SQLite android 时出错

标签 java android sqlite parsing apache-poi

我正在尝试将 xls-parser 中的所有行添加到我的 sqlite 数据库中。 它对于表的前半部分效果很好,但是对于未添加的所有行,其余的向我发送此错误:

10-22 20:43:32.453  17882-17882/? E/Database﹕ Error inserting null= diameter= category=Постное Меню price=180.0 weight=220.0 subcategory=Салаты name=Микс овощной composition=микс из листьев салата, черри, болгпрский перец, авокадо, кедроаые орешки, пармезан, зелень, ореховая заправка type=Уно Моменто url=http://epongorod.ru/images/stories/virtuemart/product/uno-miks-ovoshchnoj.jpg
android.database.sqlite.SQLiteException: near "null": syntax error: , while compiling: INSERT INTO menuTable(null, diameter, category, price, weight, subcategory, name, composition, type, url) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
        at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
        at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
        at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
        at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
        at com.epon.MainActivity.MainActivity.onCreate(MainActivity.java:158)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
        at dalvik.system.NativeStart.main(Native Method)

解析示例:

if (wb != null) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        Sheet sheet = wb.getSheetAt(0);

        for (int i = 1; i < sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);

            for (int cn=row.getFirstCellNum(); cn<row.getLastCellNum(); cn++) {
                Cell cell = row.getCell(cn, Row.CREATE_NULL_AS_BLANK);
                String result = cell.toString();

                String name = null;
                switch (cn) {
                    case 0:
                        name = "type";
                        break;
                    case 1:
                        name = "category";
                        break;
                    case 2:
                        name = "subcategory";
                        break;
                    case 3:
                        name = "name";
                        break;
                    case 4:
                        name = "composition";
                        break;

                    case 5:
                        name = "weight";
                        break;

                    case 6:
                        name = "diameter";
                        break;
                    case 7:
                        name = "price";
                        break;

                    case 8:
                        name = "url";
                        break;
                }
                cv.put(name, result);
            }

            db.insert("menuTable", null, cv);
        }

这是创建表的代码:

@Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + "menuTable" + " (" +
                "_id" + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "type" + "TEXT," +
                "category" + "TEXT," +
                "subcategory" + "TEXT," +
                "name" + "TEXT," +
                "composition" + "TEXT," +
                "weight" + "TEXT," +
                "diameter" + "TEXT," +
                "price" + "TEXT," +
                "url" + "TEXT);";
        db.execSQL(query);
    }

所以,我只有整张 table 的一半。

最佳答案

异常(exception)是 Java 告诉您 SQLite 不会接受 SQLite Keyword 的方式。 null 作为代码生成的 INSERT 语句中的列名称:

INSERT INTO menuTable(null, diameter, category, price, weight, subcategory, name, composition, type, url) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);

很难猜测您在这里分享的内容,但也许可以像这样完成您的代码:

                case 8:
                    name = "url";
                    break;

                default:
                    //name = "_id";
                    break;
            }

            if(name != null && !name.isEmpty()) {
                cv.put(name, result);
            }

        }

        db.insert("menuTable", null, cv);
    }
}

会更完整地涵盖所有开关“可能性”和名称变量“未知”吗?

关于java - 将 xls 文件中的数据插入 SQLite android 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19528157/

相关文章:

java - 日期不进行比较

Android - 触摸绘图

android - 如何在 android 上添加叠加层以映射?

安卓:更新数据

google-app-engine - 为本地开发数据创建负载

java - 在 kafka 流中使用 kafka connect json api 消费 JSON 值 : JAVA

java - 如何配置 logback 以跳过来自 org.package.* 的所有级别低于 WARN 的日志消息?

android - FBSDK 共享在 react-native 应用程序中不起作用。仅在某些安卓设备上

sqlite - 将 .CSV 文件导入 SQLite

java - TeamCity 构建中没有代码检查选项卡 (FindBugs)