0

In last 2-3 days I am having problem with inserting data to postgres databse from Qt program I made.

I have made connection with the database, but when I try to insert data, the program sends me this message:

ERROR:  syntax error at or near "("
LINE 1: EXECUTE  ('thisIsSomeName', 4, '0000')
                 ^
QPSQL: Unable to create query

Here is the code from Qt that insert the value.

QSqlQuery qsql;
qsql.prepare("INSERT INTO baza(Name, ID, Birth Date)"
                     "VALUES (?, ?, ?)");

        qsql.bindValue(0, "thisIsSomeName");
        qsql.bindValue(1, 4);
        qsql.bindValue(2, "0000");

        if (qsql.exec())
        {
            label->setText("all is good");
        }

Can you please tell how to make this work. Thanks. Script of baza

CREATE TABLE baza
(
  "Name" name NOT NULL DEFAULT 50,
  "ID" integer NOT NULL,
  "Birth Date" text DEFAULT 0,
  CONSTRAINT baza_pkey PRIMARY KEY ("ID")
)
WITH (
  OIDS=FALSE
);
ALTER TABLE baza OWNER TO postgres;
6
  • hello, what is the datatype of Name and can you give the full table script of baza? it will be helpful Commented Apr 5, 2012 at 11:46
  • @PresleyDias I have put the script of baza. Thank you for your help Commented Apr 5, 2012 at 12:34
  • hey drop your table and try this CREATE TABLE baza ( Name name NOT NULL DEFAULT 50, ID integer NOT NULL, Birth Date text DEFAULT 0, CONSTRAINT baza_pkey PRIMARY KEY ("ID") ) WITH ( OIDS=FALSE ); ALTER TABLE baza OWNER TO postgres; Commented Apr 5, 2012 at 12:36
  • that is without the " for name ,ID and Birth date Commented Apr 5, 2012 at 12:37
  • You cannot have a column with a space in it unless you use double quotes. Commented Apr 5, 2012 at 13:21

1 Answer 1

2

try this

    CREATE TABLE baza
      (
         Name  name NOT NULL DEFAULT 50,
         ID  integer NOT NULL,
         Birth_Date  text DEFAULT 0,
       CONSTRAINT baza_pkey PRIMARY KEY ("ID")
    )
     WITH (
        OIDS=FALSE
     );
       ALTER TABLE baza OWNER TO postgres;

then try this

  QSqlQuery qsql;
  qsql.prepare("INSERT INTO baza(Name, ID, Birth_Date)"
                 "VALUES (?, ?, ?)");

    qsql.bindValue(0, "thisIsSomeName");
    qsql.bindValue(1, 4);
    qsql.bindValue(2, "0000");

    if (qsql.exec())
    {
        label->setText("all is good");
    }

here are some examples sql sqlstatements

Sign up to request clarification or add additional context in comments.

2 Comments

@depecheSoul: You might want to re-visit the manual regarding identifiers: postgresql.org/docs/current/static/…
@a_horse_with_no_name thank you for your link I had a quick look on it And I have seen some useful stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.