0
<?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 也是如此。

4

2 回答 2

0

SQLite3 的一些实现将每个执行命令限制为仅一个查询。例如,您不能像以前那样链接 INSERT,但必须为每个插入调用 $db->e​​xec()。

于 2012-06-16T02:56:16.380 回答
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;

您当前在问题中发布的代码现在无需修改即可成功运行。

于 2012-06-16T02:10:21.857 回答