简体   繁体   English

如何从数据库文件制作QByteArray

[英]How to make a QByteArray from a Database file

Using QByteArray QIODevice::readAll() from QT5, I was able to make a bytes array from a txt file or an image, used decode after and recreated the file correctly. 使用QT5中的QByteArray QIODevice :: readAll() ,我能够从txt文件或图像制作字节数组,之后使用解码并正确地重新创建文件。 But, when I tried with a .db file (SQLITE) it didn't work. 但是,当我尝试使用.db文件(SQLITE)时,它不起作用。

I noticed that when you open a .db with a text editor, you will see "SQLite format 3" followed by encoded characters. 我注意到,当您使用文本编辑器打开.db时,将看到“ SQLite格式3”,后跟编码字符。 After making a QByteArray from a .db file, followed by decode() to recreate the file, when I opened it with a text editor, the file only contains the text "SQLite format 3". 从.db文件中创建一个QByteArray之后,接着是delete()来重新创建该文件,当我使用文本编辑器打开该文件时,该文件仅包含文本“ SQLite format 3”。

Does QByteArray only work with txt file or Image file? QByteArray是否仅适用于txt文件或Image文件?

If it does, how can I make a Array of bytes from a .db (SQLITE) file. 如果是这样,如何从.db(SQLITE)文件中制作字节数组。

Thanks 谢谢


Update1 (The code belows works): Update1(以下代码有效):

QFile file("C:/database.db");
if(!file.open(QIODevice::ReadOnly))
    qDebug()<<"You are stupid!";

QByteArray byteArray = file.readAll();

QFile file2("C:/database2.db");
file2.open(QIODevice::WriteOnly);
file2.write(byteArray);

file2.close();
file.close();

Update2: 更新2:

About the decode I mentioned in my initial question, I was using the following: 关于我在最初的问题中提到的解码,我使用了以下内容:

QString QFile::decodeName(const QByteArray & localFileName)

which make no sense when you read carefully the documentation and was just wrong. 当您仔细阅读文档时,这是没有意义的,只是错误。 :) :)

You should not open that file with QIODevice::Text flag. 您不应该使用QIODevice :: Text标志打开该文件。

Check this http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum 检查此http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM