将数据插入数据库表时出现问题

时间:2014-07-21 19:14:37

标签: android sql

尝试在表格中插入数据时遇到问题。表创建和插入的代码如下:

    // Creamos la tabla Montanas
    db.execSQL("CREATE TABLE montanas ("+
            "_id INTEGER PIMARY KEY, "+
            "nombre TEXT, "+
            "sierra TEXT, "+
            "altitud INTEGER, "+
            "longitud REAL, "+
            "latitud REAL, "+
            "categoria INTEGER, "+
            "url TEXT, "+
            "foto TEXT, "+
            "num_visitas INTEGER, "+
            "fecha LONG, "+
            "valoracion REAL, "+
            "comentario TEXT)");

    // Añadimos montanas
    db.execSQL("INSERT INTO montanas VALUES (null, 'Abitigarra', "+
            "'Iturrieta', 1165, 551613.0, 4734431.0, TipoCategoria.CAT1.ordinal(), "+
            "'http://www.mendikat.net/modules.php?name=tracks&id=797', '', 0, "+
            "System.currentTimeMillis(), 0.0, '')");

这是我执行时遇到的错误:

07-21 18:46:16.062: E/SQLiteLog(1485): (1) near "(": syntax error
07-21 18:46:16.073: D/AndroidRuntime(1485): Shutting down VM
07-21 18:46:16.083: W/dalvikvm(1485): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-21 18:46:16.163: E/AndroidRuntime(1485): FATAL EXCEPTION: main
07-21 18:46:16.163: E/AndroidRuntime(1485): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mendiakmendi/com.example.mendiakmendi.ListViewMontanas}: android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: INSERT INTO montanas VALUES (null, 'Abitigarra', 'Iturrieta', 1165, 551613.0, 4734431.0, TipoCategoria.CAT1.ordinal(), 'http://www.mendikat.net/modules.php?name=tracks&id=797', '', 0, System.currentTimeMillis(), 0.0, '')

。 。

你能帮助我吗?我无法看清问题是什么。

5 个答案:

答案 0 :(得分:0)

尝试取出纬度和经度的小数点,就像在表格中将它们作为整数一样。 http://www.sqlite.org/datatype3.html

答案 1 :(得分:0)

我认为您想要将TipoCategoria.CAT1.ordinal()的结果放入而不是将methodname作为字符串...所以我认为您必须将您的sql字符串更改为:

4734431.0," + TipoCategoria.CAT1.ordinal() + " 

以及

"" + System.currentTimeMillis() + "

答案 2 :(得分:0)

尝试

  db.execSQL("INSERT INTO montanas VALUES (null, 'Abitigarra', "+
            "'Iturrieta', 1165, 551613.0, 4734431.0, "+TipoCategoria.CAT1.ordinal()+", "+
            "'http://www.mendikat.net/modules.php?name=tracks&id=797', '', 0, "+
            System.currentTimeMillis()+", 0.0, '')");

答案 3 :(得分:0)

从引号中删除System.currentTimeMillis()...

db.execSQL("INSERT INTO montanas VALUES (null, 'Abitigarra', "+
        "'Iturrieta', 1165, 551613.0, 4734431.0, TipoCategoria.CAT1.ordinal(), "+
        "'http://www.mendikat.net/modules.php?name=tracks&id=797', '', 0, "+
        System.currentTimeMillis()+", 0.0, '')");

答案 4 :(得分:0)

错误是在_id列创建表时应该写入INTEGER PRIMARY KEY的概率,你忘了后者。