0

我正在尝试将 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);
    }

所以,我只有半桌。

4

1 回答 1

2

例外是 Java 告诉您 SQLite 不会接受SQLite 关键字 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);
    }
}

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

于 2013-10-23T15:11:00.187 回答