SQLite在模拟器上运行完美,但无法在移动设备上运行

时间:2019-05-11 10:41:15

标签: android sqlite

我在Android项目中使用 SQLite ,并且在 Emulator 上运行时运行良好,但是当我使用 USB设备安装应用程序 Android手机上,它甚至不允许我登录,显示我为错误的用户名或密码实施的错误。

这是DatabaseHelper的代码,其中我正在使用将数据插入数据库并从数据库中进行选择的功能。 usernamepassword 功能用于从数据库中选择用户。

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Khataapp.db";
    public static final String TABLE_NAME = "users_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "USERNAME";
    public static final String COL_3 = "PASSWORD";
    public static final String COL_4 = "ROLE";


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 2);
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,USERNAME TEXT, PASSWORD TEXT, ROLE INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("create table products (ID INTEGER PRIMARY KEY AUTOINCREMENT, PRODUCT_TITLE TEXT, PRODUCT_PRICE TEXT, PRODUCT_DESCRIPTION TEXT, PRODUCT_IMAGE BLOB)");
    }
//insert data
    public boolean insertData(String username, String password,final int role){
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,username);
        contentValues.put(COL_3,password);
        contentValues.put(COL_4,role);
        long result = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }

//    select username password
    public boolean usernamepassword(String username, String password)
    {
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("select * from users_table where username=? and password=?",new String[]{username,password});
        if (cursor.getCount()>0)
            return true;
        else
            return false;
    }

//    add products
   public void insertProducts(String pTitle, String pPrice, String pDescription, byte[] pImage){
        SQLiteDatabase database = getWritableDatabase();
        String sql = "INSERT INTO products VALUES (NULL, ?, ?, ?, ?)";

       SQLiteStatement statement = database.compileStatement(sql);
       statement.clearBindings();

       statement.bindString(1, pTitle);
       statement.bindString(2, pPrice);
       statement.bindString(3, pDescription);
       statement.bindBlob(4, pImage);

       statement.executeInsert();
   }

   public Cursor getProducts(String sql){
        SQLiteDatabase database = getReadableDatabase();
        return database.rawQuery(sql,null);
   }

    public Cursor getData(String sql){
        SQLiteDatabase database = getReadableDatabase();
        return database.rawQuery(sql, null);
    }

}

登录活动


public class Login extends AppCompatActivity {
    Button login_btn;
    EditText et_username,et_password;
    DatabaseHelper mydb;
    Session sessionManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mydb = new DatabaseHelper(this);

        sessionManager = new Session(this);

        login_btn = (Button) findViewById(R.id.login);
        et_password = (EditText) findViewById(R.id.password);
        et_username = (EditText) findViewById(R.id.username);
        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String username = et_username.getText().toString();
                        String password = et_password.getText().toString();
                        Boolean chkuser = mydb.usernamepassword(username,password);
                        if (chkuser == true){
                            sessionManager.createSession(username);
                            Intent myIntent = new Intent(Login.this,
                                    MainActivity.class);
                            startActivity(myIntent);
                        }
                        else{
                            Toast.makeText(getApplicationContext(),"username or password is incorrect",Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

工作完美,请尝试

// call below class from activity or fragment
    DBClass dbClass=new DBClass(getActivity());
    dbClass.insertData("abc","xyz","student");


public class DBClass extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Khataapp.db";
public static final String TABLE_NAME = "users_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "USERNAME";
public static final String COL_3 = "PASSWORD";
public static final String COL_4 = "ROLE";
private static final int DATABASE_VERSION      = 3;

public DBClass(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_NAME + "( " +
            COL_1 + " INTEGER PRIMARY KEY, " +
            COL_2 + " STRING, " +
            COL_3 + " STRING, " +
            COL_4 + " STRING" + ")";

    db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}


//insert data
public boolean insertData(String username, String password,String  role){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2,username);
    contentValues.put(COL_3,password);
    contentValues.put(COL_4,role);
    long result = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
    Log.d("val_result",""+result);
    if (result == -1)
        return false;
    else
        return true;
}

 // user login
  public boolean userLogin(String username, String password)
{
    SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery("select * from "+TABLE_NAME+" where "+COL_2+"=? and "+COL_3+"=?",new String[]{username,password});
    if (cursor.getCount()>0) {
        Log.d("val_", "success");
        return true;
    }
    else {
        return false;
    }
}

}