Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite if Table Exists Then Rename

Tags:

sqlite

Amazing, I expected to google and find an answer to this within a few seconds, but I've now spent 1/2 hour and tried many methods:

I need to rename a table if it exists, here's a couple of attempts:

var sql = "IF NOT EXISTS 'zones' ALTER TABLE 'sensors' RENAME TO 'zones';";
var sql = "ALTER TABLE if exists 'sensors' RENAME TO 'zones';";

Any idea how I can achieve this ??

like image 958
crankshaft Avatar asked Oct 23 '25 03:10

crankshaft


1 Answers

Alter table doesn't contain any such syntax: http://www.sqlite.org/lang_altertable.html

Simplest way of going about this would be to have a query finding out if the table exists, and if this returns a value then alter the table name

like image 200
Matthew Mcveigh Avatar answered Oct 27 '25 04:10

Matthew Mcveigh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!