我试图将此行从MS SQL Server移植到SQLite
IF NOT EXISTS(SELECT 1 FROM EVENTTYPE WHERE EventTypeName = 'ANI Received') INSERT INTO EVENTTYPE (EventTypeName) VALUES ('ANI Received');
似乎SQLite不支持IF NOT EXISTS或者至少我不能使它工作.我错过了一些简单的事吗?有解决方法吗?
这个怎么样?
INSERT OR IGNORE INTO EVENTTYPE (EventTypeName) VALUES 'ANI Received'
(未经测试,因为我没有SQLite ......但是这个链接非常具有描述性.)
此外,这也应该工作:
INSERT INTO EVENTTYPE (EventTypeName) SELECT 'ANI Received' WHERE NOT EXISTS (SELECT 1 FROM EVENTTYPE WHERE EventTypeName = 'ANI Received');
如果要忽略现有值的插入,表中必须有一个Key字段.只需创建一个包含主键字段的表:
CREATE TABLE IF NOT EXISTS TblUsers (UserId INTEGER PRIMARY KEY, UserName varchar(100), ContactName varchar(100),Password varchar(100));
然后在表上插入或替换/插入或忽略查询,如:
INSERT OR REPLACE INTO TblUsers (UserId, UserName, ContactName ,Password) VALUES('1','UserName','ContactName','Password');
它不会让它重新输入现有的主键值...这是如何检查表中是否存在值的方法.