1

How do I insert radiobutton into sqlite? I have 1 radiobutton group and 2 radiobutton.

Current code error is "myTable has no column named Male (code)".

/Database class
 public void CreateDatabase(string sqldb_name)
{
    try
    {
        sqldb_message = "";
        string sqldb_location = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        string sqldb_path = Path.Combine(sqldb_location, sqldb_name);
        bool sqldb_exists = File.Exists(sqldb_path);
        if (!sqldb_exists)
        {
            sqldb = SQLiteDatabase.OpenOrCreateDatabase(sqldb_path, null);
            sqldb_query = "CREATE TABLE IF NOT EXISTS MyTable (id_key INTEGER PRIMARY KEY AUTOINCREMENT,PhoneNumber VARCHAR, Name VARCHAR, Password VARCHAR, DateOfBirth VARCHAR, Male VARCHAR, Female VARCHAR);";
            sqldb.ExecSQL(sqldb_query);
            sqldb_message = "Database: " + sqldb_name + " created";
        }
        else
        {
            sqldb = SQLiteDatabase.OpenDatabase(sqldb_path, null, DatabaseOpenFlags.OpenReadwrite);
            sqldb_message = "Database: " + sqldb_name + " opened";
        }
        sqldb_available = true;

    }
    catch (SQLiteException ex)
    {
        sqldb_message = ex.Message;
    }
}

public void AddRecord(string PhoneNumber, string Pwd, string Name, string DOB, string Male, string Female)
{
    try
    {
        sqldb_query = "INSERT INTO MyTable (PhoneNumber, Password, Name, DateOfBirth, Male, Female) VALUES (" + "'" + PhoneNumber + "','" + Pwd + "','" + Name + "','" + DOB + "','" + Male + "','" + Female + "');";
        sqldb.ExecSQL(sqldb_query);
        sqldb_message = "Record saved";
    }

    catch (SQLiteException ex)
    {
        sqldb_message = ex.Message;
    }
}

////////////////////////////////////////////////////////////////////////////

//Mainactivity

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.RegisterLay);


    sqldb = new database("person_db");

    btnRegister = FindViewById<Button>(Resource.Id.btnRegister);

    //Gets EditText object instances
    Male = FindViewById<RadioButton>(Resource.Id.rdbMale);
    Female = FindViewById<RadioButton>(Resource.Id.rdbFemale);

    //Creates ImageButton click event for imgAdd, imgEdit, imgDelete and imgSearch
    btnRegister.Click += async delegate
    {
        sqldb.AddRecord(PhoneNumber.Text, MD5(Password.Text), Name.Text, DateOfBirth.Text, Male.Text, Female.Text);


        PhoneNumber.Text = Password.Text = Name.Text = DateOfBirth.Text = Male.Text = Female.Text = "";

        await Task.Delay(2000);
        StartActivity(typeof(MainActivity));
    }
            };
0m3r
  • 12,286
  • 15
  • 35
  • 71
terry
  • 43
  • 7

1 Answers1

0

Your code looks fine, but it seems that your have changed your database structure lately and added some columns.

When you check if the database exists, it doesn't compare structures. So I think you need to completely uninstall your app or (Clear Data) and then do a clean install, and then the error should be gone.

If the problem in not solved, you can extract your database and use a software to view its tables.

Community
  • 1
  • 1
Zein Makki
  • 29,485
  • 6
  • 52
  • 63