我正在使用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++的新手。
到存儲庫的鏈接是一個404(https://github.com/jacksiro/vsongboo-cpp)。此外,還可以使用真正的IDE,比如Visual Studio 2017. Falcon C++ IDE最近在3年前更新過。這看起來像一個死了的項目。 – IInspectable
抱歉,我已更新鏈接github.com/jacksiro/vsongbook-cpp。我不能使用VS,因爲我不能支付運行在該機器上的機器,並且我還想做一些與Windows XP兼容的東西,而不需要安裝任何東西 –