SQLite - create table if not exists

混江龙づ霸主 提交于 2019-12-04 04:53:49

问题


What is the create table statement in SQLite meant to return?

I have observed create table if not exists returning both a 0 and 1 when the table does in fact exist. Is the return value a reliable indication of whether the table does exist or not? I would expect the statement to return a 0 if the table already exists and a 1 when it does not, similar to an insert statement.


回答1:


Changes returns the number of affected rows. This values is meaningless for CREATE TABLE statements.

There is no easy way to determine whether the CREATE TABLE IF NOT EXISTS statement did the creation or not. You should check beforehand with PRAGMA table_info.




回答2:


The if not exists syntax makes the command succeed even if the table already exists. It just doesn't do anything.

I'm not sure what you're referring to with "returns 1" unless you're talking about the command-line client. In that case, if you just remove the if not exists from your create statement, the command will return 1 (indicating failure) if the table exists.



来源:https://stackoverflow.com/questions/24785584/sqlite-create-table-if-not-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!