PDA

View Full Version : inserting content in database



codeman
3rd June 2009, 15:42
Hello I have a simple question .

what is the best way to insert content to a database. What about cutting up the string if the content is to long for field in the database. Is there a flag to set for automatic cutting cause when it is to long te insert fails.

Any idea??

This is my approach:



if(qstr_textLine.count(";",Qt::CaseInsensitive)== 2){
qstr_textLine.replace(QString(","), QString("."));
qstr_textLine.replace(QString(";"), QString(","));
qstr_textLine.replace(QString("'"), QString("''"));
QSqlQuery insertquery;
qstr_textLine.replace(QString(","), QString("','"));
qstr_textLine.prepend("'");
qstr_textLine.append("'");
qstr_textLine.replace(QString("'NULL'"), QString("NULL"));

int sqlbool=insertquery.exec("INSERT INTO testtbl (field1,field2,field3) "
"VALUES ("+qstr_textLine+")");
}


I use mssql.

lni
3rd June 2009, 16:03
Don't say "I have a simple question", if it is simple, why do you ask? :)

How about using QString::left(...)?

But yet, it is simple :D

Lykurg
3rd June 2009, 17:47
if(qstr_textLine.count(";",Qt::CaseInsensitive)== 2){
qstr_textLine.replace(QString(","), QString("."));
qstr_textLine.replace(QString(";"), QString(","));
qstr_textLine.replace(QString("'"), QString("''"));
QSqlQuery insertquery;
qstr_textLine.replace(QString(","), QString("','"));
qstr_textLine.prepend("'");
qstr_textLine.append("'");
qstr_textLine.replace(QString("'NULL'"), QString("NULL"));

int sqlbool=insertquery.exec("INSERT INTO testtbl (field1,field2,field3) "
"VALUES ("+qstr_textLine+")");
}


Yeah, definitely look at QSqlQuery::prepare()!