Results 1 to 4 of 4

Thread: QSqlQuery: Invalid column name (with colon) under SQL Server

  1. #1
    Join Date
    Nov 2012
    Posts
    14
    Qt products
    Platforms
    Windows

    Default QSqlQuery: Invalid column name (with colon) under SQL Server

    Hi everyone,

    I'm trying to execute an insert query via QSqlQuery in my SQL Server Database. The query looks like this:

    INSERT INTO dbo.mytab ([col1:data1], [col2:data2], [col3:data3], [col4:data4]) VALUES(?, ?, ?, ?)

    The query is correctly prepared using addBindValue function, but when it's executed I get the following error:

    "INSERT INTO dbo.mytab ([col1?], [col2?], [col3?], [col4?]) VALUES(?, ?, ?, ?)"
    QSqlError(8180, "QODBC3: Unable to execute statement", "[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'col1?'.

    I guess this is somehow related to the role of placeholders in the query, but I cannot figure out how. Is there a way to include colon in the column names without generating this error?
    Thanks in advance!

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QSqlQuery: Invalid column name (with colon) under SQL Server

    I don't know SQL Server, but here goes... Is the col1:data syntax something supported by SQL Server for normal column names or is that the syntax used for named parameters?

    The ":variable" syntax is typically used as a placeholder that can be replaced at run-time with QSqlQuery::bindValue, so I suspect that's where your error is coming from. Are the column names known that you're trying to insert data into or are they truly variable and you are intending to bind values for the column names?

    Here's what I'd try:

    Qt Code:
    1. INSERT INTO dbo.mytab (:col1, :col2, :col3, :col4) VALUES (:data1, :data2, :data3, :data4)
    To copy to clipboard, switch view to plain text mode 

    Then prepare that statement and bind values to all of the named parameters. Never had the need to have variable column names myself, but if that's what you are trying to accomplish, might be worth a try.

    Good luck.

    Edit: I tried to use a named parameter for an SQLITE UPDATE statement and it doesn't work. While I don't see the restriction noted in the QSqlQuery documentation, all of the examples show named and positional parameters as values, not column names, etc.
    Last edited by jefftee; 1st July 2015 at 02:52.

  3. #3
    Join Date
    Nov 2012
    Posts
    14
    Qt products
    Platforms
    Windows

    Default Re: QSqlQuery: Invalid column name (with colon) under SQL Server

    Thank you for the answer jefftee. I had a try, but unfortunately it turned out that fields name cannot be bounded.

    Any other ideas?

    Thanks again.

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QSqlQuery: Invalid column name (with colon) under SQL Server

    You will have to resort to building your SQL statement at run time by concatenating the field names. Once built, you can still prepare the SQL statement and use named or positional values for the actual data. For example:

    Qt Code:
    1. QString col1name = "col1";
    2. QString col2name = "col2";
    3. QString col1data = "foo";
    4. QString col2data = "bar";
    5. QString sql = "insert into foo(" + col1name + "," + col2name + ") values(:col1data,:col2data)";
    6. QSqlQuery q(db);
    7. q.prepare(sql);
    8. q.bindValue(":col1data",col1data);
    9. q.bindValue(":col2data",col2data);
    10. q.exec();
    To copy to clipboard, switch view to plain text mode 
    If the column names are being provided by the user, you should validate the column names using a regex or other test to ensure they aren't succeptible to SQL injections. Using bindValue for the data protects against that, but blindly accepting user input and concatenating in SQL queries is bad form, as it can and will be abused.

    Hope that helps.
    Last edited by jefftee; 2nd July 2015 at 22:23.

Similar Threads

  1. Replies: 15
    Last Post: 11th March 2014, 01:54
  2. Replies: 2
    Last Post: 9th April 2013, 09:15
  3. Replies: 1
    Last Post: 18th July 2011, 12:12
  4. Replies: 3
    Last Post: 5th May 2011, 14:03
  5. [TCP Server] Server only sending when terminating
    By papperlapapp in forum Qt Programming
    Replies: 0
    Last Post: 6th December 2010, 19:41

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.