mercredi 1 avril 2015

Cannot insert multiple lines in Sqlite using C#

I know this is a very simple question but I cannot find any answer that can help me. I'm using C# in order to connect to a SQLITE database and insert some data. Here is the code :



private void OpenDb()
{
this.m_handle = new SQLiteConnection("Data Source=" + m_filename + ".sqlite3;Version=3;");
this.m_handle.Open();
}

private void CloseDb()
{
this.m_handle.Close();
}

private void CreateDb()
{
this.OpenDb();
string query = "CREATE TABLE StreamingUrl ([url] varchar(255) NOT NULL, [DateRequest] TEXT NOT NULL);";
SQLiteCommand command = new SQLiteCommand(query, this.m_handle);
command.ExecuteNonQuery();
this.CloseDb();
}

public void AddStreamingUrl(string url)
{
this.OpenDb();
string query = "INSERT INTO StreamingUrl (url, DateRequest) VALUES ('" + url + "', datetime('now', 'localtime'));";
SQLiteCommand command = new SQLiteCommand(query, this.m_handle);
command.ExecuteNonQuery();
this.CloseDb();
}


I can create the database as well as the table. When I try to insert one data, it works good. However, when I try to add a second row it does not work, sqlite does an update on it (DateRequest is updated, so I think the row is updated). My database file is not added to the project as reference or things like that.


When I use SQLiteAdministrator with this insert query, it works. So I think that my code is wrong but I cannot figure it out. I don't have any exception or errors.


EDIT : It may be important but I have two projects in my VS solution : one dll and one exe. The code is in the dll and I run the exe project> I don't know if that changes something.


EDIT2 : Added the code that calls `CreateDb. It is in my class constructor constructor:



this.m_filename = filename;
if (System.IO.File.Exists(this.m_filename) == false)
{
SQLiteConnection.CreateFile(this.m_filename + ".sqlite3");
this.CreateDb();
}


AddStreamingUrl is called by a event so I cannot show you the entire code. Here is the part I think it's interesting :



this.NetworkManager.StreamingUrl += Passerelle_StreamingUrl;


And after the event is raised :



void Passerelle_StreamingUrl(object sender, EventArgs e)
{
log.AddStreamingUrl((e as StreamingUrl).Url);
Console.WriteLine("Done");
}

Aucun commentaire:

Enregistrer un commentaire