开发者

Edit a read-only view

开发者 https://www.devze.com 2023-01-31 14:17 出处:网络
I have a column and I would like to edit some of its rows. The problem is that the table is开发者_C百科 a view so I cannot edit the rows. How would I proceed to solve this problem?SQLite doesn\'t let

I have a column and I would like to edit some of its rows. The problem is that the table is开发者_C百科 a view so I cannot edit the rows. How would I proceed to solve this problem?


SQLite doesn't let you update views. But it does allow triggers on views. So you can write an INSTEAD OF UPDATE trigger that makes the appropriate modifications to the underlying table.


As dan04 pointed out, you can use triggers to update views (like in most other databases). For sqlite, see http://www.sqlite.org/lang_createtrigger.html#instead_of_trigger

Example with a view called "myview", containing a table "my_t2"

CREATE TRIGGER myview_update INSTEAD OF UPDATE ON myview
BEGIN
  UPDATE my_t2 SET field1 = new.field1, field2 = new.field2 WHERE my_t2.key = old.key;
END


If it's a view, it's not a table. The data for the view is drawn from one or more other tables. Edit the underlying table and when you examine the data in the view it will reflect the changes you made.


A SQL View is a virtual table, which is based on SQL SELECT query. Essentially a view is very close to a real database table (it has columns and rows just like a regular table), except for the fact that the real tables store data, while the views don’t. The view’s data is generated dynamically when the view is referenced. A view references one or more existing database tables or other views. In effect every view is a filter of the table data referenced in it and this filter can restrict both the columns and the rows of the referenced tables.

If you wish to modify the data in your table, you cannot do so with a view. SQL Views are always read-only. If you want to modify your table outside of the View, then use something like an UPDATE or ALTER TABLE statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号