Error saving data to Sqlite database

Asked

Viewed 409 times

2

create table works, but when you set the code to save, the application compiles but does not save. Which can be?

SQLiteDatabase winlife;
EditText edt_meta;
EditText edt_tempo;
EditText edt_dataExpiracao;

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listagem);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    winlife = openOrCreateDatabase( "winlife.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
    winlife.setVersion(3);
    winlife.setLocale(Locale.getDefault());

    String criaTabelaMetas = "CREATE TABLE IF NOT EXISTS metas (id INTEGER PRIMARY KEY AUTOINCREMENT, descricao TEXT, tempoRealizacao INTEGER, dataExpiracao INTEGER)";
    winlife.execSQL(criaTabelaMetas);

    edt_meta = (EditText) findViewById(R.id.edt_meta);
    edt_tempo = (EditText) findViewById(R.id.edt_tempo);
    edt_dataExpiracao = (EditText) findViewById(R.id.edt_dataExpiracao);


    Button bt_salvar = (Button) findViewById(R.id.bt_salvar);
    bt_salvar.setOnClickListener(this);

    Cursor cur = winlife.rawQuery("SELECT EXISTS (SELECT 1 FROM metas)", null);

    if (cur != null) {
        cur.moveToFirst();
        if (cur.getInt (0) == 0) {
            Intent it = new Intent(this,PrimeiraActivity.class);
            startActivity(it);
            finish();
        } else {
            // Tabela ja contem dados.
        }
    }
}


@Override
public void onClick(View v) {
    if (v.getId()== R.id.bt_salvar) {
        String nomeMeta = edt_meta.getText().toString();
        //String tempoMeta = edt_tempo.getText().toString();

        ContentValues values = new ContentValues();
        values.put("descricao", nomeMeta);
        //values.put("tempoRealizacao", tempoMeta);

        winlife.insert("metas", "", values);
    }
}



@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
              .replace(R.id.container,  placeholderFragment.newInstance(position + 1))
            .commit();
  }

public void onSectionAttached(int number) {
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.listagem, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }



    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((ListagemActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

}

  • I believe the problem is due to you pass "" in the parameter nullColumnHack, pass null. The parameter nullColumnHack should only be used when you want to insert an empty row, by a limitation of the sqlite, cannot do Insert’s without passing at least one column name.

  • 1

    Tested and did not work, keep giving error!

  • send the error there

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.