简体   繁体   English

SQLite中的SELECT *不会返回表中的所有内容

[英]SELECT * in SQLite doesn't return everything in the table

I'm using PHP to fetch data from a database. 我正在使用PHP从数据库中获取数据。

Here is the code: 这是代码:

<?php  
    $db = new SQLiteDatabase("testDB.db");
    $query = $db->query("SELECT * FROM blog ORDER BY blogdate DESC");
    while($entry = $query->fetch(SQLITE_ASSOC)) { //only one blog entry per day
        echo "<a href=\"display.php?date=".$entry['blogdate']."\">".$entry['blogdate']."</a><br>";
    }
?>

But for some reason it doesn't return an entry that I am certain is in the database. 但是由于某种原因,它不会返回我确定在数据库中的条目。 The reason I think it's in the db is that I can see the text of the entry when I view the *.db file. 我认为它在数据库中的原因是,当我查看* .db文件时,可以看到条目的文本。

Here are some specific questions that might help me better understand what's going on: 以下是一些特定的问题,可以帮助我更好地了解正在发生的事情:

I CREATE a table called blog . CREATE一个名为blog的表。 I INSERT tuples into blog using the query function as I did with the SELECT calls. INSERT元组到blog使用查询功能我用SELECT通话一样。 When I DELETE tuples using LIKE, are those tuples being deleted from the database, or are they being deleted from the table blog only? 当我使用LIKE DELETE元组时,这些元组是从数据库中删除还是仅从表博客中删除? If it is the latter case, how do I get SQLite to delete the tuple from the database completely? 如果是后一种情况,如何使SQLite完全从数据库中删除元组?

Finally, I've observed some odd behavior. 最后,我观察到一些奇怪的行为。 A tuple is added to blog with blogdate as "2009-12-1" (which I treat as a string because there's not date or time type in SQLite). 将一个元组添加到blog ,其blog blogdate为“ 2009-12-1”(我将其视为字符串,因为SQLite中没有日期或时间类型)。 When I run the PHP file with the above code, the entry with 2009-12-1 as blogdate does not show up. 当我使用上述代码运行PHP文件时,以2009-12-1作为blogdate的条目不会显示。 I ran another PHP page that searches for tuples with blogdate LIKE 2009-12-1, and it showed up in the search results. 我运行了另一个PHP页面,该blogdate LIKE页面的blogdate LIKE 2009-12-1,以搜索元组,它显示在搜索结果中。 Only then did the tuple for 2009-12-1 show up when I SELECT * d for it using the PHP above. 只有这样,当我使用上述PHP为SELECT * d时,才会显示2009-12-1的元组。

The text of a record may show up when you view a DB file in a text/hex editor even though it may have been marked as deleted in the database. 当您在文本/十六进制编辑器中查看数据库文件时,记录的文本可能会显示,即使在数据库中可能已将其标记为已删除。

To fully remove these deleted records, try compacting the database. 要完全删除这些删除的记录,请尝试压缩数据库。

Viewing the binary database file is insufficient to show that a record actually exists in a database. 查看二进制数据库文件不足以显示数据库中实际存在记录。 Many databases don't bother actually removing the data from the data file, as that would be a waste of time. 许多数据库实际上并不在意从数据文件中删除数据,因为那样会浪费时间。 Instead, they may flag the block as "deleted", and overwrite it later when saving additional data. 相反,他们可以将块标记为“已删除”,并在以后保存其他数据时覆盖它。 So, you assertion that the record is in the database because you can see it in the .db file means nothing, you need to open the file in a program designed to browse the contents of the database and see if it shows up. 因此,您断言该记录在数据库中,因为您可以在.db文件中看到该记录,这意味着什么,您需要在旨在浏览数据库内容并查看其是否显示的程序中打开该文件。

为了检查的数据在数据库中的断言 ,建议您在数据库浏览器中打开数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM