Results 1 to 7 of 7

Thread: Sqlite unrecognized token while inserting of hex values

  1. #1
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Sqlite unrecognized token while inserting of hex values

    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.

    Qt Code:
    1. if(temperatureHexdata.count()>=200)
    2. {
    3. temperatureHexdata.append(QTime::currentTime().toString("hh:mm:ss"));
    4.  
    5. m_query.exec("begin transaction Trans");
    6. QString newsQuery = "insert into allhexdata";
    7. newsQuery += " values (";
    8. for(int q = 0; q < temperatureHexdata.count() && q<200;q++)
    9. {
    10. newsQuery += QString("%1").arg(temperatureHexdata[q]) + ", ";
    11. }
    12. newsQuery += "'" + QString("%1").arg(temperatureHexdata[200]) + "', ";
    13. newsQuery.remove(newsQuery.length() - 2, 2);
    14. newsQuery += ")";
    15. //qDebug() << temperatureHexdata;
    16. qDebug() << "Query=" << newsQuery;
    17. if(!m_query.exec(newsQuery))
    18. {
    19. qDebug() << m_query.lastError().text();
    20. }
    21. m_query.exec("commit transaction Trans");
    22. temperatureHexdata.clear();
    23. }
    To copy to clipboard, switch view to plain text mode 
    pls tell me if i am doing something wrong. ..

    thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sqlite unrecognized token while inserting of hex values

    What type of data can you put in that column?
    If the db column only accepts integers, that might be your problem.

  3. #3
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: Sqlite unrecognized token while inserting of hex values

    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..

    Qt Code:
    1. for( int i=0 ; i<200 ; ++i )
    2. {
    3. HexData += QString("Thermo%1 varchar(250) ").arg(i) + (i==199 ? "" : ",");
    4. }
    5. QString dbt = QString("create table if not exists allhexdata( %1, Time_InterVal varchar(16));").arg(HexData);
    6. query.exec(dbt);
    To copy to clipboard, switch view to plain text mode 

    thank you

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Sqlite unrecognized token while inserting of hex values

    The problem is, that in here
    Qt Code:
    1. for(int q = 0; q < temperatureHexdata.count() && q<200;q++)
    2. {
    3. newsQuery += QString("%1").arg(temperatureHexdata[q]) + ", ";
    4. }
    To copy to clipboard, switch view to plain text mode 
    you missed to place the single quotation marks ('). Thus all input are threaten as integers. Use
    Qt Code:
    1. QString("'%1'").arg(/*...*/);
    To copy to clipboard, switch view to plain text mode 

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

  5. The following user says thank you to Lykurg for this useful post:

    rex (30th January 2011)

  6. #5
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: Sqlite unrecognized token while inserting of hex values

    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

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Sqlite unrecognized token while inserting of hex values

    Quote Originally Posted by rex View Post
    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
    sql Code:
    1. INSERT INTO `foo` (`id`) VALUES ('123');
    To copy to clipboard, switch view to plain text mode 

    So to avoid problems always use quotation marks.

  8. The following user says thank you to Lykurg for this useful post:

    rex (30th January 2011)

  9. #7
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: Sqlite unrecognized token while inserting of hex values

    ok sir thank you again. .. I understood it now..

    regards

Similar Threads

  1. Replies: 4
    Last Post: 19th December 2010, 06:15
  2. Qt to Sqlite data insertion doubt.
    By rex in forum Qt Programming
    Replies: 15
    Last Post: 16th December 2010, 12:31
  3. How to QTableWidget default values on insertion?
    By RawArkanis in forum Qt Programming
    Replies: 1
    Last Post: 20th April 2010, 07:02
  4. Replies: 0
    Last Post: 4th December 2008, 05:48
  5. Import PostgreSQL Values to SQLITE
    By raphaelf in forum Newbie
    Replies: 2
    Last Post: 24th May 2007, 17:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.