I am trying to delete column last_name from Persons using FMDB,
let query = "ALTER TABLE Persons DROP COLUMN last_name;"
try FMDBHelper.database.executeUpdate(query, values: nil)
But comes with error
DB Error: 1 "near "DROP": syntax error".
sqlite does not support DROP COLUMN in ALTER TABLE.
You can only rename tables and add columns.
If you need to remove columns, create a new table, copy the data there, drop the old table and rename the table to its intented name.
Reference: http://www.sqlite.org/lang_altertable.html
Please note that I flagged that your question could be duplicated, I will provide an answer to make it more clear.
I think that you are missing a point, which is: The FMDB is (as mentioned in their repo description):
This is an Objective-C wrapper around SQLite
Keep in mind that since FMDB is built on top of SQLite, it is not a limitation from the library itself; it is related to how SQLite ALTER TABLE works.
The SQLite ALTER TABLE statement is limited to rename a table and add a new column to the desired table:
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE
command in SQLite allows the user to rename a table or to add a new
column to an existing table.
http://www.sqlite.org/lang_altertable.html
For achieving what are you looking for:
You could check the answers of Delete column from SQLite table.
Related
My app is in production and I want to manage user data when user updates the app without loss of their data, how can I achieve this with sqflite. Explicitly I want to add a column and delete another.
You can probably add a column using raw sql, but sqlite (and thus sqflite) doesn't support dropping a column. For that you would need to do the following:
increase the database version number
in onUpgrade copy the old database columns to a temporary table
delete the original table
create a new table using the original table name but with the right schema
copy the data from the temp table
delete the temp table
Sorry, this isn't a full answer, but it is the direction I would go if I were in your situation.
I have the same problem and found this article which seems to be a good solution.
This is how I currently have my methods in my project to use sqlite queries to return NSObjects to be used in my iOS project:
in the launch of my application, each table is checked if it needs to be created
in the application, there is no DROP TABLE .. queries nor ALTER TABLE ..
My question is:
should I be checking if a table exists every time I'm going to create a sqlite3 query?
should i use CREATE TABLE IF NOT EXISTS .. vs checking if a table exists using a different query like:
SELECT name FROM sqlite_master WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' UNION ALL SELECT name FROM sqlite_temp_master WHERE type IN ('table','view') ORDER BY 1;and iterating the names of the tables and checking if the table name exists?
I want to create the least amount of overhead. i am not using Core Data as well
Also, while updating records, i've noticed if i insert the name of a column, it takes the value of that column:
In Spotfire, I have inserted columns From an Excel File in an existing Data Table on Spotfire (See example shows at the End) but I have made a mistake in the join property. Could I change it without re creating the complete Data Table or using calculated columns/Filters?
You can only edit it by deleting the inserted columns and adding them again.
I got the data by 'MODEL.all' command in rails console
I want to put the column 'cgi_name' in the 3rd position when I run MODEL.all in the rails console
I use the postgres for my DB
How to get it ?
To answer your question directly, you'll have to move the columns at DB level
Currently, I only know MYSQL to support this functionality:
ALTER TABLE Employees CHANGE COLUMN empName empName VARCHAR(50) AFTER department;
Postgres, to my knowledge, does not support this functionality:
Many people new to postgresql often ask if it has support for altering
column positions within a table. Currently it does not; if you want to
change column positions, you must either recreate the table, or add
new columns and move data
In the view, you'll have to either manually display the columns, or create a helper method to cycle through them in an order of your choosing
Simple answer is YOU CANNOT
There is no way to re-order the column names to be displayed when you select using Model.all.
Otherwise, you can re-order this by selecting each column in the order you want.
Model.select("column1, column2, cgi_name, column4 etc..")
Hope it helps :)
What is the most efficient way to duplicate a row in an Sqlite3 database exactly except with an updated PrimaryKey?
Use an insert .. select where both the insert and select reference the same relation. Then the entire operation will occur as a single statement within SQLite code itself which cannot be beaten for "efficiency".
It will be easier with an auto-PK (just don't select the PK column), although an appropriate natural key can be assigned as well if such can be derived.
See SQLite INSERT SELECT Query Results into Existing Table?