<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Soul Of Free Loop &#187; sqlite3_prepare</title>
	<atom:link href="https://zohead.com/archives/tag/sqlite3_prepare/feed" rel="self" type="application/rss+xml" />
	<link>https://zohead.com</link>
	<description>Uranus Zhou&#039;s Blog</description>
	<lastBuildDate>Sat, 19 Jul 2025 15:42:46 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>lighttpd的SQLite问题</title>
		<link>https://zohead.com/archives/lighty-sqlite-err/</link>
		<comments>https://zohead.com/archives/lighty-sqlite-err/#comments</comments>
		<pubDate>Sun, 07 Sep 2014 14:55:19 +0000</pubDate>
		<dc:creator><![CDATA[Uranus Zhou]]></dc:creator>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[sqlite3_prepare]]></category>
		<category><![CDATA[WebDAV]]></category>

		<guid isPermaLink="false">http://zohead.com/?p=786</guid>
		<description><![CDATA[最近在使用 lighttpd 的 WebDAV 插件拷贝文件时遇到比较奇怪的 SQLite 库报错问题，WebDAV 插件需要使用 SQLite 数据库保存文件锁、文件属性等信息，报错信息如下： (mod_webdav.c.2182) sql-set failed: SQL logic error or missing database(mod_webdav.c.2182) sql-set failed: not an error(mod_webdav.c.2182) sql-set failed: not an error(mod_webdav.c.2182) sql-set failed: [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>最近在使用 lighttpd 的 WebDAV 插件拷贝文件时遇到比较奇怪的 SQLite 库报错问题，WebDAV 插件需要使用 SQLite 数据库保存文件锁、文件属性等信息，报错信息如下：</p>
<p>(mod_webdav.c.2182) sql-set failed: SQL logic error or missing database<br />(mod_webdav.c.2182) sql-set failed: not an error<br />(mod_webdav.c.2182) sql-set failed: not an error<br />(mod_webdav.c.2182) sql-set failed: not an error<br />(mod_webdav.c.2182) sql-set failed: not an error<br />(mod_webdav.c.2182) sql-set failed: not an error<br />(mod_webdav.c.2182) sql-set failed: not an error<br />(mod_webdav.c.2182) sql-set failed: not an error<br />(mod_webdav.c.2511) remove lock: bind or column index out of range<br />(mod_webdav.c.2511) remove lock: bind or column index out of range<br />(mod_webdav.c.2511) remove lock: bind or column index out of range</p>
<p>我们先看看 mod_webdav.c 中对应的 SQLite 处理代码：</p>
<p><pre class="brush: cpp; highlight: [17,18,19,20,43]; title: mod_webdav.c; notranslate">
sqlite3_stmt *stmt;

stmt = (0 == xmlStrcmp(cmd-&gt;name, BAD_CAST &quot;remove&quot;)) ?
	p-&gt;conf.stmt_delete_prop : p-&gt;conf.stmt_update_prop;

for (props = cmd-&gt;children; props; props = props-&gt;next) {
	if (0 == xmlStrcmp(props-&gt;name, BAD_CAST &quot;prop&quot;)) {
		xmlNode *prop;
		int r;

		prop = props-&gt;children;

		sqlite3_reset(stmt);

		/* bind the values to the insert */

		sqlite3_bind_text(stmt, 1,
				  con-&gt;uri.path-&gt;ptr,
				  con-&gt;uri.path-&gt;used - 1,
				  SQLITE_TRANSIENT);
		sqlite3_bind_text(stmt, 2,
				  (char *)prop-&gt;name,
				  strlen((char *)prop-&gt;name),
				  SQLITE_TRANSIENT);
		if (prop-&gt;ns) {
			sqlite3_bind_text(stmt, 3,
					  (char *)prop-&gt;ns-&gt;href,
					  strlen((char *)prop-&gt;ns-&gt;href),
					  SQLITE_TRANSIENT);
		} else {
			sqlite3_bind_text(stmt, 3,
					  &quot;&quot;,
					  0,
					  SQLITE_TRANSIENT);
		}
		if (stmt == p-&gt;conf.stmt_update_prop) {
			sqlite3_bind_text(stmt, 4,
				  (char *)xmlNodeGetContent(prop),
				  strlen((char *)xmlNodeGetContent(prop)),
				  SQLITE_TRANSIENT);
		}

		if (SQLITE_DONE != (r = sqlite3_step(stmt))) {
			log_error_write(srv, __FILE__, __LINE__, &quot;ss&quot;,
					&quot;sql-set failed:&quot;, sqlite3_errmsg(p-&gt;conf.sql));
		}
	}
}
</pre>
</p>
<p>这里用到了 SQLite 的绑定数据 sqlite3_bind_text 和 sqlite3_step 执行函数。</p>
<p>同时我们可以看看 mod_webdav.c 中打开 SQLite 数据库以及 SQLite 变量绑定方式的 SQL 处理代码：</p>
<p><pre class="brush: cpp; highlight: [11,12,31,32,44,45,65,66]; title: mod_webdav.c; notranslate">
const char *next_stmt;
char *err;

if (SQLITE_OK != sqlite3_open(s-&gt;sqlite_db_name-&gt;ptr, &amp;(s-&gt;sql))) {
	log_error_write(srv, __FILE__, __LINE__, &quot;sbs&quot;, &quot;sqlite3_open failed for&quot;,
			s-&gt;sqlite_db_name,
			sqlite3_errmsg(s-&gt;sql));
	return HANDLER_ERROR;
}

if (SQLITE_OK != sqlite3_exec(s-&gt;sql,
		&quot;CREATE TABLE properties (&quot;
		&quot;  resource TEXT NOT NULL,&quot;
		&quot;  prop TEXT NOT NULL,&quot;
		&quot;  ns TEXT NOT NULL,&quot;
		&quot;  value TEXT NOT NULL,&quot;
		&quot;  PRIMARY KEY(resource, prop, ns))&quot;,
		NULL, NULL, &amp;err)) {

	if (0 != strcmp(err, &quot;table properties already exists&quot;)) {
		log_error_write(srv, __FILE__, __LINE__, &quot;ss&quot;, &quot;can't open transaction:&quot;, err);
		sqlite3_free(err);

		return HANDLER_ERROR;
	}
	sqlite3_free(err);
}

...

if (SQLITE_OK != sqlite3_prepare(s-&gt;sql,
	CONST_STR_LEN(&quot;REPLACE INTO properties (resource, prop, ns, value) VALUES (?, ?, ?, ?)&quot;),
	&amp;(s-&gt;stmt_update_prop), &amp;next_stmt)) {
	/* prepare failed */

	log_error_write(srv, __FILE__, __LINE__, &quot;ss&quot;, &quot;sqlite3_prepare failed:&quot;, sqlite3_errmsg(s-&gt;sql));
	return HANDLER_ERROR;
}

...

/* LOCKS */

if (SQLITE_OK != sqlite3_exec(s-&gt;sql,
		&quot;CREATE TABLE locks (&quot;
		&quot;  locktoken TEXT NOT NULL,&quot;
		&quot;  resource TEXT NOT NULL,&quot;
		&quot;  lockscope TEXT NOT NULL,&quot;
		&quot;  locktype TEXT NOT NULL,&quot;
		&quot;  owner TEXT NOT NULL,&quot;
		&quot;  depth INT NOT NULL,&quot;
		&quot;  timeout TIMESTAMP NOT NULL,&quot;
		&quot;  PRIMARY KEY(locktoken))&quot;,
		NULL, NULL, &amp;err)) {

	if (0 != strcmp(err, &quot;table locks already exists&quot;)) {
		log_error_write(srv, __FILE__, __LINE__, &quot;ss&quot;, &quot;can't open transaction:&quot;, err);
		sqlite3_free(err);

		return HANDLER_ERROR;
	}
	sqlite3_free(err);
}

if (SQLITE_OK != sqlite3_prepare(s-&gt;sql,
	CONST_STR_LEN(&quot;INSERT INTO locks (locktoken, resource, lockscope, locktype, owner, depth, timeout) VALUES (?,?,?,?,?,?, CURRENT_TIME + 600)&quot;),
	&amp;(s-&gt;stmt_create_lock), &amp;next_stmt)) {
	/* prepare failed */
	log_error_write(srv, __FILE__, __LINE__, &quot;ss&quot;, &quot;sqlite3_prepare failed&quot;, sqlite3_errmsg(s-&gt;sql));

	return HANDLER_ERROR;
}

...
</pre>
</p>
<p>从上面的代码可以看出，lighttpd 打开 SQLite 数据库之后先创建 properties 表格，然后使用 sqlite3_prepare 函数进行 properties 表格的 SQL 语句预处理，接着创建 locks 表格，同时也使用 sqlite3_prepare 对 locks 表格进行预处理。</p>
<p>使用 sqlite3_prepare 函数而不直接使用 sqlite3_exec 函数的好处是可以直接将 SQL 语句转换为 SQLite 内部的字节码，后面使用时不需要再直接使用冗长的 SQL 语句，直接绑定数据就可以查询或者更新数据了。这里就比较奇怪了，stmt_update_prop 的 SQL 语句看起来也没有什么问题。首先把疑点放在 sqlite3_bind_text 上，怀疑是不是数据中有没有什么特殊字符导致 sqlite3_step 函数执行出错了，但我把 sqlite3_bind_text 的内容全部改成普通字符串还是一样的报错。</p>
<p>搜寻一番之后终于在 sqlite3.h 头文件中发现了端倪：</p>
<p><pre class="brush: cpp; title: sqlite3.h; notranslate">
** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
** recommended for all new programs. The two older interfaces are retained
** for backwards compatibility, but their use is discouraged.、
**
** In the &quot;v2&quot; interfaces, the prepared statement
** that is returned (the [sqlite3_stmt] object) contains a copy of the
** original SQL text. This causes the [sqlite3_step()] interface to
** behave differently in three ways:
**
** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
** always used to do, [sqlite3_step()] will automatically recompile the SQL
** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
** retries will occur before sqlite3_step() gives up and returns an error.
**
** When an error occurs, [sqlite3_step()] will return one of the detailed
** [error codes] or [extended error codes]. The legacy behavior was that
** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
** and the application would have to make a second call to [sqlite3_reset()]
** in order to find the underlying cause of the problem. With the &quot;v2&quot; prepare
** interfaces, the underlying reason for the error is returned immediately.
</pre>
</p>
<p>sqlite3.h 中建议使用新的 sqlite3_prepare_v2 函数替代老的 sqlite3_prepare 函数，而且中间的注释中还提到使用 sqlite3_prepare 函数之后如果数据库的结构发生了变动，sqlite3_step 函数将会出错。这时我们看看 mod_webdav.c 中初始化数据库的代码可以发现 sqlite3_prepare 函数是在两个 sqlite3_exec 函数创建数据库之间运行的，这样 sqlite3_prepare 生成的字节码就不是正确的，才导致后续的 SQL 更新操作失败。</p>
<p>知道原因之后修改也比较简单了，我们可以修改 mod_webdav.c 中的数据库初始化代码，将两个 sqlite3_exec 创建数据库的操作放在最前面，将所有 sqlite3_prepare 替换为 sqlite3_prepare_v2 函数放在创建数据库之后，这样就不会出现 SQLite 数据库错误问题了。</p>
<p>本文为个人使用分析的结果，其中有任何问题欢迎提出指正，另外 lighttpd 最新版本库代码中这个问题似乎仍然没有修正。</p>
]]></content:encoded>
			<wfw:commentRss>https://zohead.com/archives/lighty-sqlite-err/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
