sqlite and mql5 how to delete oneline

 

Hi guys  i have a database sqlite

structure:

CREATE TABLE "Trade" (
        "id"    INTEGER,
        "ticket"        TEXT,
        "symbol"        TEXT,
        "price" TEXT,
        PRIMARY KEY("id" AUTOINCREMENT)
);

with this data :

4       22117593        ETHUSD  3907.30;3914.14

i want remove the data without  drop table only data  i  try to use this function for do that  but return me always error 

void DeleteTradeFromDB(ulong ticket)
{
    int db_handle = DatabaseOpen(dbFile, DATABASE_OPEN_READWRITE);
    if(db_handle == INVALID_HANDLE)
    {
        Print("Errore apertura DB per cancellazione: ", GetLastError());
        return;
    }
Print ("IL TICKET E' ",ticket);
    //string query = StringFormat("DELETE FROM Trade WHERE ticket=%d", ticket);
     string query = "DELETE FROM Trade WHERE ticket=" + IntegerToString((long)ticket) + ";";
 
   Print("LAQUESRY DEL CAZZO E ",query);
    int request = DatabasePrepare(db_handle, query);
    if(request != INVALID_HANDLE)
    {
        if(DatabaseExecute(db_handle,request))
            PrintFormat("Trade eliminata dal DB | Ticket: %I64u", ticket);
        else
            PrintFormat(" Errore cancellazione DB | Ticket: %I64u | Errore: %d", ticket, GetLastError());

        DatabaseFinalize(request);
    }
    else
        Print("Errore preparazione query DELETE: ", GetLastError());

    DatabaseClose(db_handle);
}

the error:

MONITOR_TRADE (ETHUSD,M15) IL TICKET E' 22117593 2025.10.23 13:56:14.498 
MONITOR_TRADE (ETHUSD,M15) LAQUESRY DEL CAZZO E DELETE FROM Trade WHERE ticket=22117593; 2025.10.23 13:56:14.498 
MONITOR_TRADE (ETHUSD,M15) database error, near "131073": syntax error 2025.10.23 13:56:14.498 
MONITOR_TRADE (ETHUSD,M15)  Errore cancellazione DB | Ticket: 22117593 | Errore: 5601 2025.10.23 13:56:14.501 
MONITOR_TRADE (ETHUSD,M15) Caricate 1 trade dal DB. 2025.10.23 13:56:16.888 MONITOR_TRADE (ETHUSD,M15)  Socket connected: NO

anyone have some idea or example? i not find  a clear example for do that  thanks 

 
Stefano Cerbioni:

In your table, ticket is TEXT, but DELETE uses ticket=22117593 (without quotes), so SQLite treats it as an identifier or misplaced number.

string query = "DELETE FROM Trade WHERE ticket='" + IntegerToString((long)ticket) + "';";
 
Miguel Angel Vico Alba #:
string query = "DELETE FROM Trade WHERE ticket='" + IntegerToString((long)ticket) + "';";

i just tested my friends  and not go return me this 

2025.10.23 14:52:16.609 MONITOR_TRADE (ETHUSD,M15)      IL TICKET E' 22117593
2025.10.23 14:52:16.609 MONITOR_TRADE (ETHUSD,M15)      LAQUESRY DEL CAZZO E DELETE FROM Trade WHERE ticket='22117593';
2025.10.23 14:52:16.610 MONITOR_TRADE (ETHUSD,M15)      database error, near "131073": syntax error
2025.10.23 14:52:16.610 MONITOR_TRADE (ETHUSD,M15)       Errore cancellazione DB | Ticket: 22117593 | Errore: 5601

if  i execute  this DELETE FROM Trade WHERE ticket='22117593';   inside  a db browser   erase  correctly  the  record but if i run it by mql5 not work and return me error


i just tryed also in this mode 
void DeleteTradeFromDB(ulong ticket)
{
   int db = DatabaseOpen(dbFile, DATABASE_OPEN_READWRITE);
   if(db == INVALID_HANDLE)
   {
      PrintFormat(" Errore apertura DB (%d)", GetLastError());
      return;
   }

   string sql = "DELETE FROM Trade WHERE ticket=?";   // placeholder
   int stmt = DatabasePrepare(db, sql);

   if(stmt == INVALID_HANDLE)
   {
      PrintFormat(" Errore preparazione query (%d)", GetLastError());
      DatabaseClose(db);
      return;
   }

   // 🔹 bind del parametro (come testo, in base a come è salvato nel DB)
   DatabaseBind(stmt, 0, (long)ticket);

   if(DatabaseExecute(db,stmt))
      PrintFormat(" Trade %I64u rimosso correttamente dal DB", ticket);
   else
      PrintFormat(" Errore esecuzione DELETE per ticket=%I64u | codice=%d", ticket, GetLastError());

   DatabaseFinalize(stmt);
   DatabaseClose(db);
}

 
Stefano Cerbioni #:

The error is likely caused by an unexpected character or the trailing semicolon, which DatabasePrepare might not accept.

Try removing the ";" and make sure you're using plain single quotes. If it still fails, the query string might contain hidden characters or SQLite could be misinterpreting the value; in that case, using DatabaseBind() with a parameter would be the safest way to rule that out.

 
Stefano Cerbioni #:

i just tested my friends  and not go return me this 

if  i execute  this DELETE FROM Trade WHERE ticket='22117593';   inside  a db browser   erase  correctly  the  record but if i run it by mql5 not work and return me error


Your code is buggy and overly complicated. Please read the documentation more carefully.

   long ticket = 22117593;
   string sql  = StringFormat("DELETE FROM Data4 WHERE ticket=%lli;",ticket);  

   if(DatabaseExecute(db,sql))
      PrintFormat(" Trade %I64u rimosso correttamente dal DB", ticket);
   else
      PrintFormat(" Errore esecuzione DELETE per ticket=%I64u | codice=%d", ticket, GetLastError());
 
Alain Verleyen #:
if(DatabaseExecute(db,sql))       PrintFormat(" Trade %I64u rimosso correttamente dal DB", ticket);    else       PrintFormat(" Errore esecuzione DELETE per ticket=%I64u | codice=%d", ticket, GetLastError());
TOP   thanks so much