データベース内の既存のテーブルに対してユーザー名を照会したいと考えています。しかし、コードを実行すると、「SQLite がエラー コードを返しました: 1, msg = near "=": syntax error.
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) {
}
ログキャット: