Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修复使我的应用程序崩溃的数据库问题?_Java_Android_Sqlite - Fatal编程技术网

Java 如何修复使我的应用程序崩溃的数据库问题?

Java 如何修复使我的应用程序崩溃的数据库问题?,java,android,sqlite,Java,Android,Sqlite,我添加了一个新用户,但当我尝试登录时,它会使我的应用程序崩溃。它告诉我没有名称列。我们将非常感谢您的任何帮助。这是我的密码: public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL( "create table users " + "(id integer primary key, name te

我添加了一个新用户,但当我尝试登录时,它会使我的应用程序崩溃。它告诉我没有名称列。我们将非常感谢您的任何帮助。这是我的密码:

 public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(
            "create table users " +
                    "(id integer primary key, name text,password text,age integer)"
    );
    db.execSQL(
            "create table tasks " +
                    "(id integer primary key, name text, agemin integer,agemax integer,time integer)"
    );
}
public boolean insertUser (String name, String password, int age) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("name", name);
    contentValues.put("password", password);
    contentValues.put("age", age);
    db.insert("users", null, contentValues);
    return true;
}

public boolean checkPassword(String name, String password) {
    int id = getUserIDByName(name);
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select password from users where id="+id+"", null );
    String pass = res.getString(1);
    return (password.equals(pass));
}

public int getUserIDByName(String name) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select id from users where name="+name+"", null );
    int id = res.getInt(1);
    return id;

我建议如下,因为它可以防止sql注入

String query = "select id from users where name=?";
Cursor cursor = db.rawQuery(query, new String [] {name});
但如果您想使用原始查询,请确保在字符串值周围加引号

改变

Cursor res =  db.rawQuery( "select id from users where name="+name+"", null );


由于要在getUserIdbyName函数中比较名称,所以在查询中应该使用colun

public int getUserIDByName(String name) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select id from users where name='"+name+"'", null );
    int id = res.getInt(1);
    return id;
}
整数和数字数据类型在SQL中很容易比较,当涉及到字符串或文本类型时,有时会非常棘手且有点困难


希望这对你有用

首先在名称变量周围使用引号:

Cursor res =  db.rawQuery( "select id from users where name = '"+ name + "'", null );
然后改为:

int id = res.getInt(0);
public boolean checkPassword(String name, String password) {
    int id = getUserIDByName(name);
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select password from users where id="+id+"", null );
    String pass = "";
    if (res.moveFirst())
        pass = res.getString(0);
    return (password.equals(pass));
}

public int getUserIDByName(String name) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select id from users where name='"+name+"'", null );
    int id = 0;
    if (res.moveFirst())
        id = res.getInt(0);
    return id;
}
这是:

String pass = res.getString(0);
游标列的索引基于0。 此外,还必须使用moveFirst检查光标是否提取了任何行。 因此,请改为:

int id = res.getInt(0);
public boolean checkPassword(String name, String password) {
    int id = getUserIDByName(name);
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select password from users where id="+id+"", null );
    String pass = "";
    if (res.moveFirst())
        pass = res.getString(0);
    return (password.equals(pass));
}

public int getUserIDByName(String name) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select id from users where name='"+name+"'", null );
    int id = 0;
    if (res.moveFirst())
        id = res.getInt(0);
    return id;
}

把你的事故日志贴出来。我照你说的做了,但还是不行。现在它告诉我索引-1被请求,大小为6,我做了你让我做的,但它仍然不起作用。现在它告诉我请求索引-1,大小为6