DbHelper.javaここで、2 つのテーブル 'login' と '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ここで登録ボタンをクリックすると、ログインテーブルに行が追加されます...しかし、挿入中にエラーが表示されます
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();
}
}
}