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

2

I'm currently developing a program to help manage a barracks in my city, I'm using Qt to create the graphical interface and other necessary libraries like database manipulation, for the server I'm using 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)")

In addition to closing the program the runtime of visual studio accuses me of a heap corruption error:

Hereisthesnippetofcoderesponsibleforcreatingthetable:

boolAtirador::insert(QStringList&data){QSqlDatabasedb=QSqlDatabase::database();constQStringsqlTable="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)
ERROR:  syntax error at or near "("
LINE 4:                        cpf integer(11) NOT NULL UNIQUE,
                                          ^
SQL state: 42601
Character: 189
"; 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 non-escaped character, but when I test the sql code in the query tool of pgAdmin 4 I get the same 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)")
    
asked by anonymous 31.08.2018 / 13:44

1 answer

4

The error message displays syntax error in the query assembly on line cpf integer(11) NOT NULL UNIQUE ; you are wanting an integer but you 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)
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)%pre%";
";

I've removed these settings, now you should not have any more problems

    
31.08.2018 / 13:49