Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.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 在打开之前在SQLite中创建数据库_Java_Android_Sqlite_Database Create - Fatal编程技术网

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

Java 在打开之前在SQLite中创建数据库,java,android,sqlite,database-create,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 = "UserNa

我希望根据数据库中已有的表查询我的用户名。但当我执行代码时,我的应用程序强制关闭,因为“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) {
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) {
}

以及MainActivity.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) {
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) {
}

日志:

您可能将表名从某物更新为
UserNamePassword
,但没有更改
数据库版本
,这将触发
onUpgrade()

当然,还必须实现
onUpgrade()
来创建适当的表。

是的,它没有因为“没有这样的表”而崩溃“,但由于其他原因,我现在要解决:)非常感谢。你能解释一下为什么这会有不同吗@霍诺,不是这样的。我试图在模拟器上调试应用程序,我不知道为什么,但模拟器没有SD卡。因此应用程序将崩溃。我在一个真实的设备上进行了调试,在编辑了一点代码之后,在DB adapter类中删除了一些bug。另一个错误是我将字符串与“==”运算符进行比较,这不起作用。应该是