2010-10-18 20 views
11

मैंने SQLite का उपयोग करने का निर्णय लिया क्योंकि यह डेटाबेस को एक फ़ाइल में संग्रहीत करने की अनुमति देता है। मुझे लगता है कि मैंने SQLite Database Browser के साथ डेटाबेस करने में कामयाब रहा है।SQLite डेटाबेस से डेटा कैसे पढ़ा जाए?

मैं सी/सी ++ प्रोग्राम में उस डेटा को कैसे पढ़ सकता हूं?

उत्तर

8
+1

धन्यवाद। मैंने स्क्वाइट साइट्स को ब्राउज़ करते समय क्विकस्टार्ट ट्यूटोरियल को नोटिस नहीं किया था। बिल्कुल वही दिखता है जो मुझे चाहिए: सरल निर्देश SQLite के साथ कैसे शुरू करें और अभी तक कुछ भी उन्नत शब्दकोष नहीं है :) – zaplec

+5

स्टैक ओवरफ़्लो में आपका स्वागत है! लिंक के बजाय, उत्तर निकाय में वास्तविक स्रोत को प्राथमिकता दें। – Jonny

+1

Pleas इस उत्तर में कुछ और विवरण जोड़ने पर विचार करें। – NathanOliver

8

'An Introduction to Sqlite C/C++ Interface' के बारे में है, और वहाँ एक पूरी सी ++ उदाहरण here on CodeProject है कैसे।

यह और अधिक पूर्ण नमूना के टुकड़े है,

#include "CppSQLite.h" 
#include <ctime> 
#include <iostream> 
using namespace std; 
const char* gszFile = "C:\\test.db"; 

int main(int argc, char** argv) 
{ 
    try 
    { 
     int i, fld; 
     time_t tmStart, tmEnd; 
     CppSQLiteDB db; 

     cout << "SQLite Version: " << db.SQLiteVersion() << endl; 

     db.open(gszFile); 
     cout << db.execScalar("select count(*) from emp;") 
       << " rows in emp table in "; 
     db.Close(); 
    } 
    catch (CppSQLiteException& e) 
    { 
     cerr << e.errorCode() << ":" << e.errorMessage() << endl; 
    } 
} 
6

एक उदाहरण SQLite पढ़ने का उपयोग कर:

#include <stdio.h> 
#include <sqlite3.h> 
#include <string.h> 


int main(int argc, char** argv) 
{ 
    const char*   username = "satyam"; 
    char     q[999]; 
    sqlite3*    db; 
    sqlite3_stmt*  stmt; 
    int     row = 0; 
    int     bytes; 
    const unsigned char* text; 

    if (2 == argc) { 
     username = argv[1]; 
    } 

    q[sizeof q - 1] = '\0'; 
    snprintf(
     q, 
     sizeof q - 1, 
     "SELECT ipaddr FROM items WHERE username = '%s'", 
     username 
    ); 

    if (sqlite3_open ("test.db", &db) != SQLITE_OK) { 
     fprintf(stderr, "Error opening database.\n"); 
     return 2; 
    } 

    printf("Query: %s\n", q); 

    sqlite3_prepare(db, q, sizeof q, &stmt, NULL); 

    bool done = false; 
    while (!done) { 
     printf("In select while\n"); 
     switch (sqlite3_step (stmt)) { 
     case SQLITE_ROW: 
      bytes = sqlite3_column_bytes(stmt, 0); 
      text = sqlite3_column_text(stmt, 1); 
      printf ("count %d: %s (%d bytes)\n", row, text, bytes); 
      row++; 
      break; 

     case SQLITE_DONE: 
      done = true; 
      break; 

     default: 
      fprintf(stderr, "Failed.\n"); 
      return 1; 
     } 
    } 

    sqlite3_finalize(stmt); 

    return 0; 
} 
+0

यह खराब स्वरूपण, खराब क्रम, अप्रयुक्त चर, निरर्थक अस्थायी, लापता न्यूलाइन, और बहुत कुछ का एक पूर्ण गड़बड़ है। मैंने इसे ठीक करना शुरू कर दिया लेकिन फिर महसूस किया कि मेरी ज़िम्मेदारी नहीं है। मैं नौसिखिया पाठकों को सलाह दूंगा कि इस कोड से कोई प्रेरणा न लें। साथ ही, मुझे लगता है कि यह नौसिखिया पाठक थे जो इसे ऊपर उठा रहे थे, इस मामले में, आपको नहीं करना चाहिए। –

0

एक तरह से अतिरिक्त रैपर के बिना यह करने के लिए

#include <stdio.h> 
#include <string> 
using std::string; 
#include <sstream> 
using std::stringstream; 

#include "sqlite3.h" 

bool find_employee(int _id) 
{ 
    bool found = false; 
    sqlite3* db; 
    sqlite3_stmt* stmt; 
    stringstream ss; 

    // create sql statement string 
    // if _id is not 0, search for id, otherwise print all IDs 
    // this can also be achieved with the default sqlite3_bind* utilities 
    if(_id) { ss << "select * from employees where id = " << _id << ";"; } 
    else { ss << "select * from employees;"; } 
    string sql(ss.str()); 

    //the resulting sql statement 
    printf("sql: %s\n", sql.c_str()); 

    //get link to database object 
    if(sqlite3_open("data/test.db", &db) != SQLITE_OK) { 
     printf("ERROR: can't open database: %s\n", sqlite3_errmsg(db)); 
     sqlite3_close(db); 
     return found; 
    } 

    // compile sql statement to binary 
    if(sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL) != SQLITE_OK) { 
     printf("ERROR: while compiling sql: %s\n", sqlite3_errmsg(db)); 
     sqlite3_close(db); 
     sqlite3_finalize(stmt); 
     return found; 
    } 

    // execute sql statement, and while there are rows returned, print ID 
    int ret_code = 0; 
    while((ret_code = sqlite3_step(stmt)) == SQLITE_ROW) { 
     printf("TEST: ID = %d\n", sqlite3_column_int(stmt, 0)); 
     found = true; 
    } 
    if(ret_code != SQLITE_DONE) { 
     //this error handling could be done better, but it works 
     printf("ERROR: while performing sql: %s\n", sqlite3_errmsg(db)); 
     printf("ret_code = %d\n", ret_code); 
    } 

    printf("entry %s\n", found ? "found" : "not found"); 

    //release resources 
    sqlite3_finalize(stmt); 
    sqlite3_close(db); 

    return found; 
} 
संबंधित मुद्दे