duanbu1998 2014-01-25 19:24
浏览 32
已采纳

在类中加载数据库表

Is it a good practice to load an entire table from a database into a class?

for example a user class? Adding all the information of users in a class?

  • 写回答

1条回答 默认 最新

  • douaipi3965 2014-01-25 21:11
    关注

    If your table has a few data it will be fine to get all the information. e.g. if user table has only 3 users; student, tutor and admin along with their ids it is ok to get all the data.

    But if the table has a large amount of data it is not a good practice to load whole table. Therefore you can use the operations such as LIMIT to query limited number of rows.

    LIMIT example:

    SELECT * FROM `my_table` LIMIT 0, 5 
    

    (This will display the first 5 results from 'my_table'.)

    For pagination following will be useful:

    1. totalRows = the total number of rows in DB. (can be taken using COUNT in sql)
    2. itemsPerPage = define how many items you want to show in a single page
    3. numberOfPages = totalRows/ itemsPerPage (numberOfPages is useful to display the page numbers in the page).
    4. startingItem = (pageNumber - 1) * itemsPerPage
    5. SELECT * FROM my_table LIMIT startingItem, itemsPerPage

    So basically you will need to pass the pageNumber to a function (when user clicks on a page) in order to retrieve limited number of rows.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)