如何使用sqlite3。c++项目中的c ?

时间:2023-01-13 10:44:54

I am attempting to use sqlite3 in a C++ project in Eclipse and have found a great deal of advice online on using the API, but unfortunately am falling at an earlier hurdle. I guess this is due to my lack of experience with C/C++ and CDT. I've simply copied sqlite3.c and sqlite3.h into the project's source folder and have a test method which is as follows:

我正在尝试在Eclipse的c++项目中使用sqlite3,并在网上找到了关于使用API的大量建议,但不幸的是,我遇到了更早的障碍。我想这是因为我在C/ c++和CDT方面缺乏经验。我简单地拷贝sqlite3。c和sqlite3。h进入项目源文件夹,并有如下测试方法:

int main() {
    sqlite3* db;
    sqlite3** dbpointer = &db;
    const char* dbname = "test.db";
    sqlite3_open(dbname, dbpointer);
    return 0;
}

However, the sqlite3.c file shows up in Eclipse with numerous errors. For example, the following section is annotated with 'Field 'IN_DECLARE_VTAB' could not be resolved'.

然而,sqlite3。c文件在Eclipse中出现了许多错误。例如,下面的部分用'Field 'IN_DECLARE_VTAB'无法解析'注释。

#ifdef SQLITE_OMIT_VIRTUALTABLE
  #define IN_DECLARE_VTAB 0
#else
  #define IN_DECLARE_VTAB (pParse->declareVtab)
#endif

When I try to compile I get a series of errors like:

当我试图编译时,我得到了一系列的错误,比如:

 gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/sqlite3.d" -MT"src/sqlite3.d" -o "src/sqlite3.o" "../src/sqlite3.c"
../src/sqlite3.c:30997: error: initializer element is not constant
../src/sqlite3.c:30997: error: (near initialization for `aSyscall[0].pCurrent')
../src/sqlite3.c:30997: error: initializer element is not constant
../src/sqlite3.c:30997: error: (near initialization for `aSyscall[0]')
../src/sqlite3.c:31009: error: initializer element is not constant
../src/sqlite3.c:31009: error: (near initialization for `aSyscall[1]')
../src/sqlite3.c:31017: error: initializer element is not constant
../src/sqlite3.c:31017: error: (near initialization for `aSyscall[2]')

I did find a similar question here, but it doesn't appear to have been resolved there either.

我确实在这里发现了一个类似的问题,但似乎也没有在那里得到解决。

I suspect this is a set-up issue with Eclipse, so if anyone could give me any advice or directions to useful tutorials I'd really appreciate it. And if I'd be better off posting this to a dedicated sqlite forum just let me know.

我怀疑这是Eclipse的设置问题,所以如果有人能给我一些有用教程的建议或指导,我将非常感激。如果我最好把它贴在一个专门的sqlite论坛上,请告诉我。

2 个解决方案

#1


1  

Have you try in this way? (with double pointer):

你试过这种方法吗?(双指针):

int main() {
    sqlite3* db;
    const char* dbname = "test.db";
    sqlite3_open(dbname, &db);
    return 0;
}

I suppose you're working on linux.
Another approach is to execute a script:

我想你正在研究linux。另一种方法是执行脚本:

int main() {
  system("connectDB.sh"); 
  /* connectDB.sh should be chmod +x */
}

Your file connectDB will be:

您的文件connectDB将是:

#!/bin/bash
sqlite3 test.db "select * from test.table" 

#2


1  

SQLite is written in C, and there are a number of differences between C and C++. Not huge numbers, but they're definitely not the same and neither is a superset of the other. Because you are using a single Eclipse project, you've probably ended up trying to compile C code with a C++ compiler, and are therefore coming unstuck on these small differences.

SQLite是用C编写的,在C和c++之间有很多不同之处。不是很大的数字,但它们肯定是不一样的,也不是另一个的超集。因为您使用的是一个Eclipse项目,所以您可能最终尝试使用c++编译器编译C代码,因此无法解决这些小的差异。

You are advised to build sqlite3.c into a separate library (it can be a static library or a dynamic one; your call) as a C project, and then make your C++ project just use that C project as a dependency. Or you can build it once and just have it as an external dependency; that'll work too. (To be fair, it's an external dependency; you shouldn't really embed it wholesale into your code anyway as that will make tracking bugfixes harder. Keeping it separate — at least for build, even if not for distribution — will make your life much easier.)

建议您构建sqlite3。c组成一个单独的库(可以是静态库,也可以是动态库;您的调用)作为一个C项目,然后使您的c++项目仅使用该C项目作为一个依赖项。或者你可以只构建一次它作为外部依赖;这也会工作。(公平地说,这是一种外部依赖;无论如何,您不应该将它大量地嵌入到代码中,因为这会使跟踪错误更加困难。保持它的独立性——至少对于构建来说是这样,即使不是用于发布——也会让您的生活变得更轻松。

#1


1  

Have you try in this way? (with double pointer):

你试过这种方法吗?(双指针):

int main() {
    sqlite3* db;
    const char* dbname = "test.db";
    sqlite3_open(dbname, &db);
    return 0;
}

I suppose you're working on linux.
Another approach is to execute a script:

我想你正在研究linux。另一种方法是执行脚本:

int main() {
  system("connectDB.sh"); 
  /* connectDB.sh should be chmod +x */
}

Your file connectDB will be:

您的文件connectDB将是:

#!/bin/bash
sqlite3 test.db "select * from test.table" 

#2


1  

SQLite is written in C, and there are a number of differences between C and C++. Not huge numbers, but they're definitely not the same and neither is a superset of the other. Because you are using a single Eclipse project, you've probably ended up trying to compile C code with a C++ compiler, and are therefore coming unstuck on these small differences.

SQLite是用C编写的,在C和c++之间有很多不同之处。不是很大的数字,但它们肯定是不一样的,也不是另一个的超集。因为您使用的是一个Eclipse项目,所以您可能最终尝试使用c++编译器编译C代码,因此无法解决这些小的差异。

You are advised to build sqlite3.c into a separate library (it can be a static library or a dynamic one; your call) as a C project, and then make your C++ project just use that C project as a dependency. Or you can build it once and just have it as an external dependency; that'll work too. (To be fair, it's an external dependency; you shouldn't really embed it wholesale into your code anyway as that will make tracking bugfixes harder. Keeping it separate — at least for build, even if not for distribution — will make your life much easier.)

建议您构建sqlite3。c组成一个单独的库(可以是静态库,也可以是动态库;您的调用)作为一个C项目,然后使您的c++项目仅使用该C项目作为一个依赖项。或者你可以只构建一次它作为外部依赖;这也会工作。(公平地说,这是一种外部依赖;无论如何,您不应该将它大量地嵌入到代码中,因为这会使跟踪错误更加困难。保持它的独立性——至少对于构建来说是这样,即使不是用于发布——也会让您的生活变得更轻松。