Android SQLite - 命名参数 [英] Android SQLite - named parameters
问题描述
我正在开发使用 SQLite 作为本地存储的 Android 应用程序.我需要在 sql 查询中使用参数,但我发现的所有示例都包含未命名的参数,如下所示:
I'm working on Android application that uses SQLite as a local storage. I need to use parameters in sql query but all examples I have found contain unamed parameters, like so:
INSERT INTO SomeTable(ColA, ColB, ColC) VALUES (?,?,?);
我想知道 - Android 上的 SQLite 是否支持命名参数?像这样的东西而不是问号..
I'm wondering - does SQLite on Android supports named parameters? Something like this instead of question marks..
INSERT INTO SomeTable(ColA, ColB, ColC) VALUES (@paramA, @paramB, @paramC);
SQLite 本身支持这个(根据文档 https://www.sqlite.org/lang_expr.html).
SQLite itself supports this (according to the documentation https://www.sqlite.org/lang_expr.html).
提前致谢
推荐答案
Android 数据库 API 仅允许通过索引进行参数绑定.
The Android database API allows parameter binding only by index.
这不会阻止您在 SQL 中使用命名参数,但您仍然必须使用正确的索引来绑定它们.这几乎没用,除了文档或重用参数:
This does not prevent you from using named parameters in SQL, but you still have to use the correct index to bind them. This is pretty much useless, except for documentation, or for reusing a parameter:
db.rawQuery("SELECT * FROM Tab WHERE A = @search OR B = @search",
new String[]{ search });
这篇关于Android SQLite - 命名参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!