如何使用 php 在 xampp 中使用 sqlite 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6951824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use sqlite database with xampp using php
提问by uday
How to install sqlite or sqlite 3 database with xampp server and how to use sqlite or sqlite 3 database using php scripts or programs...
如何使用 xampp 服务器安装 sqlite 或 sqlite 3 数据库以及如何使用 php 脚本或程序使用 sqlite 或 sqlite 3 数据库...
回答by vstm
Creating the sqlite Database
创建 sqlite 数据库
You have two possibilities to achieve that:
您有两种可能性来实现这一目标:
Writing your DDL-statements (CREATE TABLE...) in a .sql file and execute it using the sqlite command line (assuming your CREATE TABLE statements are in a file called tabledef.sql):
cat tabledef.sql | sqlite3 yourdbname.db
- Use PHP to execute the DDL-statements on a connected database (see "Executing statements").
在 .sql 文件中编写 DDL 语句(CREATE TABLE...)并使用 sqlite 命令行执行它(假设您的 CREATE TABLE 语句位于名为 tabledef.sql 的文件中):
cat tabledef.sql | sqlite3 yourdbname.db
- 使用 PHP 在连接的数据库上执行 DDL 语句(请参阅“执行语句”)。
Connecting to a sqlite Database
连接到一个 sqlite 数据库
You should definitely use PDO to do that:
你绝对应该使用 PDO 来做到这一点:
$dbh = new PDO('sqlite:/path/to/your/database.db', '', '', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
));
If the database does not exist, then it is created. But you need write-access to the directory that contains the database. If you permit write-access only to the database file, sqlite fails because it also needs to create a lock file in the same directory.
如果数据库不存在,则创建它。但是您需要对包含数据库的目录进行写访问。如果您只允许对数据库文件进行写访问,sqlite 将失败,因为它还需要在同一目录中创建一个锁定文件。
Executing statements
执行语句
Now that you have a connection you can do your stuff with the database, for example execute some DDL-statements:
现在你有了一个连接,你可以对数据库做你的事情,例如执行一些 DDL 语句:
$dbh->exec("CREATE TABLE IF NOT EXISTS mytable (
mypk INTEGER PRIMARY KEY AUTOINCREMENT,
myvalue TEXT);");
Or if you need to dynamically generate SQL statements, use prepared statements:
或者如果你需要动态生成 SQL 语句,使用准备好的语句:
$statement = $dbh->prepare("INSERT INTO mytable (myvalue) VALUES(?)");
$statement->execute(array("hello"));
$statement->execute(array("world"));
This is only a small overview, for further information you should check out the PDO manualand the sqlite Documentation.
回答by Jorg
Well to see if it's installed, check phpinfo() and see if the drivers for sqlite are installed (search the list for "sqlite"), it will mention sqlite and and sqlite 3 seperately. I think they both come preinstalled with XAMPP so it should just work. Then check the PHP manual for sqlite functions, or use the PDO wrappers. Google is your friend.
好吧,看看它是否已安装,检查 phpinfo() 并查看是否安装了 sqlite 的驱动程序(在列表中搜索“sqlite”),它会分别提到 sqlite 和 sqlite 3。我认为它们都预装了 XAMPP,所以它应该可以正常工作。然后检查SQLite 函数的 PHP 手册,或使用 PDO 包装器。谷歌是你的朋友。
回答by zeuf
To activate SQLite3 in Xampp (v3.2.2). Open xampp/php/php.ini
, un-comment the line ;extension=sqlite3
(retrieve the ;), save the php.ini
and restart your Xampp server.
在 Xampp (v3.2.2) 中激活 SQLite3。打开xampp/php/php.ini
,取消注释该行;extension=sqlite3
(检索 ;),保存php.ini
并重新启动您的 Xampp 服务器。
回答by ddalu5
You can use a database wrapper, that can make things much easier.
您可以使用数据库包装器,这可以使事情变得更容易。
try this one, i made it :
试试这个,我做到了:
<?php
Class Sqlite3_Wrapper
{
private $sFileName, $oCon=null, $iDebug, $oResult;
private $sDefaultFile = 'default.db';
public function __construct($sFileName, $iDebug=false)
{
$this->setFileName($sFileName);
}
private function showMessage($sMsg)
{
if($this->iDebug != false)
{
$sMessage = (php_sapi_name() == 'cli')?$sMsg."\n":$sMsg.'<br />' ;
echo $sMessage;
}
}
public function setFileName($sFileName)
{
if(is_string($sFileName) && strlen($sFileName)>0)
{
$this->sFileName = $sFileName;
}
else
{
$this->sFileName = $this->sDefaultFile;
}
}
public function openDB($sFileName=false)
{
if($sFileName!==false)
{
$this->setFileName($sFileName);
}
try
{
$this->oCon = new SQLite3($this->sFileName);
}catch(Exception $oEx)
{
$this->showMessage($ex->getMessage());
}
}
public function closeDB()
{
if($this->oCon!=null)
{
$this->oCon->close();
$this->oCon = null;
}
}
public function executeCUDQuery($sQuery)
{
if($this->oCon==null)
{
$this->showMessage('Not connected to database');
}
else
{
try
{
$this->oCon->exec($sQuery);
}catch(Exception $ex)
{
$this->showMessage($ex->getMessage());
}
}
}
public function executeQuery($sQuery)
{
$this->oResult = null;
if($this->oCon==null)
{
$this->showMessage('Not connected to database');
}
else{
try
{
$this->oResult = $this->oCon->query($sQuery);
}catch(Exception $oEx)
{
$this->showMessage($oEx->getMessage());
}
}
return $this->oResult;
}
public function fetch($iMode=SQLITE3_NUM)
{
$aData = array();
try
{
$aData = $this->oResult->fetchArray($iMode);
}
catch(Exception $oEx)
{
$this->showMessage($oEx->getMessage());
}
return $aData;
}
}
?>
or get the files here :
或在此处获取文件:
http://vaan3713.github.com/databaseswrappers/
http://vaan3713.github.com/databaseswrappers/
wish it will help.
希望它会有所帮助。