A
A
Alexander Tokmakov2014-12-08 23:16:34
PHP
Alexander Tokmakov, 2014-12-08 23:16:34

How to work with .db tables?

Hello.
The point is this. I want to use the database in the datebase.db file I
did not encounter this and the problems crawled out at an early stage. Need a specific datebase.db file?
I created an empty one. Connected like this:

try {
$db = new PDO("sqlite:database.db");
}
catch(PDOException $e) {
    echo "Нет соединения с базой данных";
}

Next, I need to create one table. And fill it up.
I enter the following:
$rows = $db->exec("CREATE TABLE `testing`(
    id INT PRIMARY KEY AUTO_INCREMENT,
    fname VARCHAR(20) NOT NULL DEFAULT '',
    email VARCHAR(50) NOT NULL DEFAULT '',
    money INT NOT NULL DEFAULT 0) ENGINE=InnoDB;");

$rows = $db->exec("INSERT INTO `testing` VALUES
    (null, 'Ivan', '[email protected]', 15000),
    (null, 'Petr', '[email protected]', 411000),
    (null, 'Vasiliy', '[email protected]', 1500000)
    ");

And that's all. Nothing happens.
Please tell me how to create a table in a file.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Spirin, 2014-12-08
@calliko

ENGINE=InnoDB in your query refers to the MySQL DBMS. There is no such thing in Sqlite.
In general, it would be nice to enable correct error handling, for example, like this:

try {
    $dbh = new PDO("sqlite:database.db");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $rows = $dbh->exec("...");
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}

In general, use SQLite3 if you have PHP >=5.3

P
Push Pull, 2014-12-08
@deadbyelpy

What are you expecting?? What should be happening??
Seriously, if this is all the code that you have, then you will not have anything in the output, but if the trouble is that the file is not being created, you probably do not have the rights to create it. The biggest problem for you will be the rights, the rest will be in the output if you have it configured.

S
Sergey Lerg, 2014-12-08
@Lerg

One problem is that you are creating a SQLite database and you are trying to use the query syntax for MySQL.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question