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の一部の実装では、各実行コマンドを1つのクエリのみに制限しています。たとえば、これまでのようにINSERTをチェーンすることはできませんが、挿入ごとに$ db-> exec()を呼び出す必要があります。

于 2012-06-16T02:56:16.380 に答える
0

この 2 つの行の違いがわかりますか。

"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 に答える