SQLite3未返回的特殊字符(Special characters not returned by SQLite3)

编程入门 行业动态 更新时间:2024-10-17 05:31:26
SQLite3未返回的特殊字符(Special characters not returned by SQLite3)

我有一个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 :)

更多推荐

本文发布于:2023-07-23 02:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226473.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:特殊字符   Special   returned   characters

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!