0

我为“银行模拟”开发了一个应用程序,它使用 sqlite 创建数据库...当我在手机上运行该应用程序时,它意外停止...。我是否需要安装任何服务器才能在手机上运行基于 sqlite 的应用程序?提前致谢!

DbHelper.java

private static final String DATABASE_NAME = "saket.db";
private static final int DATABASE_VERSION = 1;
public static final String SUBH_TABLE_NAME = "login";
public static final String SUBH_TABLE_DATA = "TBL_Transaction";
public static final String KEY_ROWID = "_id";
private static final String SUBH_TABLE_CREATE =
                "CREATE TABLE " + SUBH_TABLE_NAME + "(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
                "username TEXT NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL, balance INTEGER);";
private static final String SUBH_TABLE_DATA_CREATE =
        "CREATE TABLE " + SUBH_TABLE_DATA + "(" +
        "trans_id INTEGER PRIMARY KEY AUTOINCREMENT, "+
        "user_id INTEGER, " +
        "trans TEXT NOT NULL);";

private static final String SAKET_DB_ADMIN = "INSERT INTO "+ SUBH_TABLE_NAME +" values(1, admin, password, admin@gmail.com);";
//private static final String SAKET_DB_ADMIN_Trans = "INSERT INTO "+ SUBH_TABLE_DATA +" values(1, asdf);";


public DbHelper(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    System.out.println("In constructor");

}

/* (non-Javadoc)
 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
 */
@Override
public void onCreate(SQLiteDatabase db) {

    try{
        //Create Database
        db.execSQL(SUBH_TABLE_CREATE);

        //create transaction account
        db.execSQL(SUBH_TABLE_DATA_CREATE);

        //create admin account
        db.execSQL(SAKET_DB_ADMIN);

        //db.execSQL(SAKET_DB_ADMIN_Trans);

        System.out.println("In onCreate");
    }catch(Exception e){
        e.printStackTrace();
    }
}

数据库活动.java

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mNewUser = (Button) findViewById(R.id.buttonNewUser);
        mNewUser.setOnClickListener(this);

        mLogin = (Button) findViewById(R.id.buttonLogin);
        mLogin.setOnClickListener(this);

        mShowAll = (Button) findViewById(R.id.buttonShowAll);
        mShowAll.setOnClickListener(this);
    }

    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.buttonLogin:
            mUsername = (EditText) findViewById(R.id.editUsername);
            mPassword = (EditText) findViewById(R.id.editPassword);

            String uname = mUsername.getText().toString();
            String pass = mPassword.getText().toString();

            if (uname.equals("") || uname == null) {
                Toast.makeText(getApplicationContext(), "Username Empty",
                        Toast.LENGTH_SHORT).show();
            } else if (pass.equals("") || pass == null) {
                Toast.makeText(getApplicationContext(), "Password Empty",
                        Toast.LENGTH_SHORT).show();
            } else {
                boolean validLogin = false;
                try {
                    validLogin = validateLogin(uname, pass,
                            DatabaseActivity.this);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (validLogin) {
                    System.out.println("In Valid");
                    Intent i_login = new Intent(DatabaseActivity.this,
                            UserLoggedInPage.class);

                    try {
                        id = getID(uname, pass, DatabaseActivity.this);
                        Ubal = getBAL(uname, pass, DatabaseActivity.this);
                    } catch (NumberFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Log.d(TAG, "putting the extra          " + id);
                    i_login.putExtra("key", id);
                    i_login.putExtra("bkey", Ubal);
                    startActivity(i_login);
                    finish();
                }
            }
            break;

        case R.id.buttonNewUser:
            Intent i = new Intent(DatabaseActivity.this, NewUserActivity.class);
            startActivity(i);
            finish();
            break;

        case R.id.buttonShowAll:
            Intent i_admin = new Intent(DatabaseActivity.this, AdminPage.class);
            startActivity(i_admin);
            finish();
            break;
        }
    }

    public boolean validateLogin(String uname, String pass, Context context)
            throws Exception {

        myDb = new DbHelper(context);
        SQLiteDatabase db = myDb.getReadableDatabase();
        // SELECT
        String[] columns = { "_id" };

        // WHERE clause
        String selection = "username=? AND password=?";

        // WHERE clause arguments
        String[] selectionArgs = { uname, pass };

        Cursor cursor = null;
        try {
            // SELECT _id FROM login WHERE username = uname AND password=pass
            cursor = db.query(DbHelper.SUBH_TABLE_NAME, columns, selection,
                    selectionArgs, null, null, null);

            startManagingCursor(cursor);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int numberOfRows = cursor.getCount();

        if (numberOfRows <= 0) {

            Toast.makeText(getApplicationContext(),
                    "Login Failed..\nTry Again", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    // get rowid
    // public int getID(String uname, String pass, Context context)
    // throws Exception {
    //
    // myDb = new DbHelper(context);
    // SQLiteDatabase db = myDb.getReadableDatabase();
    // cursor = db.rawQuery("select * from " + DbHelper.SUBH_TABLE_NAME +
    // " where username = " + uname + "&" + "password = " + pass + ";)", null);
    // if (cursor != null) {
    // if(cursor.moveToFirst()){
    // int id = cursor.getInt(cursor.getColumnIndex(DbHelper.KEY_ROWID));
    // }
    //
    // }
    //
    // return id;
    //
    // }
    public String getID(String uname, String pass, Context context) {
        try {
            String idddd = null;
            SQLiteDatabase db = myDb.getReadableDatabase();

            String[] columns = { "_id" };

            // WHERE clause
            String selection = "username=? AND password=?";

            // WHERE clause arguments
            String[] selectionArgs = { uname, pass };

            Cursor cursor = db.query(DbHelper.SUBH_TABLE_NAME, columns,
                    selection, selectionArgs, null, null, null);
            if (cursor != null) {
                startManagingCursor(cursor);
                while (cursor.moveToNext()) {
                    idddd = cursor.getString(0);
                }
                return idddd;
            }
            System.out.println("Cursor NuLL");

        } catch (Exception e) {
            e.printStackTrace();
        } 
        return null;
    }

    private String getBAL(String uname, String pass,
            DatabaseActivity databaseActivity) {
        try {
            String ballll = null;
            SQLiteDatabase db = myDb.getReadableDatabase();

            String[] columns = { "balance" };

            // WHERE clause
            String selection = "username=? AND password=?";

            // WHERE clause arguments
            String[] selectionArgs = { uname, pass };

            Cursor cursor = db.query(DbHelper.SUBH_TABLE_NAME, columns,
                    selection, selectionArgs, null, null, null);
            if (cursor != null) {
                startManagingCursor(cursor);
                while (cursor.moveToNext()) {
                    ballll = cursor.getString(0);
                }
                return ballll;
            }
            System.out.println("Cursor NuLL");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onDestroy() {
        super.onDestroy();
        if (myDb != null && cursor != null ) {
            cursor.close();
            myDb.close();

        }

    }
}
4

0 回答 0