免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7201 | 回复: 7
打印 上一主题 下一主题

sqlite中如何存储一个列表? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-17 17:26 |只看该作者 |倒序浏览
hi,
目前我需要用Sqlite存储一些数据。比如有以下一张表:table1

Name    ID     Actions
-----------------------------
steave   1      delete,edit, add
jacky      2     delete,add
.....


其中Actions这一栏是一个列表,每个记录有不同的actions。请问如何建立该表?使得我使用如下命令可以返回一个list?

selection actions from table1 where name='jacky';

我最初的想法是把Actions这一项当成字符串处理,然后将通过逗号将其转换为一个list。但感觉这样做笨了点。在Sqlite侧应该有更简单的实现吧(我对数据库/SQL不太熟)?

论坛徽章:
0
2 [报告]
发表于 2008-03-17 20:58 |只看该作者
把一个字符串拆分在python中非常简单。sqlite不提供把一个字符串字段拆成一个list的功能。

论坛徽章:
0
3 [报告]
发表于 2008-03-18 09:47 |只看该作者
谢谢 limodou的答复。如果只能以字符串形式存储的话,那如果记录数量大的话,那不就很浪费空间?

论坛徽章:
0
4 [报告]
发表于 2008-03-18 13:37 |只看该作者
可否使用  Name + ID + Action 的表。

也就是多个Action存成多条记录。

论坛徽章:
0
5 [报告]
发表于 2008-03-19 16:26 |只看该作者
可以使用VARCHAR http://www.sqlite.org/faq.html#q9

论坛徽章:
0
6 [报告]
发表于 2008-03-19 21:03 |只看该作者
SQLite里面的,其实没有VARCHAR字符类型,只有TEXT类型,也不需要指定确切的字段长度。(http://sqlite.org/datatype3.html)

至于字符串拆分的问题,可以通过自定义aggregate函数的方法,把python函数binding到sqlite,这样在SQL语句里就可以直接引用。

也可以考虑使用“串行化”的处理方式,这样会显得好像OO一点。

论坛徽章:
0
7 [报告]
发表于 2008-03-19 21:15 |只看该作者
看这里 http://www.sqlite.org/datatypes.html 在前面我给出的FAQ中也有VARCHAR的说明。不过在3版中,仍然可以使用VARCHAR,不过会自动转为TEXT.

2.1 Determination Of Column Affinity

The type affinity of a column is determined by the declared type of the column, according to the following rules:

   1.

      If the datatype contains the string "INT" then it is assigned INTEGER affinity.
   2.

      If the datatype of the column contains any of the strings "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity. Notice that the type VARCHAR contains the string "CHAR" and is thus assigned TEXT affinity.
   3.

      If the datatype for a column contains the string "BLOB" or if no datatype is specified then the column has affinity NONE.
   4.

      If the datatype for a column contains any of the strings "REAL", "FLOA", or "DOUB" then the column has REAL affinity
   5.

      Otherwise, the affinity is NUMERIC.

If a table is created using a "CREATE TABLE <table> AS SELECT..." statement, then all columns have no datatype specified and they are given no affinity.

论坛徽章:
0
8 [报告]
发表于 2008-03-25 15:21 |只看该作者
谢谢楼上各位的回复。我知道该怎么做了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP