Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

SQLite Insert or Replace

Discussion in 'Scripting' started by zergmouse, Aug 2, 2010.

  1. zergmouse

    zergmouse

    Joined:
    Jun 30, 2010
    Posts:
    216
    Just curious how to impliment INSERT OR REPLACE via sqlite? I am currently trying

    Code (csharp):
    1. cmd.CommandText = "REPLACE INTO tblMASTER(PID,NAME,VALUE) VALUES ('1', 'Name', 'Value') WHERE PID='1';";
    2. cmd.ExecuteNonQuery();
    and I keep getting "SQLite Error" yet my insert function works:

    Code (csharp):
    1. cmd.CommandText = "INSERT INTO tblMASTER(PID,NAME,VALUE) VALUES ('1', 'Name', 'Value');";
    2. cmd.ExecuteNonQuery();
    Any ideas?
     
  2. zergmouse

    zergmouse

    Joined:
    Jun 30, 2010
    Posts:
    216
    So has anyone tried REPLACE or UPDATE in unity? I cant seem to get anything to work.

    The only things that seem to work are INSERT and SELECT.

    What am I doing wrong? (doing this in C#)
     
  3. psikoT

    psikoT

    Joined:
    Feb 2, 2010
    Posts:
    2
    try deleting the quotes in WHERE PID='1'
     
  4. zergmouse

    zergmouse

    Joined:
    Jun 30, 2010
    Posts:
    216
    When I take out the "where" command the INSERT OR REPLACE workes ... so it looks like this

    Code (csharp):
    1. cmd.CommandText = "INSERT OR REPLACE INTO tblVALVES (PID, NAME, VALUE, UNIT, LOCATION) VALUES ('1', 'ValveName', 'true', 'UnitHere', 'LocationHere');";
    But when I add a WHERE command it stops working. I tried:

    Code (csharp):
    1. WHERE PID = 1);";
    2. WHERE PID = '1');";
    3. WHERE PID=1);";
    4. WHERE PID='1');";
    5. WHERE 'PID = 1');";
    6. WHERE (PID = 1));";
    Nothing seems to work.
     
  5. zergmouse

    zergmouse

    Joined:
    Jun 30, 2010
    Posts:
    216
    Apparently the UPDATE works :

    Code (csharp):
    1. "UPDATE tblVALVES SET PID='2', NAME='ValveName', VALUE='true', UNIT='UnitHere', LOCATION='LocationHere' WHERE PID = '1';";
    So Now I have INSERT, SELECT, and UPDATE working. It would be nice if 'INSERT OR REPLACE' worked but I guess I can work with these for now.
     
  6. beltzaser

    beltzaser

    Joined:
    Aug 4, 2010
    Posts:
    16
    I am new to game development, but have been working on Real time systems in banks etc using SQL for a over 15 years, and worked on DB systems like Oracle, SQL Server and MySQL mainly.

    The SQL Statement "Insert or Replace" cannot have a where clause to my knowledge. That is the reason why you got the warning, but it still parses and executes. To make your statement work, you need to look at your primary keys, ie. the unique indices on your table. The statement will use that as reference to either insert a new row, or replace an existing row.

    For example, if you have a table structure like below:-
    A - Unique ID
    B - String

    And you have the following inserted:-
    1, Hello
    2, Pete
    3, Dog

    If you then run your statement as Insert or Replace Into Table values (2, 'Doh!')

    It will replace 'Pete' with the string 'Doh!'

    I hope this is of some help.