2012-06-01 17 views
11

मैं 'C++ sqlite3 lib .. मैं हो रही है त्रुटि sqlite3_prepare_v2 का उपयोग कर
एक डेटाबेस बनाने के लिए कोशिश कर रहा हूँ इस दायरे
में घोषित नहीं किया गया था।C++: डालें के लिए SQLite का उपयोग कर डेटाबेस बनाएं और अपडेट करते ही logcat में दिखाया गया

लॉग फ़ाइल

..\src\Test.cpp: In function 'int main(int, const char**)': 
..\src\Test.cpp:21:85: error: 'sqlite3_prepare_v2' was not declared in this scope 
..\src\Test.cpp:30:13: error: variable 'sqlite3 in' has initializer but incomplete type 
..\src\Test.cpp:30:30: error: invalid use of incomplete type 'sqlite3 {aka struct sqlite3}' 
..\src\/sqlite3.h:73:16: error: forward declaration of 'sqlite3 {aka struct sqlite3}' 

यहाँ मेरी कोड

#include <iostream> 
using namespace std; 
#include "sqlite3.h" 

int main (int argc, const char * argv[]) { 

sqlite3 *db; 
sqlite3_open("test.db", & db); 

    string createQuery = "CREATE TABLE IF NOT EXISTS items (busid INTEGER PRIMARY KEY, ipaddr TEXT, time TEXT NOT NULL DEFAULT (NOW()));"; 
    sqlite3_stmt *createStmt; 
    cout << "Creating Table Statement" << endl; 
    sqlite3_prepare_v2(db, createQuery.c_str(), createQuery.size(), &createStmt, NULL); 
    cout << "Stepping Table Statement" << endl; 
    if (sqlite3_step(createStmt) != SQLITE_DONE) cout << "Didn't Create Table!" << endl; 

    string insertQuery = "INSERT INTO items (time, ipaddr) VALUES ('test', '192.168.1.1');"; // WORKS! 


    sqlite3_stmt *insertStmt; 
    cout << "Creating Insert Statement" << endl; 
    sqlite3_prepare(db, insertQuery.c_str(), insertQuery.size(), &insertStmt, NULL); 
    cout << "Stepping Insert Statement" << endl; 
    if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl; 

cout << "Success!" << endl; 

return 0; 
} 

कृपया मेरी मदद से बाहर है। धन्यवाद .....

उत्तर

16
#include <sqlite3.h> 

sqlite3_prepare_v2 और struct sqlite3 शामिल करना चाहिए। सुनिश्चित करें कि आप सही sqlite3.h फ़ाइल शामिल कर रहे हैं।

इसके अलावा sqlite3_prepare_v2 में तीसरा तर्क हो सकता है (और आपके मामले में होना चाहिए) -1 इसलिए एसक्यूएल पहले शून्य टर्मिनेटर को पढ़ा जाता है।

कार्य नंगे धातु नमूना SQLite 3.7.11 का उपयोग कर:

#include <sqlite3.h> 
int test() 
{ 
    sqlite3* pDb = NULL; 
    sqlite3_stmt* query = NULL; 
    int ret = 0; 
    do // avoid nested if's 
    { 
     // initialize engine 
     if (SQLITE_OK != (ret = sqlite3_initialize())) 
     { 
      printf("Failed to initialize library: %d\n", ret); 
      break; 
     } 
     // open connection to a DB 
     if (SQLITE_OK != (ret = sqlite3_open_v2("test.db", &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL))) 
     { 
      printf("Failed to open conn: %d\n", ret); 
      break; 
     } 
     // prepare the statement 
     if (SQLITE_OK != (ret = sqlite3_prepare_v2(pDb, "SELECT 2012", -1, &query, NULL))) 
     { 
      printf("Failed to prepare insert: %d, %s\n", ret, sqlite3_errmsg(pDb)); 
      break; 
     } 
     // step to 1st row of data 
     if (SQLITE_ROW != (ret = sqlite3_step(query))) // see documentation, this can return more values as success 
     { 
      printf("Failed to step: %d, %s\n", ret, sqlite3_errmsg(pDb)); 
      break; 
     } 
     // ... and print the value of column 0 (expect 2012 here) 
     printf("Value from sqlite: %s", sqlite3_column_text(query, 0));  

    } while (false); 
    // cleanup 
    if (NULL != query) sqlite3_finalize(query); 
    if (NULL != pDb) sqlite3_close(pDb); 
    sqlite3_shutdown(); 
    return ret; 
} 

