简体   繁体   中英

Delete and Update statements in Sqlite not working

I am using SqlitePCL on a windows phone 8.1 application, I have the queries select and insert working but for some reason the delete and update are not working

Here are my codes:

Select Statement:

public static itemPassword[] AllGetItemPassword()
        {
            var db = App.conn;
            int numbofPass = 0;
            itemPassword[] itmpass = null;
            try
            {
                using (var statement = db.Prepare("SELECT nameWeb, username, password, link FROM Passwords"))
                {
                    while (statement.Step() == SQLiteResult.ROW)
                    {
                        numbofPass++;
                    }
                }
            }
            catch
            {

            }

Insert Statement:

    public void insertToDB(String usernameWeb, String userusername, String userpassword, String userlink)
    {
        var db = App.conn;

        try
        {
            using (var pass = db.Prepare("INSERT INTO Passwords (nameWeb, username, password, link) VALUES (?,?,?,?)"))
            {
                pass.Bind(1, usernameWeb);
                pass.Bind(2, "Username: " + userusername);
                pass.Bind(3, "Password: " + userpassword);
                pass.Bind(4, "Link: " + userlink);
                pass.Step();
            }
        }
        catch
        {
        }

And now the not working codes:

Delete statement:

 public static void DeleteRecord12(string webname, string username , string password, string link)
        {
            var db = App.conn;
            try
            {
                using (var pass = db.Prepare("DELETE FROM Passwords WHERE webname = ? AND username = ? AND password = ? AND link = ?"))
                {
                    pass.Bind(1, webname);
                    pass.Bind(2, username);
                    pass.Bind(3, password);
                    pass.Bind(4, link);
                    pass.Step();
                }
            }
            catch (Exception ex)
            {
                ex.GetType();
            }
        }

Update statement:

public void UpdateRecord(string webname, string username, string password, string link,string Newwebname, string Newusername, string Newpassword, string Newlink)
        {
            var db = App.conn;
            try
            {
                using (var pass = db.Prepare("UPDATE Passwords SET webname = ?, username = ?, password = ?, link = ? WHERE webname = ? AND username = ? AND password = ? AND link = ?"))
                {
                    pass.Bind(1, Newwebname);
                    pass.Bind(2, Newusername);
                    pass.Bind(3, Newpassword);
                    pass.Bind(4, Newlink);
                    pass.Bind(5, webname);
                    pass.Bind(6, username);
                    pass.Bind(7, password);
                    pass.Bind(8, link);
                    pass.Step();
                }
            }
            catch (Exception ex)
            {
                ex.GetType();
            }
        }

UPDATE: It seems that sqlite Pcl on windows phone doesn't support the command "update" and when Selecting or deleting the WHERE command only supports "WHERE id=?" and not anything else like "WHERE name=belal".

You are putting literal texts in front of the username and other variables in the insert statement. Because of this the record is never found when updating or deleting it. Remove the labels in front of the variables in the binds of the insert statement to get it to work:

public void insertToDB(String usernameWeb, String userusername, String userpassword, String userlink)
{
    var db = App.conn;

    try
    {
        using (var pass = db.Prepare("INSERT INTO Passwords (nameWeb, username, password, link) VALUES (?,?,?,?)"))
        {
            pass.Bind(1, usernameWeb);
            pass.Bind(2, userusername);
            pass.Bind(3, userpassword);
            pass.Bind(4, userlink);
            pass.Step();
        }
    }
    catch
    {
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM