0

DbHelper.java here i created two Table 'login' and 'TBL_transaction'

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 + "(" +
            "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);";


    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 admin account
            db.execSQL(SAKET_DB_ADMIN);

            //create transaction account
            db.execSQL(SUBH_TABLE_DATA_CREATE);
            System.out.println("In onCreate");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

NewUserAcitvity.java here when i click the register button it should add a row in the login table...but it shows an error while inserting

public void onClick(View v) {

        switch (v.getId()) {

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

        case R.id.buttonRegister:

            mUsername = (EditText) findViewById(R.id.editUsername);
            mPassword = (EditText) findViewById(R.id.editPassword);
            mEmail = (EditText) findViewById(R.id.editEmail);

            String uname = mUsername.getText().toString();
            String pass = mPassword.getText().toString();
            String email = mEmail.getText().toString();
            boolean invalid = false;

            if (uname.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(), "Username Missing",
                        Toast.LENGTH_SHORT).show();
            } else if (pass.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(), "Password Missing",
                        Toast.LENGTH_SHORT).show();
            } else if (email.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(), "Email ID Missing",
                        Toast.LENGTH_SHORT).show();
            }

            if (invalid == false) {
                addEntry(uname, pass, email);
                Intent i_register = new Intent(NewUserActivity.this,
                        DatabaseActivity.class);
                startActivity(i_register);
                finish();
            }

            break;
        }
    }

    public void onDestroy() {
        super.onDestroy();
        myDb.close();

    }

    public void addEntry(String uname, String pass, String email) {

        SQLiteDatabase db = myDb.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("username", uname);
        values.put("password", pass);
        values.put("email", email);
        values.put("balance", 100);

        try {
            db.insert(DbHelper.SUBH_TABLE_NAME, null, values);
            Toast.makeText(getApplicationContext(), "Inserted",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
4

2 回答 2

0

创建数据库后,您是否添加了“余额”列?如果是这样,您将需要升级您的数据库(删除表然后重新创建它)。或者你可以卸载你的应用程序来清理数据库。

于 2013-08-05T15:27:36.807 回答
0

你的桌子需要空间。具体here"_id INTEGER PRIMARY KEY AUTOINCREMENT," --> No space present after comma和here "user_id INTEGER," --> No space present after comma

于 2013-08-05T15:42:44.893 回答