I download C/C++ interface for SQLite from here. And I get 4 source files shell.c
, sqlite3.c
, sqlite3.h
and sqlite3ext.h
.
Now I add all these 4 files into my cocos2d-x project and use the following code for 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 run it. But it builds failed, the error is showing below:
duplicate symbol _main in:
/Users/tangyue/Library/Developer/Xcode/DerivedData/CrossKaiser-bkepfijxelboxkchsxvcmpozrwlt/Build/Intermediates/CrossKaiser.build/Debug-iphonesimulator/CrossKaiser.build/Objects-normal/i386/main.o
/Users/tangyue/Library/Developer/Xcode/DerivedData/CrossKaiser-bkepfijxelboxkchsxvcmpozrwlt/Build/Intermediates/CrossKaiser.build/Debug-iphonesimulator/CrossKaiser.build/Objects-normal/i386/shell.o
ld: 1 duplicate symbol for architecture i386 clang: error: linker
command failed with exit code 1 (use -v to see invocation)
I think there must be a main
in these file, which occurs this error. And I find main
method in the file shell.c
. Because I don't include this file in my test code, I remove it from the project.
Then I run it again. This time, it build success, but the value of result
is NOT SQLITE_OK
, it is 14(SQLITE_CANTOPEN
), which means 'Unable to open the database file'.
Now what should I do to run the program correctly? What is the shell.c
file used for, and is it wrong for me to remove it from project.
[update]
I solove it use 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 of
result = sqlite3_open("save.db", &pDB);
Remove shell.c from your project. It is cli tool to work with sqlite databases. It define main() because it is executable.
Regarding second part of your question sqlite3_open: "unable to open database file"