219mze 发表于 2024-7-10 18:09:18

PHP运用SQLite3嵌入式关系型数据库


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="//q5.itc.cn/q_70/images03/20240409/748731f4a10642968530c8bed82d04f9.jpeg" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">介绍 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">SQLite是一种嵌入式关系型数据库管理系统,与其他数据库管理系统(如MySQL、PostgreSQL)相比,它是基于文件的、<span style="color: black;">没</span>需服务器的数据库引擎。在PHP中,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>SQLite3扩展来操作SQLite数据库。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">安装 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">SQLite3 扩展默认启用。<span style="color: black;">准许</span>在编译时<span style="color: black;">运用</span> </span><span style="color: black;">--without-sqlite3</span>禁用。Windows 用户<span style="color: black;">必要</span>启用 <span style="color: black;">php_sqlite3.dll</span>方可<span style="color: black;">运用</span>该扩展。此扩展的 DLL 文件 <span style="color: black;">包括</span>于 Windows 版的 PHP 发行包中。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重视</span>: 自 PHP 7.4.0 起在 Windows 上的附加设置。为了使此扩展生效, DLL 文件<span style="color: black;">必要</span>能在 Windows 系统的 PATH 指示的路径下找到。<span style="color: black;">怎样</span>操作的信息,请参见题为“<span style="color: black;">怎样</span>在 Windows 中将 PHP 目录加到</span><span style="color: black;">PATH</span>中”的FAQ。虽然将 DLL 文件从 PHP 文件夹复制到 Windows 系统目录<span style="color: black;">亦</span>行,但不<span style="color: black;">意见</span><span style="color: black;">这般</span>做。此扩展<span style="color: black;">必须</span>下列文件在 <span style="color: black;">PATH</span>路径中:<span style="color: black;">libsqlite3.dll</span>.</p>
    <span style="color: black;">运用</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">连接</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在PHP中,<span style="color: black;">咱们</span>可以<span style="color: black;">运用</span></span><span style="color: black;">SQLite3</span>类来连接<span style="color: black;">SQLite3</span>数据库。<span style="color: black;">经过</span>实例化一个<span style="color: black;">SQLite3</span>对象,并传入数据库文件的路径<span style="color: black;">做为</span>参数,<span style="color: black;">能够</span>创建一个数据库连接。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$dbFile = runtime_path . DIRECTORY_SEPARATOR . tinywan.db; $database = new SQLite3($dbFile); var_dump($database);</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面的示例中,<span style="color: black;">咱们</span>连接到了名为</span><span style="color: black;">tinywan.db</span>的SQLite3数据库。<span style="color: black;">倘若</span><span style="color: black;">tinywan.db</span>不存在,SQLite3会自动创建一个空的数据库文件。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">打印输出</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">class SQLite3#2 (0) { } </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">创建表</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在SQLite3中,使用</span><span style="color: black;">SQL</span>语句来创建表。<span style="color: black;">能够</span><span style="color: black;">经过</span>调用<span style="color: black;">exec</span><span style="color: black;">办法</span>并传入创建表的<span style="color: black;">SQL</span>语句来创建表。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面的代码展示了<span style="color: black;">怎样</span>在SQLite3数据库中创建一个名为</span><span style="color: black;">resty_user</span>的表</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$database-&gt;exec(CREATE TABLE IF NOT EXISTS resty_user (id INTEGER PRIMARY KEY, username TEXT, age INTEGER));</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面的示例中,创建了一个<span style="color: black;">拥有</span></span><span style="color: black;">id</span>、<span style="color: black;">username</span>和<span style="color: black;">age</span>字段的<span style="color: black;">resty_user</span>表。<span style="color: black;">id</span>字段被设置为主键,并自动递增。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">插进</span>数据</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在SQLite3中,<span style="color: black;">咱们</span><span style="color: black;">运用</span>SQL语句的</span><span style="color: black;">INSERT INTO</span>语句来<span style="color: black;">插进</span>数据。<span style="color: black;">能够</span><span style="color: black;">经过</span>调用<span style="color: black;">exec</span><span style="color: black;">办法</span>并传入相应的SQL语句来<span style="color: black;">插进</span>数据。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面的代码展示了<span style="color: black;">怎样</span>向</span><span style="color: black;">resty_user</span>表中<span style="color: black;">插进</span>一条数据:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$database-&gt;exec("INSERT INTO resty_user (username, age) VALUES (Tinywan, 24)"); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面的示例中,<span style="color: black;">咱们</span>向</span><span style="color: black;">resty_user</span>表中<span style="color: black;">插进</span>了一条姓名为<span style="color: black;">Tinywan</span>、年龄为<span style="color: black;">24</span>的数据。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">查找</span>数据</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在SQLite3中,<span style="color: black;">咱们</span><span style="color: black;">运用</span>SQL语句的</span><span style="color: black;">SELECT</span>语句来<span style="color: black;">查找</span>数据。<span style="color: black;">能够</span><span style="color: black;">经过</span>调用<span style="color: black;">query</span><span style="color: black;">办法</span>并传入相应的<span style="color: black;">SQL</span>语句来<span style="color: black;">查找</span>数据。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面的代码展示了<span style="color: black;">怎样</span>从</span><span style="color: black;">resty_user</span>表中<span style="color: black;">查找</span>所有数据:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$result = $database-&gt;query(SELECT * FROM resty_user); while ($row = $result-&gt;fetchArray) { echo ID: . $row . , username: .$row . , age: . $row . PHP_EOL; }</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面的示例中,从</span><span style="color: black;">resty_user</span>表中<span style="color: black;">查找</span>了所有数据,并<span style="color: black;">经过</span>循环遍历结果集来输出<span style="color: black;">查找</span>结果。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ID: 1, username: Tinywan, age: 24 ID: 2, username: Tinywan, age: 24 ID: 3, username: Tinywan, age: 24 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">更新数据</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在</span><span style="color: black;">SQLite3</span>中,<span style="color: black;">咱们</span><span style="color: black;">运用</span>SQL语句的<span style="color: black;">UPDATE</span>语句来更新数据。<span style="color: black;">能够</span><span style="color: black;">经过</span>调用<span style="color: black;">exec</span><span style="color: black;">办法</span>并传入相应的SQL语句来更新数据。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面的代码展示了<span style="color: black;">怎样</span>更新</span><span style="color: black;">resty_user</span>表中的数据:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$database-&gt;exec("UPDATE resty_user SET age = 25 WHERE id = 1");</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面的示例中,将</span><span style="color: black;">resty_user</span>表中<span style="color: black;">id</span>为<span style="color: black;">1</span>的数据的年龄更新为<span style="color: black;">25</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">删除数据</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在SQLite3中,<span style="color: black;">咱们</span><span style="color: black;">运用</span>SQL语句的</span><span style="color: black;">DELETE FROM</span>语句来删除数据。<span style="color: black;">能够</span><span style="color: black;">经过</span>调用<span style="color: black;">exec</span><span style="color: black;">办法</span>并传入相应的SQL语句来删除数据。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面的代码展示了<span style="color: black;">怎样</span>从</span><span style="color: black;">resty_user</span>表中删除<span style="color: black;">id</span>为<span style="color: black;">1</span>的数据:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$database-&gt;exec(DELETE FROM resty_user WHERE id = 1);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">关闭数据库连接</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">运用</span>完SQLite3数据库之后,<span style="color: black;">咱们</span>应该关闭与数据库的连接,以释放资源。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面的代码展示了<span style="color: black;">怎样</span>关闭与SQLite3数据库的连接:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$database-&gt;close; <span style="color: black;">经过</span>IDE数据库管理器查看数据</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://q1.itc.cn/q_70/images03/20240409/3292c411338741b4af4f4d43d89c3d8d.png" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">总结 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">经过</span>本教程,<span style="color: black;">认识</span>了<span style="color: black;">怎样</span><span style="color: black;">经过</span>PHP扩展SQLite来进行数据库管理。学习了<span style="color: black;">怎样</span>创建数据库、创建表格、<span style="color: black;">插进</span>数据、<span style="color: black;">查找</span>数据、更新数据和删除数据。SQLite是一个强大又简单的数据库<span style="color: black;">处理</span><span style="color: black;">方法</span>,适用于小型项目和快速<span style="color: black;">研发</span>。<span style="color: black;">期盼</span>本教程对您有所<span style="color: black;">帮忙</span>!</span><a style="color: black;"><span style="color: black;">返回<span style="color: black;">外链论坛:www.fok120.com</span>,查看<span style="color: black;">更加多</span></span></a></p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">责任编辑:网友投稿</span></p>




4lqedz 发表于 2024-10-12 23:05:15

说得好啊!我在外链论坛打滚这么多年,所谓阅人无数,就算没有见过猪走路,也总明白猪肉是啥味道的。

m5k1umn 发表于 2024-10-29 20:02:39

“NB”(牛×的缩写,表示叹为观止)‌

nykek5i 发表于 2024-11-5 04:39:23

外贸论坛是我们的,责任是我们的,荣誉是我们的,成就是我们的,辉煌是我们的。

m5k1umn 发表于 2024-11-14 09:53:02

你的言辞如同繁星闪烁,点亮了我心中的夜空。

7wu1wm0 发表于 前天 07:14

软文发布平台 http://www.fok120.com/
页: [1]
查看完整版本: PHP运用SQLite3嵌入式关系型数据库