I'm working on a project, where I use metadata of mp3 songs in a string class. These are the data (i.e. artist, title, etc.) I want to copy into a mySQL database (from a list I made from the data) for which I have the next query:
SELECT * FROM database.tablename WHERE artist='"+song[i].artist+ "' AND title='" + song[i].title + "' AND genre='"+song[i].genre+"';
Code works perfectly (knowing that 12 lines copied successfully) until the for loop reaches a song what contains an apostrophe ('), it exits with syntax error at the given symbol. For example: song[i].title is I'm not her. I tried everything i could imagine:
- I tried replace ' to string.empty
- Tried to replace to ""
- Even tried to replace to "''" (2 apostrophes) so the query can recognize an apostrophe but nothing seems to help (always got the same syntax error, like when replace even wasn't there)
Here is a line, so you can see how I tried:
song[i].artist = song[i].artist.Replace("'","");
So my question is: Is there any solution to get rid of the apostrophe, or any way to make the query work?
EDIT: Here is my original code (only the for loop) for better understanding.
for (int i = 0; i < Array.Length; i++)
{
Command = @"SELECT * FROM database.tablename WHERE artist='"+song[i].artist+ "' AND title='" + song[i].title + "' AND genre='"+song[i].genre+"';";
MySqlCommand myCommand = new MySqlCommand(Command);
using (MySqlConnection mConnection = new MySqlConnection(ConnectionString.ToString()))
{
adapter = new MySqlDataAdapter(Command,mConnection);
mConnection.Open();
adapter.Fill(dataset);
int adat = dataset.Tables[0].Rows.Count;
if (adat <= 0) //if current data does not exist
{
song[i].artist = song[i].artist.Replace("'","\'");
song[i].title = song[i].title.Replace("'", "\'");
song[i].genre = song[i].genre.Replace("'", "\'");
myCommand = new MySqlCommand("INSERT INTO database.tablename (artist,title,length,genre) VALUES ('"+song[i].artist+"','"+song[i].title+"','"+song[i].length+"','"+song[i].genre+"');",mConnection);
myCommand.ExecuteNonQuery();
dataset.Clear();
}
mConnection.Close();
}
}
And here is what I'm "using" (sorry, don't know how to make it look proper):
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using TagLib; using TagLib.Id3v2; using System.Configuration; using System.Data.SqlClient; using MySql; using MySql.Data; using MySql.Data.MySqlClient; using Programname.Properties;