How to use sqlite3 in cocos2d-x project in Xcode
I am downloading C / C ++ - Interface for SQLite from here . And I get the source file 4 shell.c
, sqlite3.c
, sqlite3.h
and sqlite3ext.h
.
Now I add all these 4 files to my cocos2d-x project and use the following code to test:
#include "sqlite/sqlite3.h"
sqlite3 *pDB = NULL;
char * errMsg = NULL;
std::string sqlstr;
int result;
result = sqlite3_open("save.db", &pDB);
if( result != SQLITE_OK )
CCLog( "failed,status_code:%d ,error_msg:%s\n" , result, errMsg );
sqlite3_close(pDB);
Then I launched it. But it doesn't work, the error is displayed below:
duplicate symbol _main in:
/Users/tangyue/Library/Developer/Xcode/DerivedData/CrossKaiser-bkepfijxelboxkchsxvcmpozrwlt/Build/Intermediates/CrossKaiser.build/Debug-iphonesimulator/CrossKaiser.build/Objects6-normalmainal
/Users/tangyue/Library/Developer/Xcode/DerivedData/CrossKaiser-bkepfijxelboxkchsxvcmpozrwlt/Build/Intermediates/CrossKaiser.build/Debug-iphonesimulator/CrossKaiser.build/Objects6-normsal
ld: 1 duplicate symbol for i386 architecture clang: error: linker
with exit code 1 (use -v to invoke the call)
I think there must be one in that file main
that is causing this error. And I find a method main
in the file shell.c
. Since I am not including this file in my test code, I am removing it from the project.
Then I run it again. This time it creates success, but the value is result
NOT SQLITE_OK
, it is 14 ( SQLITE_CANTOPEN
), which means "Unable to open database file".
Now what do I need to do in order to run the program correctly? What is used for the file shell.c
and is it wrong for me to remove it from the project.
[ Update ]
I am using the following code:
string dbPath = CCFileUtils::sharedFileUtils()->getWriteablePath();
dbPath.append("save.db");
CCLog("%s", dbPath.c_str());
result = sqlite3_open_v2(dbPath.c_str(), &pDB, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
instead
result = sqlite3_open("save.db", &pDB);
source to share
Extract shell.c from your project. It is a cli tool for working with sqlite databases. It defines main () because it is executable.
Regarding the second part of your sqlite3_open question : "cannot open database file"
source to share