Select, onde está o erro:

5 respostas
R

Olá,

estou fazendo a seguinte select:
cQuerySql = "select * from ESTADOS where ABREVIATURA = " + S;

já tentei:
cQuerySql = "select * from ESTADOS where ABREVIATURA = " + ‘S’;

Mas sempre dá erro ao rodar. Informa que a coluna não existe.
GDS Exception. 335544569. Dynamic SQL Error
SQL error code = -206

Um exemplo: Seleciono o estado PR ao executar a select ela fica assim:

cQuerySql = "select * from ESTADOS where ABREVIATURA = PR;

eu acho que  teria que ser assim:

cQuerySql = "select * from ESTADOS where ABREVIATURA = PR;

5 Respostas

H

abreviatura provavelmente é uma string

entao teria q ser

SELECT * FROM estados WHERE abreviatura LIKE ‘" + S + "’";

aspas simples " + S + " aspas simples;

B

Se a SQL for estatica:

cQuerySql = “select * from ESTADOS where ABREVIATURA like ‘PR’”;

se for dinâmica:

cQuerySql = “select * from ESTADOS where ABREVIATURA like ?”;

daí use o método setString do PreparedStatement para setar a variável.

T

PreparedStatement stmt = connection.prepareStatement(“select * fom TABELA where CAMPO = ?”);

stmt.setString(1, variavelcampo);

stmt.executeQuery();

stmt.close();
R

já resolvido:

cQuerySql = "select * from ESTADOS where ABREVIATURA = '" + S +"' ";
B

rsa_tche:
já resolvido:

cQuerySql = "select * from ESTADOS where ABREVIATURA = '" + S +"' ";


Vai levar um belo ataque de SQL Injection :slight_smile:

Criado 3 de julho de 2008
Ultima resposta 3 de jul. de 2008
Respostas 5
Participantes 4