我希望针对数据库中已经存在的表查询我的用户名。但是当我执行我的代码时,由于“SQLite 返回错误代码:1,msg = near”=:语法错误,我的应用程序强制关闭。
这是 DBAdapter.java 文件
package com.example.usernammepassword;
import android.content.ContentValues;
public class DBAdapter {
public static final String KEY_NAME = "UserName";
public static final String KEY_PASS = "Password";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "Test";
private static final String DATABASE_TABLE = "UsernamePassword";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table if not exists UsernamePassword (UserName text not null primary key, Password text not null);";
private final Context context;
DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db1) {
try {
db1.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public DBAdapter open() throws SQLException {
public void close() {
public long insertNewUser(String UserName, String Password) {
public boolean deleteUser(String UserName)
public Cursor getAllUserNamesAndPasswords()
{
return db.query(DATABASE_TABLE, new String[] { KEY_NAME,
KEY_PASS}, null, null, null, null, null);
}
public Cursor getPasswordForUserName(String UserName) throws SQLException
public boolean updatePasswordForUserName( String UserName, String Password) {
}
和 MainActivity.java 文件
package com.example.usernammepassword;
import android.os.Bundle;
public class MainActivity extends Activity {
private String md5(String in) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText text = (EditText)findViewById(R.id.editText1);
EditText text1 = (EditText)findViewById(R.id.editText2);
String userid = text.getText().toString();
String pass = text1.getText().toString();
Toast.makeText(MainActivity.this,"Entered "+userid+" and password entered is "+pass,Toast.LENGTH_LONG).show();
pass = md5(pass + "@string/salt");
Toast.makeText(MainActivity.this,"Password after adding a salt and md5 hashing is now equal to " + pass,Toast.LENGTH_LONG).show();
DBAdapter db = new DBAdapter(MainActivity.this);
db.open();
Cursor c = db.getPasswordForUserName(userid);
if(c.moveToFirst())
{
if(c.getString(1) == pass)
{
Toast.makeText(MainActivity.this, "Authentication Succeded", Toast.LENGTH_SHORT).show();
//proceed
}
else
{
Toast.makeText(MainActivity.this, "@string/AuthFail", Toast.LENGTH_SHORT).show();
//AuthFailure
}
}
else
{
Toast.makeText(MainActivity.this,"@string/UserNotFound", Toast.LENGTH_SHORT).show();
//where to from here
}
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
}
日志猫: