我有一个SQLite3 C API的问题。 我正在尝试从places用于存储许多内容的places.sqlite数据库中读取一些信息。 我感兴趣的是检索上次访问过的网站和相关的窗口标题。
我写了这段代码:
query = "SELECT `url`, `title` FROM `moz_places` WHERE `id`=(SELECT `place_id` FROM `moz_historyvisits` ORDER BY `id` DESC LIMIT 1)"; stmt = NULL; if (sqlite3_prepare_v2(db, query.c_str(), strlen(query.c_str()) + 1, &stmt, NULL) != SQLITE_OK) cerr << sqlite3_errmsg(db) << endl; else { while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) cout << sqlite3_column_text(stmt, 0) << endl << sqlite3_column_text(stmt, 1) << endl; if (ret != SQLITE_DONE) cerr << sqlite3_errmsg(db) << endl; }当没有特殊字符时它可以正常工作,但如果窗口标题包含“é”,“è”或“•”,我会得到那些精彩的字符“Ô,“Ô或“— 。 我做了一些研究,发现有些人实际上有非UTF8编码的数据库。 所以我用SQLite Manager和“PRAGMA编码”请求检查了places.sqlite数据库编码,两者都说它是UTF-8编码的。 然后我创建了自己的数据库,以UTF-8编码,在表格中输入了一些特殊字符,但它也没有用。 所以我认为也许QtCreator(我正在使用)无法显示特殊字符,我试图用GetForegroundWindow()获取前景窗口标题并在QtCreator应用程序输出中打印它。 它成功地展示了一些窗口标题的特殊字符。
我在这里想念的是什么?
谢谢。
I have a problem with the SQLite3 C API. I'm trying to read some information from the places.sqlite database that Firefox uses to store many things. What I'm interested in is to retrieve the last visited website and the window title associated.
I wrote this piece of code:
query = "SELECT `url`, `title` FROM `moz_places` WHERE `id`=(SELECT `place_id` FROM `moz_historyvisits` ORDER BY `id` DESC LIMIT 1)"; stmt = NULL; if (sqlite3_prepare_v2(db, query.c_str(), strlen(query.c_str()) + 1, &stmt, NULL) != SQLITE_OK) cerr << sqlite3_errmsg(db) << endl; else { while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) cout << sqlite3_column_text(stmt, 0) << endl << sqlite3_column_text(stmt, 1) << endl; if (ret != SQLITE_DONE) cerr << sqlite3_errmsg(db) << endl; }When there are no special characters it works fine, but if the window title contains "é", "è" or "•" for example, I get those wonderful characters "é", "è" or "•". I did some research and found that some people actually got non-UTF8 encoded databases. So I checked the places.sqlite database encoding with both SQLite Manager and the "PRAGMA encoding" request, both say that it is UTF-8 encoded. Then I created my own database, encoded in UTF-8, entered some special characters in a table and it didn't work either. So I thought that maybe QtCreator (which I'm using) can't show special characters, I tried to get the foreground window title with GetForegroundWindow() and print it in QtCreator application output. It successfully showed the special characters of some window titles.
What am I missing here ?
Thank you.
最满意答案
Weel,好像我设法通过使用我写的这个函数解决了这个问题:
wchar_t *char_to_wchar(const char *str) { wchar_t *wbuf; int wsz; if (!str) return (NULL); wbuf = NULL; wsz = MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, 0); if (wsz) wbuf = (wchar_t*)malloc(wsz * sizeof(*wbuf)); else cerr << "MultiByteToWideChar fail getting required size: " << GetLastError() << endl; if (wbuf) { if (!MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, wsz)) cerr << "MultiByteToWideChar fail: " << GetLastError() << endl; } return (wbuf); }尽管如此,我发现使用像GetWindowText()这样的函数来设置从一个简单的char *正确打印“é”和其他字符很奇怪,但在这里我必须使用wchar_t * ......好吧,它至少可以工作,让我们希望这是最好的解决方案:)
Weel, seems like I managed to fix that problem by using this function I wrote:
wchar_t *char_to_wchar(const char *str) { wchar_t *wbuf; int wsz; if (!str) return (NULL); wbuf = NULL; wsz = MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, 0); if (wsz) wbuf = (wchar_t*)malloc(wsz * sizeof(*wbuf)); else cerr << "MultiByteToWideChar fail getting required size: " << GetLastError() << endl; if (wbuf) { if (!MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, wsz)) cerr << "MultiByteToWideChar fail: " << GetLastError() << endl; } return (wbuf); }Still, I found it weird that with a function like GetWindowText() I manage to correctly print "é" and other caracters from a simple char *, but here I must use a wchar_t *... Well, it works at least, let's hope it was the best solution :)
更多推荐
发布评论