PDA

View Full Version : Sqlite unrecognized token while inserting of hex values



rex
30th January 2011, 08:16
Hello every one i have a small doubt i am inserting some hex value into sqlite db but i am getting a unrecognized token error while inserting value like 000c, 03f4 , 038b but when i am inserting values like 0017 , 0023 , 0384 etc there is no error the data is saved properly to the db... as per my analysis only when there is some alphabet in the hex value i am getting a unrecognized token error.
when there is only numbers in the value it is inserted into the data base.
this is the part of code where i am saving the data into db.


if(temperatureHexdata.count()>=200)
{
temperatureHexdata.append(QTime::currentTime().toS tring("hh:mm:ss"));

m_query.exec("begin transaction Trans");
QString newsQuery = "insert into allhexdata";
newsQuery += " values (";
for(int q = 0; q < temperatureHexdata.count() && q<200;q++)
{
newsQuery += QString("%1").arg(temperatureHexdata[q]) + ", ";
}
newsQuery += "'" + QString("%1").arg(temperatureHexdata[200]) + "', ";
newsQuery.remove(newsQuery.length() - 2, 2);
newsQuery += ")";
//qDebug() << temperatureHexdata;
qDebug() << "Query=" << newsQuery;
if(!m_query.exec(newsQuery))
{
qDebug() << m_query.lastError().text();
}
m_query.exec("commit transaction Trans");
temperatureHexdata.clear();
}
pls tell me if i am doing something wrong. ..

thank you

tbscope
30th January 2011, 09:11
What type of data can you put in that column?
If the db column only accepts integers, that might be your problem.

rex
30th January 2011, 09:15
Hello sir this is how i am creating the columns for the table. It is varchar so it should't be a problem no sir..


for( int i=0 ; i<200 ; ++i )
{
HexData += QString("Thermo%1 varchar(250) ").arg(i) + (i==199 ? "" : ",");
}
QString dbt = QString("create table if not exists allhexdata( %1, Time_InterVal varchar(16));").arg(HexData);
query.exec(dbt);

thank you

Lykurg
30th January 2011, 09:34
The problem is, that in here
for(int q = 0; q < temperatureHexdata.count() && q<200;q++)
{
newsQuery += QString("%1").arg(temperatureHexdata[q]) + ", ";
} you missed to place the single quotation marks ('). Thus all input are threaten as integers. Use
QString("'%1'").arg(/*...*/);

Or consider using prepare() and bindValue() to avoid such mistakes.

rex
30th January 2011, 09:46
Hello Lykurg sir thank you so much.. that solved the problem..
i wanted to know why is it treated as a integer if we don't use the single quotation mark.

regards

Lykurg
30th January 2011, 10:04
i wanted to know why is it treated as a integer if we don't use the single quotation mark. Well that's the common SQL syntax: No quotation -> number, quotation -> string. And that is undependent from the type of the field. You also can add an integer to an INT field with INSERT INTO `foo` (`id`) VALUES ('123');

So to avoid problems always use quotation marks.

rex
30th January 2011, 10:11
ok sir thank you again. .. I understood it now.. :)

regards