如何检查用户名和密码是否在数据库SQLite中?

时间:2017-04-05 19:13:04

标签: android sqlite android-sqlite

尝试进行基本的登录活动,但我仍然对这一切都很陌生。 我在数据库中有一个用户名和密码(我将添加功能,以便尽快添加更多用户)。我想要的是当他们输入用户名和密码时,它会检查数据库中是否存在该对,如果是,则返回true,否则返回false。

所以这是我的数据库连接器

    public boolean checkLogin(String username, String password) {
    SQLiteDatabase db = md.getWritableDatabase();

    String s;
    Cursor c = db.rawQuery("SELECT * FROM MyTable WHERE " + username + " =? AND" + password + " =?", null);

    if(c.getCount() <= 0) {
        c.close();
        db.close();
        return false;
    } else {
        c.close();
        db.close();
        return true;
    }
}

这是调用代码的地方 - 在我的主要活动中

        btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Boolean validUser = myDB.checkLogin(username.getText().toString(), password.getText().toString());

            if(validUser == true) {
                Intent i = new Intent(getApplicationContext(), DBTest.class);
                startActivity(i);
            } else {
                Toast.makeText(getApplicationContext(), "Invalid login", Toast.LENGTH_SHORT).show();
            }

        }
    });

这是错误。据我所知,这是一个语法错误,但我不太确定如何去修改它

04-05 19:08:46.536 11313-11313 / com.example.c1641195.testproject E / SQLiteLog:(1)“AND1234”附近:语法错误 04-05 19:08:46.536 11313-11313 / com.example.c1641195.testproject D / AndroidRuntime:关闭VM 04-05 19:08:46.536 11313-11313 / com.example.c1641195.testproject E / AndroidRuntime:FATAL EXCEPTION:main                                                                                   处理:com.example.c1641195.testproject,PID:11313                                                                                   android.database.sqlite.SQLiteException:near“AND1234”:语法错误(代码1):,同时编译:SELECT * FROM History WHERE Admin =? AND1234 =?                                                                                       在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)                                                                                       在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)                                                                                       在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)                                                                                       在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                                                                                       在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)                                                                                       在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37)                                                                                       在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)                                                                                       在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)                                                                                       在android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)                                                                                       在com.example.c1641195.testproject.MyDBConnector.checkLogin(MyDBConnector.java:66)                                                                                       在com.example.c1641195.testproject.MainActivity $ 1.onClick(MainActivity.java:30)                                                                                       在android.view.View.performClick(View.java:5198)                                                                                       在android.view.View $ PerformClick.run(View.java:21147)                                                                                       在android.os.Handler.handleCallback(Handler.java:739)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                       在android.os.Looper.loop(Looper.java:148)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

3 个答案:

答案 0 :(得分:0)

此行" =? AND" + password上存在语法错误,该错误结合了AND和返回的密码。因此,请尝试在" =? AND " + password

之后留下空格的AND

答案 1 :(得分:0)

也许你应该试试这个:

"SELECT t FROM MyTable t WHERE t.username = '" username + "' AND t.password = '" + password+"'", null

答案 2 :(得分:0)

您的查询如下

<TabControl ... >
    <TabControl.Resources>
        <Style TargetType="TabItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TabItem">
                        <Grid SnapsToDevicePixels="True">
                            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                                <ContentPresenter x:Name="Content" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" TargetName="Bd">
                                    <Setter.Value>
                                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                            <GradientStop Color="#FFEAF6FD" Offset="0.15"/>
                                            <GradientStop Color="#FFD9F0FC" Offset="0.5"/>
                                            <GradientStop Color="#FFBEE6FD" Offset="0.5"/>
                                            <GradientStop Color="#FFA7D9F5" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="Tag">
                                <Trigger.Value>
                                    <sys:Boolean>True</sys:Boolean>
                                </Trigger.Value>
                                <Setter Property="Background" TargetName="Bd">
                                    <Setter.Value>
                                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                            <GradientStop Color="#FFFFEAC7" Offset="0.15"/>
                                            <GradientStop Color="#FFFFE9C2" Offset="0.5"/>
                                            <GradientStop Color="#FFFFE1AD" Offset="0.5"/>
                                            <GradientStop Color="#FFFFDDA3" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Panel.ZIndex" Value="1"/>
                                <Setter Property="Background" TargetName="Bd" Value="#FFF9F9F9"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="Tag">
                                        <Condition.Value>
                                            <sys:Boolean>True</sys:Boolean>
                                        </Condition.Value>
                                    </Condition>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="#FFFFE5C1"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="False"/>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="#FF3C7FB1"/>
                            </MultiTrigger>
                            <Trigger Property="TabStripPlacement" Value="Bottom">
                                <Setter Property="BorderThickness" TargetName="Bd" Value="1,0,1,1"/>
                            </Trigger>
                            <Trigger Property="TabStripPlacement" Value="Left">
                                <Setter Property="BorderThickness" TargetName="Bd" Value="1,1,0,1"/>
                            </Trigger>
                            <Trigger Property="TabStripPlacement" Value="Right">
                                <Setter Property="BorderThickness" TargetName="Bd" Value="0,1,1,1"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="TabStripPlacement" Value="Top"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Margin" Value="-2,-2,-2,-1"/>
                                <Setter Property="Margin" TargetName="Content" Value="0,0,0,1"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="TabStripPlacement" Value="Bottom"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Margin" Value="-2,-1,-2,-2"/>
                                <Setter Property="Margin" TargetName="Content" Value="0,1,0,0"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="TabStripPlacement" Value="Left"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Margin" Value="-2,-2,-1,-2"/>
                                <Setter Property="Margin" TargetName="Content" Value="0,0,1,0"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="TabStripPlacement" Value="Right"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Margin" Value="-1,-2,-2,-2"/>
                                <Setter Property="Margin" TargetName="Content" Value="1,0,0,0"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Background" TargetName="Bd" Value="#FFF4F4F4"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="#FFC9C7BA"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.Resources>
    <TabItem></TabItem>
</TabControl>

您没有使用表格列名称并将用户名和pwd ...传递给您的表格列名称,并且在查询中'AND'后添加空格。

所以你的查询应该是

SELECT * FROM History WHERE Admin =? AND1234 =?