Sqlite3顺序上的整数不起作用(即使是索引)

时间:2013-06-14 08:36:26

标签: python sqlite sql-order-by

Hy,

有人可以解释一下吗?

sqlite> .schema LastFM
CREATE TABLE LastFM(id INTEGER PRIMARY KEY, AlbumTitle TEXT, Hoerer INTEGER);
CREATE INDEX Hoerer_Index ON LastFM( Hoerer DESC );
sqlite> Explain SELECT * FROM LastFM ORDER BY Hoerer;
addr        opcode      p1          p2          p3          p4          p5
----------  ----------  ----------  ----------  ----------  ----------  ---------
0           Trace       0           0           0                       00
1           Noop        0           0           0                       00
2           Goto        0           16          0                       00
3           OpenRead    0           6           0           3           00
4           OpenRead    2           9           0           keyinfo(1,  00
5           Rewind      2           13          1           0           00
6           IdxRowid    2           1           0                       00
7           Seek        0           1           0                       00
8           IdxRowid    2           2           0                       00
9           Column      0           1           3                       00
10          Column      2           0           4                       00
11          ResultRow   2           3           0                       00
12          Next        2           6           0                       00
13          Close       0           0           0                       00
14          Close       2           0           0                       00
15          Halt        0           0           0                       00
16          Transactio  0           0           0                       00
17          VerifyCook  0           4           0                       00
18          TableLock   0           6           0           LastFM      00
19          Goto        0           3           0                       00
sqlite> SELECT * FROM LastFM ORDER BY Hoerer;
id          AlbumTitle    Hoerer
----------  ------------  ----------
35          Live In Oslo  1
36          Kleine Welt   1
37          The Land Of   1
38          No Limit / R  1
39          Faust... In   1
40          Live At Last  1
41          2009-10-10:   1
42          Collectif Me  1
13          Bbc Sessions  128
14          From Glory T  128
3           So Far        1294
27          Schiphorst 2  15
28          Od Serca Do   15
12          Faust / So F  178
32          The Wumme Ye  2
33          The Faust Co  2

我有一个表LastFm(id INTEGER PRIMARY KEY,AlbumTitle TEXT,Hoerer INTEGER) 并使用csv文件在python中填充数据:

    reader = csv.reader(open(csvFileName, 'r'), delimiter=';')
    for row in reader:
        to_db = []
        for col in row:
            to_db.append(unicode(col, "utf8"))
        cur.execute("INSERT INTO LastFM(AlbumTitle, Hoerer) VALUES (?, ?);", to_db)
    sqlConnection.commit()

但是“order by”功能无法正常工作。 我已经尝试在我想订购的专栏上创建一个索引,但它也不起作用。

一些提示?

1 个答案:

答案 0 :(得分:0)

您已将字符串插入Hoerer列,这就是字符串的排序方式。

如果您想在此列中包含数字,则导入代码必须插入数字。