QT存储日志用数据库还是txt文本

QT存储日志用数据库还是txt文本,第1张

QT存储日志用数据库还是txt文本是需要具体问题具体分析的,因为如果小量的写数据库没事。如果是大量的,肯定写文件好。汇总后写程序导入数据库。还有一种方法是写redis等内存数据库,并累积数量后触发合并写入数据库 *** 作。

并且如果这个日志是需要定期分析的,写在数据库里更方便处理;反之只是留档,就存文件里 但2种方式都要注意写 *** 作的频率。

绝对不能产生一行写一行,中间加一个内存队列来过渡,比如memcache,有新日志就加入队列,然后做个定时器去批量写入文件并清空队列,同时也规避文件冲突了。

QT存储中大端模式和小端模式是:

对于long long a 和 struct{ char a;short b;int c;}二者同样占据了8个字节的空间,在存储上,后者则是先存储一个char,空一个字节,然后按照大端/小端模式存储short,最后按照大端/小端模式存储int。

在我们日常使用的x86架构的计算机中(其他类别的可能会采用大端模式或可配置模式,可以通过查阅资料或者用下文的代码进行测试),都是使用的小端模式,而网络字节序是大端模式的。

这就使得在网络通信时进行字节序的转换变得极为重要。比方说,通信双方规定了了通信头为一个4字节的魔数(Magic Number),而一方按着大端序的模式发送。

一方按着小端序的模式解读,那么两方的通信就会失败。如果没有这个魔数,而在内部的数据中出现这样的问题则会更加的麻烦。

create table test

(

NID VARCHAR2(1) not null,

IMG BLOB,

zhengqueFlag VARCHAR2(1),

cuowuFlag VARCHAR2(1)

)

说明:

1、IMG字段只需要保存,将信息经过Base64编码,存到数据库BLOB字段中。显示的时候要经过Base64解码。

2、对于像“正确”、“错误”用标志位在区别。你可以用1表示正确、0表示错误。当然你也可以用汉字的保存这样的信息。自己来设计。

然后你到网上查一下怎么样对BLOB字段进行 *** 作。如果不行,我在增加回复。

1将以二进制存入数据库

//保存到数据库

protected void Button1_Click(object sender, EventArgs e)

{

//路径

string strPath = "~/photo/03JPG";

string strPhotoPath = ServerMapPath(strPath);

//读取

FileStream fs = new SystemIOFileStream(strPhotoPath, FileModeOpen, FileAccessRead);

BinaryReader br = new BinaryReader(fs);

byte[] photo = brReadBytes((int)fsLength);

brClose();

fsClose();

//存入

SqlConnection myConn = new SqlConnection("Data Source=127001;Initial Catalog=TestDB;User ID=sa;Password=sa");

string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";

strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";

SqlCommand myComm = new SqlCommand(strComm, myConn);

myCommParametersAdd("@photoBinary", SqlDbTypeBinary,photoLength);

myCommParameters["@photoBinary"]Value = photo;

myConnOpen();

myCommExecuteNonQuery();

myConnClose();

}

2读取二进制在页面显示

//读取

SqlConnection myConn = new SqlConnection("Data Source=127001;Initial Catalog=TestDB;User ID=sa;Password=sa");

string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";

SqlCommand myComm = new SqlCommand(strComm, myConn);

myConnOpen();

SqlDataReader dr = myCommExecuteReader();

while (drRead())

{

byte[] photo = (byte[])dr["personPhoto"];

thisResponseBinaryWrite(photo);

}

drClose();

myConnClose();

SqlConnection myConn = new SqlConnection("Data Source=127001;Initial Catalog=TestDB;User ID=sa;Password=sa");

SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);

DataSet myds = new DataSet();

myConnOpen();

mydaFill(myds);

myConnClose();

byte[] photo = (byte[])mydsTables[0]Rows[0]["personPhoto"];

thisResponseBinaryWrite(photo);

3设置Image控件显示从数据库中读出的二进制

---------------------------------------------

SqlConnection myConn = new SqlConnection("Data Source=19216801;Initial Catalog=TestDB;User ID=sa;Password=sa");

SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);

DataSet myds = new DataSet();

myConnOpen();

mydaFill(myds);

myConnClose();

byte[] photo = (byte[])mydsTables[0]Rows[0]["personPhoto"];

//路径

string strPath = "~/photo/wangwuJPG";

string strPhotoPath = ServerMapPath(strPath);

//保存文件

BinaryWriter bw = new BinaryWriter(FileOpen(strPhotoPath,FileModeOpenOrCreate));

bwWrite(photo);

bwClose();

3显示

