3

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);

1 Answer 1

2

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"

Sign up to request clarification or add additional context in comments.

1 Comment

Thank for your help, and after my some work on finding correct file path, my problem is soloved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.