<?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();
}
?>
我不确定为什么没有插入表中。文件'music.db'存在于正确的路径中。
对于记录,我只能使用SQlite3,不允许SQL。允许使用PHP,SQLite3也是如此。
答案 0 :(得分:0)
你看到这两行之间的区别吗?
"INSERT INTO Music(Title, Author, Release Date) VALUES
"INSERT INTO Music(Title, Author, ReleaseDate) VALUES
^
你去吧。从列名中取出那个空格,你应该没问题。
在运行代码之前,这是我的music.db的内容。
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE music (Title text, Author text, ReleaseDate text);
COMMIT;
您目前在问题中发布的代码现在无需修改即可成功运行。
答案 1 :(得分:0)
SQLite3的某些实现将每个执行命令限制为仅一个查询。例如,你不能像你所做的那样链接INSERT,但必须为每个插入调用$ db-&gt; exec()。