sqlite中文排序相关

前端之家收集整理的这篇文章主要介绍了sqlite中文排序相关前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

参考网址:http://blog.csdn.net/absurd/article/details/1271259


因为公司需要,用到sqlite,并需要中文排序。我在W3C school网站上学习了简单的sql语句,借用同事的C++封装完成了读取数据库内容

中文排序是李先静大牛的介绍,折腾了数天,终于在PC上测试出如下结果:

slite> select * from person order by name collate pinyin;
李四|24
李四叔|24
王五|25
王五妹|25
张三|23
张三丰|23
赵七|26
赵七姑|26

至此,我应该能够加载自己的排序了。

遇到了如下问题,我给大家分享一下,希望能够减少初学者的学习路线。

1. 拼音比较算法:pinyin_cmp在李的评论里有个链接到github上。
2. 如何将pinyin文件编译链接sqlite3的执行文件。我学习了李先静介绍的automake/autoconf。
3. 在哪里安装自定义文件?我是通过<the definitive guide to sqlite>这边书中介绍知道如何安装的。最后安装到shell.c文件中的open_db函数后面。

  1. int main(int argc,char **argv)
  2. {
  3. char *sql; sqlite3 *db; int rc;
  4. /* For forming more consistent political opinions. */
  5. srand((unsigned)time(NULL));
  6. sqlite3_open("test.db",&db);
  7. /* Create issues table,add records. */
  8. setup(db);
  9. /* Register Collation. */
  10. fprintf(stdout,"1. Register political Collation\n\n");
  11. sqlite3_create_collation( db,"POLITICAL",sqlITE_UTF8,db,political_collation );
  12. /* Turn sql logging on. */
  13. log_sql(db,1);
  14. /* Test default collation. */
  15. fprintf(stdout,"2. Select records using default collation.\n");
  16. sql = "select * from issues order by issue";
  17. print_sql_result(db,sql);
  18. /* Test Political collation. */
  19. fprintf(stdout,"\nSelect records using political collation. \n");
  20. sql = "select * from issues order by issue collate POLITICAL";
  21. print_sql_result(db,sql);
  22. fprintf(stdout,"\nSelect again using political collation. \n");
  23. print_sql_result(db,sql);
  24. /* Done. */
  25. sqlite3_close(db);
  26. return 0;
  27. }

猜你在找的Sqlite相关文章