Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite syntax not compatible with MySQL?

I'm using PDO and trying to make my application support both MySQL and SQLite, but in sqlite I get this error when I try to import my database schema:

SQLSTATE[HY000]: General error: 1 near "AUTO_INCREMENT": syntax error

The query looks like this:

CREATE TABLE events (

  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(32) NOT NULL,
  title VARCHAR(64) NOT NULL,
  description LONGTEXT,
  starttime DATETIME DEFAULT '0000-00-00 00:00:00',

  PRIMARY KEY(id),
  KEY name(name)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

(and it works in a MySQL database.)

I don't understand what the problem is here? Shouldn't both database systems be compatible?

like image 811
Alex Avatar asked Feb 11 '12 21:02

Alex


People also ask

Do MySQL and SQLite use same syntax?

Both MySQL and SQLite are forms of a Relational Database Management System (RDBMS). These are based on the Structured Query Language (SQL). In addition, the syntax of each can be similar, and, in my opinion, it is not too difficult to learn one after the other.

Is SQLite syntax different from MySQL?

They are completely different. SQLite is a public domain, open-source project. It is what is called an “embedded” database which means the DB engine runs as part of your app. MySQL is also open-source but is owned by Oracle.

What is not supported by SQLite?

The SQLite database engine does not support a number of schema operations that are supported by the majority of other relational databases. If you attempt to apply one of the unsupported operations to a SQLite database then a NotSupportedException will be thrown.

What syntax does SQLite use?

All the SQLite statements start with any of the keywords like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, etc., and all the statements end with a semicolon (;).


4 Answers

http://www.sqlite.org/autoinc.html

In SQLite it's called AUTOINCREMENT, not AUTO_INCREMENT

like image 75
Vilius Sutkus '89 Avatar answered Sep 30 '22 18:09

Vilius Sutkus '89


They should be compatible as regards the ANSI SQL standards, and all SQL databases should adhere to that. However, AutoIncrement is not a part of that standard, but an extra feature implemented by some databases (including MySQL). Not all databases provide that feature, or may provide it in a different manner, or with different syntax.

like image 37
Mark Baker Avatar answered Sep 30 '22 17:09

Mark Baker


AUTO_INCREMENT is MySQL-specific. SQLite apparently has a similar thing, AUTOINCREMENT.

like image 43
cHao Avatar answered Sep 30 '22 19:09

cHao


Unfortunately though SQL should be a standard, each database implementation is different and have its own peculiarities, so you have to arrange your Query to make it work on SQLite.

like image 26
aleroot Avatar answered Sep 30 '22 18:09

aleroot