在打开之前在SQLite中创建数据库

时间:2013-03-08 00:01:48

标签: java android sqlite database-create

我希望根据数据库中已有的表查询我的用户名。但是当我执行我的代码时,由于“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) {

}

Logcat: enter image description here

1 个答案:

答案 0 :(得分:0)

您可能已将您的表名从某些内容更新为UserNamePassword,但未更改DATABASE_VERSION,这将触发onUpgrade()。 当然,还必须实现onUpgrade()来创建适当的表。