在内存中创建 SQLite 数据库

发布于 2024-08-24 17:48:05 字数 716 浏览 1 评论 0 原文

尝试了解一些有关 PDO 的知识,并正在阅读本教程。它具有以下代码片段:

<?php

  try
  {
    $db = new PDO('sqlite::memory');
    echo "SQLite created in memory.";
  }
  catch(PDOException $e)
  {
    echo $e->getMessage();
  }

当我运行此代码时,我收到以下异常消息:

SQLSTATE[HY000] [14] 无法打开数据库文件

是什么意思?我怎样才能让它发挥作用?我能够连接到 MySQL 数据库以及常规 SQLite 数据库文件。所以我知道至少有一些东西正在工作......

我使用的是 Windows 7 64 位,带有 Apache 2.2.11 和 PHP 5.3.0(最新的 WampServer 安装)。 phpinfo() 报告我启用了启用了 SQLite 库 3.6.15 的 pdo_sqlite。

Trying to learn a bit about PDO and is going through this tutorial. It has the following snippet of code:

<?php

  try
  {
    $db = new PDO('sqlite::memory');
    echo "SQLite created in memory.";
  }
  catch(PDOException $e)
  {
    echo $e->getMessage();
  }

When I run this I get the following exception message:

SQLSTATE[HY000] [14] unable to open database file

What does that mean? How can I get it to work? I am able to connect to a MySQL database and also a regular SQLite database file. So I know at least something is working...

I'm on Windows 7 64-bit with Apache 2.2.11 and PHP 5.3.0 (latest WampServer install). phpinfo() reports that I have pdo_sqlite with SQLite Library 3.6.15 enabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪之魂 2024-08-31 17:48:05

您必须编写

$db = new PDO('sqlite::memory:');

缺少尾随 : 的内容。


PDO_SQLITE 数据源名称 (DSN) 由以下元素组成:

DSN 前缀是 sqlite:

  • 要访问磁盘上的数据库,请将绝对路径附加到 DSN 前缀。

  • 要在内存中创建数据库,请将 :memory: 附加到 DSN 前缀。

文档

You have to write

$db = new PDO('sqlite::memory:');

the trailing : is missing.


The PDO_SQLITE Data Source Name (DSN) is composed of the following elements:

The DSN prefix is sqlite:.

  • To access a database on disk, append the absolute path to the DSN prefix.

  • To create a database in memory, append :memory: to the DSN prefix.

Documentation

泡沫很甜 2024-08-31 17:48:05

不确定 @ win 权限,但在 *nix 上,SQLite 需要对临时文件的数据库文件目录进行写入权限。

Not sure @ win permissions, but on *nix, SQLite needs write permision to the dir confining database files for temp files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文