Export to database

Asked

Viewed 121 times

3

I load 3 Strings from a CMS.

String Data da Noticia
String Titulo da Noticia
String Texto da Noticia

Within a loop, create the query to insert with these 3 strings.

I’m just having some problems.

1º) The string Texto da Noticia, has characters like "-" and " ' to solve, I used replace. But I would have some way of inserting, without having to take these characters from the text of the news?

2º)Characters accentuated in the text of the news as "ç", "is" , "á" are being saved in the bank as a " ? "

I tried to use that:

pstmt = con.prepareStatement("SET NAMES 'utf8'");
pstmt.executeUpdate();
pstmt = con.prepareStatement("SET CHARACTER SET utf8");
pstmt.executeUpdate();

But it does not solve. In the bank I also set to utf8 and unicod.

Does anyone know how to solve these 2 problems?

  • 1

    What kind of database are you using? What programming language are you using?

  • I load the contents of a CMS into a JSP page. And export to Mysql database.

  • Try exporting the strings in UTF-8 or Unicode. I had a similar problem with SQL Server and this "conversion" worked.

  • How do you export in these formats?

  • In the question Bruno is using Mysql, this is valid also in it?

1 answer

1

Mysql is fully capable of saving special characters. I advise you instead of using string concatenation to form a script.

Ex :

string script = "INSERT INTO TABELA (ID, NOME, IDADE) VALUES ("+ id +",'"+ nome +"'," + idade +")" 

Use variables to pass information.

Ex :

string script = "INSERT INTO TABELA (ID, NOME, IDADE) VALUES (@ID, @NOME, @IDADE)" 

var command = new SqlCommand(sql);

command.Parameters.AddWithValue("@ID", id);
command.Parameters.AddWithValue("@NOME", nome);
command.Parameters.AddWithValue("@IDADE", idade);

Browser other questions tagged

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