2017-07-28 109 views
1

我正在使用cpp和sqlite http://github.com/jacksiro/vsongbook-cpp使用falcon C++ IDE的本地winapi应用程序。这是我的数据库:如何在for循环中声明和使用一个struct数组?

void CreateDatabase() 
{ 
    sqlite3 * db = NULL; 
    int db_qry; 
    const char * sqlCreateTables = 
     "CREATE TABLE my_friends(friend_name VARCHAR(20), " 
     "friend_job VARCHAR(20), friend_job INTEGER(11));"; 

    db_qry = sqlite3_open("friends.db", &db); 
    db_qry = sqlite3_exec(db, sqlCreateTables, NULL, NULL, NULL); 
    sqlite3_close(db); 
} 

而且我这个插入数据库:

void InsertToDatabase() 
{ 
    int db_qry; 
    char *error;  
    sqlite3 *db; 

    const char *sqlInsert = 
     "INSERT INTO my_friends(friend_name, friend_job, friend_age) " 
     "VALUES('Ngunjiri James', 'Teacher', 25);" 
     "INSERT INTO my_friends(friend_name, friend_job, friend_age) " 
     "VALUES('Wafula Shem', 'Capernter', 30);" 
     "INSERT INTO my_friends(friend_name, friend_job, friend_age) " 
     "VALUES('Jane Akinyi', 'Nurse', 23);"; 

    db_qry = sqlite3_open("friends.db", &db); 
    db_qry = sqlite3_exec(db, sqlInsert, NULL, NULL, &error); 
    sqlite3_close(db); 
} 

现在,我在我的窗口中的简单列表框,我从这样我的sqlite的表值填充它。

inline UINT AddStringList(const HWND hList,const ustring& s) 
{ 
    return static_cast<UINT>(SendMessage(hList,LB_ADDSTRING,0, 
         reinterpret_cast<LPARAM>(s.c_str()))); 
} 

//I call this method in my WM_CREATE in WinMain and it works very well 
    void AddFriendListBox(const HWND hList) 
    { 
     sqlite3 * db = NULL; 
     sqlite3_stmt * stmt; 
     int i, db_qry; 
     const char * tail; 
     const char * sqlSelect = "SELECT * FROM my_friends"; 

    db_qry = sqlite3_open("friends.db", &db); 
    if(sqlite3_prepare(db, sqlSelect, -1, &stmt, &tail) == SQLITE_OK) 
    { 
     while(sqlite3_step(stmt) != SQLITE_DONE) 
     { 
      for(i = 0; i < sqlite3_column_count(stmt); i++) { 
       const unsigned char * p = reinterpret_cast<const unsigned char *> 
         (sqlite3_column_text(stmt, 0)); 
       const char * finaltext = (const char *)p; 
       AddStringList(hList,_T(finaltext)); 
       } 
      } 
     sqlite3_finalize(stmt); 
    } 

    sqlite3_close(db); 
} 

我碰到一些代码http://zetcode.com/gui/winapi/advancedcontrols/来创建一个列表框,并使用struct填充它:

typedef struct { 

    wchar_t name[30]; 
    wchar_t job[20]; 
    int age; 

} Friends; 

Friends friends[] = { 

    {L"Lucy", L"waitress", 18}, 
    {L"Thomas", L"programmer", 25}, 
    {L"George", L"police officer", 26}, 
    {L"Michael", L"producer", 38}, 
    {L"Jane", L"steward", 28}, 
}; 

然后用它如下:

case WM_CREATE: 

      hwndList = CreateWindowW(WC_LISTBOXW , NULL, WS_CHILD 
       | WS_VISIBLE | LBS_NOTIFY, 10, 10, 150, 120, hwnd, 
       (HMENU) IDC_LIST, NULL, NULL); 


      for (int i = 0; i < ARRAYSIZE(friends); i++) { 
       SendMessageW(hwndList, LB_ADDSTRING, 0, (LPARAM) friends[i].name); 
      } 

      break; 

因为我无法理解上面使用的ARRAYSIZE。我用如下面的int 5取代它:

现在
case WM_CREATE: 

     hwndList = CreateWindowW(WC_LISTBOXW , NULL, WS_CHILD 
      | WS_VISIBLE | LBS_NOTIFY, 10, 10, 150, 120, hwnd, 
      (HMENU) IDC_LIST, NULL, NULL); 

     for (int i = 0; i < 5; i++) { 
      SendMessageW(hwndList, LB_ADDSTRING, 0, (LPARAM) friends[i].name); 
     } 

     break; 

好,所述和来完成。

如何创建一个struct类似于上面的朋友之一,可与使用for循环从我的sqlite3的表中的值来填充它?

我还是c/C++的新手。

+0

到存储库的链接是一个404(https://github.com/jacksiro/vsongboo-cpp)。此外,还可以使用真正的IDE,比如Visual Studio 2017. Falcon C++ IDE最近在3年前更新过。这看起来像一个死了的项目。 – IInspectable

+0

抱歉,我已更新链接github.com/jacksiro/vsongbook-cpp。我不能使用VS,因为我不能支付运行在该机器上的机器,并且我还想做一些与Windows XP兼容的东西,而不需要安装任何东西 –

回答

1

由于我不能uderstand使用上述我与一个int 5如下取代它的 “ARRAYSIZE”

它最有可能是宏,它可以定义为:

#define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0])) 

这适用于在使用宏的相同函数中定义的数组。当数组传递给函数时,它将不起作用。在获取数组作为参数的函数中,sizeof(array)将评估为指针的大小,而不是数组的大小。

E.g.

void foo(int array[]) 
{ 
    int s = ARRAYSIZE(array); // Won't work here. 
} 

void foo() 
{ 
    int array[] = {1, 3, 20}; 
    int s = ARRAYSIZE(array); // Will work here. 
} 

如果您有选择,您应该使用std::vector。这将使编码从长远来看变得更容易。

在C++ 11,你可以定义为friends

std::vector<Friends> friends = { 
    {L"Lucy", L"waitress", 18}, 
    {L"Thomas", L"programmer", 25}, 
    {L"George", L"police officer", 26}, 
    {L"Michael", L"producer", 38}, 
    {L"Jane", L"steward", 28}, 
}; 
+0

谢谢先生。我刚刚发布的内容没有问题。请帮助我解决真正的问题:如何创建类似于朋友的结构的结构,并使用for循环使用sqlite3表中的值填充它? –

+0

@AppSmata,最好在另一篇帖子中提出这个问题。它与你的文章的标题完全不同。 –

+0

真的吗?那是痛苦和昂贵的。我宁愿在这篇文章 –