当检查表是否存在时,Sqlite3错误:no such table
sqlite3 error: no such table: when checking if table exists
我已经读了几个帖子,但仍然不知道这里有什么问题。我有一个c++包装器来调用sqlite。我想在创建表之前测试表是否存在,通过读取check-in-sqlite-whether-a-table-exists,我使用以下sql语句检查表是否存在
"SELECT name FROM test.db WHERE type='table' AND name='table1';"
主代码如下:
static int callback(void *db, int argc, char **argv, char **azColName){
for(int i=0; i<argc; ++i){
printf("%s = %sn", azColName[i], argv[i] ? argv[i] : "NULL");
}
return 0;
}
class SqliteAccessor {
public:
open(){ sqlite3_open("test.db", &m_db); }
createTable1(){
string sql = "CREATE TABLE table1(one TEXT);";
char *zErrMsg = 0;
int rc = sqlite3_exec(m_db, sql.c_str(), callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
printf("SQL error: %s", zErrMsg);
sqlite3_free(zErrMsg);
}
}
hasTable1(){
string sql = "SELECT name FROM test.db WHERE type='table' AND name='table1'";
char *zErrMsg = 0;
int rc = sqlite3_exec(m_db, sql.c_str(), callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
printf("SQL error: %s", zErrMsg); // Error: no such table: test.db
sqlite3_free(zErrMsg);
}
}
private:
sqlite3* m_db;
}
main(){
SqliteAccessor sql;
sql.open(); // success;
sql.createTable1(); // success;
sql.hasTable1(); // fail
}
我也尝试了cli api:
sqlite3 test.db
sqlite> create table table1(one varchar(10));
sqlite> SELECT * FROM test.db;
Error: no such table: test.db
sqlite> SELECT name FROM test.db WHERE type='table' AND name='table1';
Error: no such table: test.db
// however, if I run .tables, then it is there.
sqlite> .tables
table1
是同样的错误,但是为什么会有这样的错误呢?这是最新的sqlite合并版本。
查看sqlite_master。
SELECT * FROM sqlite_master WHERE name LIKE '%your_table_name%'
相关文章:
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 学习 c++,编译错误"no matching function for call"
- 尝试查找两组点的交集,错误"No viable overloaded '=' "
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 如何修复以下错误"no match for call to '(std::tr1::shared_ptr<_iobuf*>) (FILE*&)'"
- 函数模板不起作用,出现错误"No suitable user-defined conversion"
- GDB 列表错误"No such file or directory"
- 运算符重载错误"no match for operator error"
- 调用布尔函数但收到错误"no matching function to call"?
- 为什么编译器显示错误"no match for 'operator[]' (operand types are 'carti' and 'int')"
- 模板化重载运算符的编译错误"No global operator found"
- 意外错误"no matching function for call to",需要没有参数的构造函数
- C++ 为什么当错误看起来 100% 匹配时会出现错误"no matching function"?
- C++向量和错误"no instance of overloaded function"
- C++结构错误"No matching function for call..."
- 提升精神语法错误 - "no type named ‘size’ in ‘struct boost::spirit::unused_type’"
- 错误"No Entry point"
- 为什么我在 Linux 上收到错误"no matching function for call to A::A(A)",而在 Windows 上没有
- Clang 输出错误"no matching construct for initialization"
- 接收错误"no matching function for call to 'getline(std::ifstream&, int&, char)'"