如何在SQLite数据库中获取并使用游标?

huangapple go评论69阅读模式
英文:

How to get a Cursor in sqlite db to work?

问题

我已经创建了一个用户注册活动,并成功将用户详细信息添加到数据库中,但现在我正在尝试使用户能够使用相同的详细信息登录,但我遇到了一个问题,代码中有一个错误,指出需要逗号或分号,但我不认为那是我问题的正确答案。有人能看出我的错误可能是什么吗?错误出现在以下代码中的“SELECT * FROM”处,from 是错误,而“WHERE”是另一个错误。

MainActivity 类:

public class MainActivity extends AppCompatActivity {

    SQLiteDatabase db;
    SQLiteOpenHelper openHelper;
    Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        openHelper = new DatabaseHelperUser(this);
        db = openHelper.getReadableDatabase();

        final EditText userEdit = findViewById(R.id.editTextUsername);
        final EditText passwordEdit = findViewById(R.id.editTextTextPassword);
        final Button loginBt = findViewById(R.id.buttonLogin);

        loginBt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
               String userEt = userEdit.getText().toString();
               String userP = passwordEdit.getText().toString();

               cursor = db.rawQuery("SELECT * FROM " + DatabaseHelperUser.TABLE_NAME + " WHERE " + DatabaseHelperUser.COL2 + "=? AND " + DatabaseHelperUser.COL3 + "=?", new String[]{userEt, userP});
            }
        });
    }
}

DatabaseHelperUser 类:

public class DatabaseHelperUser extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "User.db";
    public static final String TABLE_NAME = "User_table";
    public static final String COL1 = "UserNum";
    public static final String COL2 = "UserName";
    public static final String COL3 = "Password";
    public static final String COL4 = "BirthDate";
    public static final String COL5 = "Phone";
    public static final String COL6 = "Address";

    public DatabaseHelperUser(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (UserNum TEXT, UserName Text, Password Text, BirthDate Text, Phone Text, Address Text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}
英文:

Ive made a user register activity and it succesfully adds the user details to the database but now I am trying to get the user to be able to login with the same details but I have ran into a problem where I have a error in the code below saying comma or semi column needed but I dont think thats the correct answer to my problem. Can anyone see why my error would be happening? The error is where it says "SELECT * FROM" the from is the error and "WHERE" is the other error.

MainActivity class:

public class MainActivity extends AppCompatActivity {

SQLiteDatabase db;
SQLiteOpenHelper openHelper;
Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    openHelper = new DatabaseHelperUser(this);
    db = openHelper.getReadableDatabase();


    final EditText userEdit = findViewById(R.id.editTextUsername);
    final EditText passwordEdit = findViewById(R.id.editTextTextPassword);
    final Button loginBt = findViewById(R.id.buttonLogin);

    loginBt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {


           String userEt = userEdit.getText().toString();
           String userP = passwordEdit.getText().toString();


          cursor = db.rawQuery("SELECT * FROM" + DatabaseHelperUser.TABLE_NAME + "WHERE" + DatabaseHelperUser.COL2 + "=? AND" + DatabaseHelperUser.COL3 + "=?", new String[]{userEt, userP});

DatabaseHelperUser class:

public class DatabaseHelperUser extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "User.db";
public static final String TABLE_NAME = "User_table";
public static final String COL1 = "UserNum";
public static final String COL2 = "UserName";
public static final String COL3 = "Password";
public static final String COL4 = "BirthDate";
public static final String COL5 = "Phone";
public static final String COL6 = "Address";


public  DatabaseHelperUser(Context context){
    super(context, DATABASE_NAME, null, 1);
}


@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table " + TABLE_NAME + " (UserNum TEXT,UserName Text,Password Text,BirthDate Text,Phone Text,Address Text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

}

答案1

得分: 1

请在您的问题中包含完整的错误文本,但在我看来,它看起来像是:

cursor = db.rawQuery("SELECT * FROM" + DatabaseHelperUser.TABLE_NAME
                                + "WHERE" + DatabaseHelperUser.COL2 +
                               "=? AND" + DatabaseHelperUser.COL3 + "=?", 
                               new String[]{userEt, userP});

我注意到在 FROMWHEREAND 之后没有尾随空格,由于您的 TABLE_NAME 等没有前导空格,FROMUser_tableWHEREUserName 将成为语法错误。

英文:

Please include the full text of the error in your question, but it looks to me like:

cursor = db.rawQuery("SELECT * FROM" + DatabaseHelperUser.TABLE_NAME
                            + "WHERE" + DatabaseHelperUser.COL2 +
                           "=? AND" + DatabaseHelperUser.COL3 + "=?", 
                           new String[]{userEt, userP});

I see that there is no trailing space after FROM, WHERE and AND, and since your TABLE_NAME etc does not have a leading space, FROMUser_tableWHEREUserName is going to be a syntax error.

答案2

得分: 0

你必须在命令的 FROMWHEREAND 部分添加空格:

cursor = db.rawQuery("SELECT * FROM " + DatabaseHelperUser.TABLE_NAME + " WHERE " + DatabaseHelperUser.COL2 + "=? AND " + DatabaseHelperUser.COL3 + "=?", new String[]{userEt, userP});
英文:

You must add space to command FROM , WHERE, AND :

cursor = db.rawQuery("SELECT * FROM " + DatabaseHelperUser.TABLE_NAME + " WHERE " + DatabaseHelperUser.COL2 + "=? AND " + DatabaseHelperUser.COL3 + "=?", new String[]{userEt, userP});

huangapple
  • 本文由 发表于 2020年10月1日 12:24:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64149053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定