通过传递用户名从SQLite数据库中删除行?

时间:2015-11-22 14:07:38

标签: android sqlite

我正在通过传递用户名作为参数来删除我的SQLite数据库中的特定行 现在,这就是发生的事情。

如果我传递了行ID,一切正常,但是当我传入KEY_Name = username时,没有任何反应。

所以,我现在很困惑。
谁能告诉我为什么会出现这个错误呢? 我的更新按钮也会出现同样的问题。

Option Explicit
Dim MyScriptPath 
MyScriptPath = WScript.ScriptFullName
Call Shortcut(MyScriptPath,"Shutdown the computer")
Call AskQuestion()
'**********************************************************************************************
Sub Shortcut(PathApplication,Name)
    Dim objShell,DesktopPath,objShortCut,MyTab
    Set objShell = CreateObject("WScript.Shell")
    MyTab = Split(PathApplication,"\")
    If Name = "" Then
        Name = MyTab(UBound(MyTab))
    End if
    DesktopPath = objShell.SpecialFolders("Desktop")
    Set objShortCut = objShell.CreateShortcut(DesktopPath & "\" & Name & ".lnk")
    objShortCut.TargetPath = Dblquote(PathApplication)
    ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,-28"
    objShortCut.Save
End Sub
'**********************************************************************************************
Sub AskQuestion()
    Dim Question,Msg,Title
    Title = "Shutdown the computer"
    Msg = "Are you sure to shutdown the computer now ?"& Vbcr &_
    "If yes, then click [YES] button "& Vbcr &_
    "If not, then click [NO] button"
    Question = MsgBox (Msg,VbYesNo+VbQuestion,Title)
    If Question = VbYes then
        Call Run_Shutdown(30)
    else
        WScript.Quit()
    End if
End Sub
'**********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Sub Run_Shutdown(N)
    Dim ws,Command,Execution
    Set ws = CreateObject("wscript.Shell")
    Command = "Cmd /c Shutdown -s -t "& N &" -c "& DblQuote("Save your work because your PC will shut down in "& N &" seconds")
    Execution = ws.run(Command,0,True)
End sub
'**********************************************************************************************

令我惊讶的是,如果我将行id作为参数传递如下,则以下代码可以正常工作: -

public void deleteUser(String userName)
        {
            try
            {
                SQLiteDatabase db = helper.getWritableDatabase();
                db.delete(DATABASE_TABLE, KEY_NAME + "=" + userName,null);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

        }

case R.id.button4:

              String sr = t7.getText().toString();

              Electronicscheckout exht = new Electronicscheckout(this);
              try {
                  exht.open();
                  exht.delete(sr);
                  exht.close();
              } catch (SQLException e) {
                  Dialog d = new Dialog(this);
                  d.setTitle("Dang");
                  TextView tv = new TextView(this);
                  tv.setText("error");
                  d.setContentView(tv);
                  d.show();
              }
              break;

1 个答案:

答案 0 :(得分:2)

username字符串。因此,您必须使用string delimiter')包围它。

所以:

db.delete(DATABASE_TABLE, KEY_NAME + "= '" + userName + "'",null);

或(更好)使用绑定参数

所以:

db.delete(DATABASE_TABLE, KEY_NAME + "= ?", new String[]{userName});

在这种情况下,Android本身会正确处理字符串,因此您不必添加字符串分隔符。