<?php
try
{
//open the database
$db = new PDO('sqlite:music.db');
$db->exec("DELETE from Music;");
$db->exec("INSERT INTO Music(Title, Author, ReleaseDate) VALUES ('Whatd I Say', 'Ray Charles', '1956');" .
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES ('Smells Like Teen Spirit.', 'Nirvana', '1991');" .
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES ('Hey Jude', 'The Beatles', '1968');" .
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES ('Johnny B. Goode', 'Chuck Berry', '1958');" .
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES ('Good Vibrations', 'The Beach Boys', '1966');" .
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES ('Respect', 'Aretha Franklin', '1967');" .
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES ('Whats Going On', 'Marvin Gaye', '1971');" .
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES ('Imagine', 'John Lennon', '1971');" .
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES ('(I Cant Get No) Satisfaction', 'Rolling Stones', '1965');" .
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES ('Like A Rolling Stone', 'Bob Dylan', '1965');");
//now output the data to a simple html table...
//example of specifier --> WHERE First=''Josh''; <--
print "<table border=1>";
print "<tr><td>Title</td><td>Author</td><td>Y.O.P.</td></tr>";
$result = $db->query('SELECT * FROM Music ');
foreach($result as $row)
{
print "<td>".$row['Title']."</td>";
print "<td>".$row['Author']."</td>";
print "<td>".$row['ReleaseDate']."</td></tr>";
}
print "</table>";
$db = NULL;
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
我不确定为什么没有在表格中插入任何东西。文件"音乐.db"存在于正确的路径中。
作为记录,我只能使用SQlite3,不允许使用SQL。PHP 是允许的,SQLite3 也是允许的。
SQLite3 的某些实现将每个执行命令限制为只有一个查询。例如,您不能像以前那样链接 INSERT,但必须为每个插入调用 $db->exec()。
你看出这两行之间的区别了吗?
"INSERT INTO Music(Title, Author, Release Date) VALUES
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES
^
给你。从列名中删除该空格,您应该没问题。
这是我的音乐内容.db在运行代码之前。
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE music (Title text, Author text, ReleaseDate text);
COMMIT;
您当前在问题中发布的代码现在无需在此处修改即可成功运行。