आशा इस मदद करता है

+1

धन्यवाद क्रिस्टियन .. – Satyam

+0

हमारे लिए बहुत उपयोगी है thnx दोस्त ... –

+0

यहाँ। डेटाबेस खुला है ... लेकिन मुझे डेटाबेस –

1

this लिंक के माध्यम से जाएं। मुझे यकीन नहीं है। यह आपकी मदद कर सकता है।

मुझे लगता है कि उनके sqlite3.h lib में कोई sqlite3_prepare_v2 है, इसलिए इस कोशिश .. sqlite3_prepare_v2 sqlite3_prepare द्वारा प्रतिस्थापित किया जा सकता है, लेकिन और अधिक देखभाल की जरूरत है, क्योंकि यह आगामी कॉल के शब्दों से थोड़ा बदल जाता है।

+1

मेरी खुशी .... :) –

4

दोस्तों, डेटाबेस बनाने ग में sqlite3 का उपयोग कर/C++, यहाँ मैं follwing चरणों का उपयोग कर रहा हूँ .. ।

1) Firstly you include MinGw file . 
2) Add header file sqlite3.h, sqlite3.c in your src folder. 
3) Add libr folder , in libr here include these file 

    mysqlite.h, shell.c, sqlite3.c, sqlite3.h, sqlite3ext.h 

के बाद फिर अपने कोडिंग शुरू ...

 #include <iostream> 
using namespace std; 
#include "sqlite3.h" 

int main (int argc, const char * argv[]) { 

    sqlite3 *db; 
    sqlite3_open("test1.db", & db); 

    string createQuery = "CREATE TABLE IF NOT EXISTS items (userid INTEGER PRIMARY KEY, ipaddr 
     TEXT,username TEXT,useradd TEXT,userphone INTEGER,age INTEGER, " 
                "time TEXT NOT NULL DEFAULT 
                      (NOW()));"; 
    sqlite3_stmt *createStmt; 
    cout << "Creating Table Statement" << endl; 
    sqlite3_prepare(db, createQuery.c_str(), createQuery.size(), &createStmt, NULL); 
    cout << "Stepping Table Statement" << endl; 
    if (sqlite3_step(createStmt) != SQLITE_DONE) cout << "Didn't Create Table!" << endl; 

    string insertQuery = "INSERT INTO items (time, ipaddr,username,useradd,userphone,age) 
     VALUES ('7:30', '192.187.27.55','vivekanand','kolkatta','04456823948',74);"; // WORKS! 
    sqlite3_stmt *insertStmt; 
    cout << "Creating Insert Statement" << endl; 
    sqlite3_prepare(db, insertQuery.c_str(), insertQuery.size(), &insertStmt, NULL); 
    cout << "Stepping Insert Statement" << endl; 
    if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl; 



     return 0; 
    } 
+0

कमाल का जवाब। बहुत बहुत दोस्त धन्यवाद! :-) –

+0

सम्मिलित क्वेरी के मानों को स्थैतिक पाठ के रूप में रखने के बजाय, यह अच्छा हो सकता है कि अगर आप विवरण को अद्यतन करते हैं, कथन तैयार होने के बाद, मानों को गतिशील रूप से कैसे बदलें .. – phoad

+0

फ़ोड, आप इसका उपयोग करके ऐसा करते हैं "?" या? 1? 2 आदि क्वेरी के अंदर और फिर बाध्यकारी पैरामीटर को एक कथन में देखें। देखें http://www.sqlite.org/c3ref/bind_blob.html –