thisImage1ImageUrl = strPath;

4GridView中ImageField以URL方式显示

--------------------------

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

<Columns>

<asp:BoundField DataField="personName" HeaderText="姓名" />

<asp:ImageField DataImageUrlField="personPhotoPath"

HeaderText="">

</asp:ImageField>

</Columns>

</asp:GridView>

5GridView显示读出的二进制

//样板列

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">

<Columns>

<asp:BoundField DataField="personName" HeaderText="姓名" />

<asp:ImageField DataImageUrlField="personPhotoPath"

HeaderText="">

</asp:ImageField>

<asp:TemplateField HeaderText="">

<ItemTemplate>

<asp:Image ID="Image1" runat="server" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (eRowRowIndex < 0)

return;

// SystemComponentModelContainer

string strPersonName = (string)DataBinderEval(eRowDataItem, "personName");

Image tmp_Image = (Image)eRowCells[2]FindControl("Image1");

if (!SystemConvertIsDBNull(DataBinderEval(eRowDataItem, "personPhoto")))

{

//

byte[] photo = (byte[])DataBinderEval(eRowDataItem, "personPhoto");

//路径

string strPath = "~/photo/" + strPersonNameTrim() + "JPG";

string strPhotoPath = ServerMapPath(strPath);

//保存文件

BinaryWriter bw = new BinaryWriter(FileOpen(strPhotoPath, FileModeOpenOrCreate));

bwWrite(photo);

bwClose();

//显示

tmp_ImageImageUrl = strPath;

}

}

回复:

Qt::BackgroundRole改成Qt::ForegroundRole,就是文字颜色(0,5)单元格就是indexcolumn()==5&&indexrow()==0

subclass你的Model,重载data、setData函数,以data()为例:

QVariant MyTestModel::data(const QModelIndex &index, int role) const

{

switch(role)

{

case Qt::DisplayRole:

return QVariant(QString(tr("%1"))arg((indexcolumn() + 1) 1000 + indexrow() + 1));

case Qt::BackgroundRole:

switch(indexcolumn() % 3)

{

case 0:

return QVariant(QColor(Qt::red));

case 1:

return QVariant(QColor(Qt::green));

case 2:

return QVariant(QColor(Qt::blue));

default://only to disable warning

return QVariant(QColor(Qt::white));

}

break;

default:

return QVariant();

}

}

1、使用blob将保存为二进制格式,(可以用浏览器来转换)随后用base64编码来保存,再将base64编码保存进数据库的clob类型字段上。

2、然后要用一个数据名称SystemDataOracleClient。

3、创建一个储存文件,然后把相关代码写入比如string execSql="insert into clob_table(clob_id,) values(1,:clob_pic);"编写代码的时候注意,后缀的符号(;")也要写入,不然无法进行下一步指令。

4、之前上面用的是base64编码,当显示的时候要将输出到浏览器流中,不然在base64里面是看不到。

5、在输出的时候要把转换成二进制(buffur即二进制编码)。

6、随后上传即可,然二进制流也可以存成文件(File)存到FTP服务器,当需要的时候可以根据路径进行下载的。

在QT的widget中用tableview显示sqlite数据库表中的内容。

假设有数据库文件testdb,有表table(id integer, name nvarchar(20),age integer),且有数条数据。

首先用QTcreator创建一个基于Widget类的窗口,再拖一个tableview到widget中,保存,然后按照如下方法进行:

1在widgeth中增添头文件:QtSql/qsqlh、QtSql/QsqlDatabase、QtSql/QsqlQuery、QtSql/QsqlQueryModel

2在pro工程文件中添加:QT+=sql

3在widgetcpp中widget的构造函数中添加如下代码:

QsqDatabase db = QsqlDatabase::addDatabase("QSQLITE");

dbsetDatabaseName("testdb");

if(!dbopen())

{

//错误处理

}

static QSqlQueryModel model = new QSqlQueryModel(ui->tableview);

model->setQuery(QString("select from table"));

model->setHeaderData(0,Qt::Horizontal,QObject::tr("编号"));

model->setHeaderData(1,Qt::Horizontal,QObject::tr("姓名"));

model->setHeaderData(2,Qt::Horizontal,QObject::tr("年龄"));

ui->tableview->setModel(model);

db->close();

这样之后,table表里的内容就会显示到tableview中了。

以上就是关于QT存储日志用数据库还是txt文本全部的内容,包括:QT存储日志用数据库还是txt文本、如何向ORACLE数据库表中存入带图片的文件、Sqlserver数据库存储的图片格式(二进制数据)怎么显示到页面等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/sjk/9762925.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存