Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an SQLite table only if it doesn't already exist

Tags:

I need my PHP app to be able to create an SQLite table but only if it doesn't already exist. How should I go about it?

like image 980
Emanuil Rusev Avatar asked Sep 15 '10 09:09

Emanuil Rusev


People also ask

Which command creates a database only if it does not already exist?

The IF NOT EXISTS clause prevents you from an error of creating a new database that already exists in the database server. You cannot have 2 databases with the same name in a MySQL database server.

How can you delete the existing records from a table in SQLite?

SQLite DELETE query is used to remove existing records from a specified table. You can use the WHERE clause with DELETE queries to delete the selected rows. You have to write a table name after the DELETE FROM clause, from which you want to delete records.

What happens if you create a table that already exists?

When you try to create a table with an already existing table name, you will receive an error message, and no table will be modified or created.


1 Answers

You can use:

CREATE TABLE IF NOT EXISTS <name> (   /* definition */ ) 

Which is supported by SQLite (http://www.sqlite.org/syntaxdiagrams.html#create-table-stmt)

like image 176
halfdan Avatar answered Oct 28 '22 01:10

halfdan