Qt SQL - ERROR (42601): syntax error at or near

Asked

Viewed 7,520 times

2

I am currently developing a program to assist in the management of a barracks in my city, I am using Qt to create the graphical interface and other libraries needed as the manipulation of databases, for the server I am using the postgres, when trying to create a table I get the following error:

Falha ao criar a tabela va_atiradores:  QSqlError("42601", "QPSQL: Unable to create query", "ERROR:  syntax error at or near \"(\"\nLINE 4: cpf integer(11) NOT NULL UNIQUE,\n                   ^\n(42601)")

Also while terminating the program the visual studio Runtime accuses me a heap corruption error:

inserir a descrição da imagem aqui

Here is the excerpt of the code responsible for creating the table:

bool Atirador::insert(QStringList &data)
{

    QSqlDatabase db = QSqlDatabase::database();

    const QString sqlTable = "CREATE TABLE IF NOT EXISTS sch_tg.va_atiradores(\n"
                       "id SERIAL PRIMARY KEY,\n"
                       "nome varchar(1000) NOT NULL UNIQUE,\n"
                       "cpf integer(11) NOT NULL UNIQUE,\n"
                       "rg integer(20) NOT NULL UNIQUE,\n"
                       "ra integer(30) UNIQUE,\n"
                       "nPai varchar(1000) NOT NULL UNIQUE,\n"
                       "nMae varchar(1000) NOT NULL UNIQUE,\n"
                       "dataNasc date NOT NULL,\n"
                       "estdCvl integer NOT NULL,\n"
                       "cidade varchar(250) NOT NULL,\n"
                       "cep integer(10) NOT NULL,\n"
                       "estado varchar(250) NOT NULL,\n"
                       "uf varchar(2) NOT NULL,\n"
                       "rua varchar(500) NOT NULL,\n"
                       "nCasa integer,\n"
                       "bairro varchar(500) NOT NULL,\n"
                       "cel integer(30) UNIQUE,\n"
                       "tel integer(30) UNIQUE,\n"
                       "tipoAtr integer NOT NULL,\n"
                       "status integer NOT NULL,\n"
                       "cadPor varchar(250) NOT NULL,\n"
                       "ultServ date,\n"
                       "pontos integer,\n"
                       "servicos integer)\0";

    QSqlQuery createTableQuery(db);

    if(!createTableQuery.exec(sqlTable)){

        qCritical() << "Falha ao criar a tabela va_atiradores: " << createTableQuery.lastError();

        createTableQuery.finish();

        return true;
    }

    createTableQuery.finish();

    db.close();

    return true;
}

At first I thought it was some error of some escaped character, but when testing the sql code in the query tool pgAdmin 4 I get the same mistake:

ERROR:  syntax error at or near "("
LINE 4:                        cpf integer(11) NOT NULL UNIQUE,
                                          ^
SQL state: 42601
Character: 189
  • Wouldn’t it be better to use one StringBuilder to build the query and use the AppendLine() to give line feeds?

1 answer

4


Error message shows syntax error in the query on the line cpf integer(11) NOT NULL UNIQUE; you are wanting a whole but are setting a size for it, which is not necessary.

const QString sqlTable = "CREATE TABLE IF NOT EXISTS sch_tg.va_atiradores(\n"
                   "id SERIAL PRIMARY KEY,\n"
                   "nome varchar(1000) NOT NULL UNIQUE,\n"
                   "cpf integer NOT NULL UNIQUE,\n"
                   "rg integer NOT NULL UNIQUE,\n"
                   "ra integer UNIQUE,\n"
                   "nPai varchar(1000) NOT NULL UNIQUE,\n"
                   "nMae varchar(1000) NOT NULL UNIQUE,\n"
                   "dataNasc date NOT NULL,\n"
                   "estdCvl integer NOT NULL,\n"
                   "cidade varchar(250) NOT NULL,\n"
                   "cep integer NOT NULL,\n"
                   "estado varchar(250) NOT NULL,\n"
                   "uf varchar(2) NOT NULL,\n"
                   "rua varchar(500) NOT NULL,\n"
                   "nCasa integer,\n"
                   "bairro varchar(500) NOT NULL,\n"
                   "cel integer UNIQUE,\n"
                   "tel integer UNIQUE,\n"
                   "tipoAtr integer NOT NULL,\n"
                   "status integer NOT NULL,\n"
                   "cadPor varchar(250) NOT NULL,\n"
                   "ultServ date,\n"
                   "pontos integer,\n"
                   "servicos integer)\0";

I removed these settings, now should not have more problems

Browser other questions tagged

You are not signed in. Login or sign up in order to post.